What is Express.js and Why would we use it?

rukonpro profile

Rukon Uddin

08/25/2024

What is Express.js and Why would we use it?

Express.js 

is a free open-source Node.js web application framework. It is used to quickly and easily design and build web applications.Express.js requires only JavaScript, so it is easy for programmers and developers to create web applications and APIs without much effort.



which means that most of the code is written for the work of developers. You can create a single-page, multi-page or hybrid website using Express.js. Express.js helps organize lightweight and server-side web applications into more organized MVC architectures. Learning JavaScript and HTML is very important to be able to use Express.js.



Express.js makes it easy to manage web applications It is a part of JavaScript-based technology which is MEAN software undefined Known as Stack which stands for MongoDB, ExpressJS, AngularJS, and Node.js.Express.js is part of MEAN's backend and handles routing, sessions, HTTP requests, errors. Express.js's JavaScript library helps programmers build web apps faster. Express.js doubles the functionality of node.js. In fact, if you do not use Express.



js, then you had to do a lot of complex programming to create an efficient and good quality API. This makes programming in node.js effortless and adds many extra features. undefined



How does Express.js Work?



Below is a block image of how Express.js works with Node.js and MongoDB.



Node.js is an event-driven server with a single thread, to manage all connections to the server. It processes as many callback requests as possible without delay.



As you can see from the image above, as soon as the request enters the server, Express processes the callback immediately without blocking the main stack.



The server accepts requests and registers event data processing. The event then sends a request to the remote Mongo server. Once this is done, it takes some time for the data to return. At the same time, the server is processing other requests. When the data arrives, the Mongo driver calls the user request handler. The user request handler returns the data to the client.




What is REST API?



REST is the representation state transfer. Where HTTP clients can request information from the server via the HTTP protocol.



REST is the representation state transfer. Where HTTP clients can request information from the server via the HTTP protocol.




HTTP Request Types:



There are several types of HTTP methods we need to know before creating a REST API. These methods are compatible with CRUD functions:



Post: Used to submit data, usually used to create new entities or edit existing entities.



GET: Used to request data from the server usually used to read data



PUT: Used to completely replace resources with submitted resources Used to update data.



DELETE: Used to delete an entity from the server.




Here is a brief overview on how to create API in Node.js with Express.js;



import 'dotenv/config';

...

import express from 'express';




const app = express();

...

app.get('/', (req, res) => {

 return res.send('Received a GET HTTP method');

});

app.post('/', (req, res) => {

 return res.send('Received a POST HTTP method');

});

app.put('/', (req, res) => {

 return res.send('Received a PUT HTTP method');

});

app.delete('/', (req, res) => {

 return res.send('Received a DELETE HTTP method');

});

app.listen(process.env.PORT, () =>

 console.log(`Example app listening on port undefined!`),

);