Express.js (or simply, Express) is a Node.js framework that’s free, open-source, and quick. It offers a substantial collection of features and tools, making it a popular choice for developing web applications and APIs in Node.js. It provides Node.js with extra capabilities, such as middleware and routing. Express has become the most popular, widely-used Node.js standard server framework.
In an Express.js application, the HTTP request flow is as follows:
First, the client requests something through the HTTP protocol, the primary communication method in most web applications.
Then the HTTP server sends the request to Express for the required logic. Node.js is the entry point for the server, so it’s the first to receive the proposal before any further processing takes place.
Express adds features to the request and response. It also adds more functionalities to the native request and response, making it easier to handle or manipulate. After that, it passes on the request to the middleware.
Our function responds to the request. The middleware chooses which function to call and processes that request.
Then the HTTP server sends the final response ahead to the client.
Express significantly simplifies and facilitates the back-end construction of a web application. Moreover, it’s more intangible than writing standard JavaScript.
Here are some of the main characteristics of Express:
Efficient
Fast
More organized
Easy to learn
Here are some features of Express:
Routing
Middleware
Templating
Routing describes how an application responds to a client-side request for a particular endpoint.
We can perform many requests on routes using HTTP methods such as GET, POST, PUT, and DELETE.
Middleware in Express.js refers to a collection of functions executed between receiving raw requests and processing them through the final intended route. One typical example of middleware is authentication, which ensures the user is authenticated before accessing specific resources or making certain requests. By using middleware, we can modify and validate incoming requests, add or modify properties of the request or response objects, and control the flow of data within the application.
There are a few different types of middleware:
Application middleware: This is a type of middleware executed for all endpoints and bound to the application using the use()
function. This middleware runs throughout the application, modifies the request and response objects, and controls the dataflow.
Router middleware: This is similar to application middleware but is instead bound to the router object. It’s used to modify requests and responses for specific routes and is added to the router using the use()
function.
Error-handling middleware: This is a specific type of middleware used to detect and handle errors in the application. It has additional arguments in its function signature and is added to the application using the use()
function.
Third-party middleware: This is an integrated middleware from other third-party packages or custom middleware functions that can be used to enhance the functionality of Express.js. Third-party middleware can be easily added to the application using the use()
function, providing additional features and functionality.
In Express.js, templating refers to dynamically rendering HTML pages using data retrieved from a database or other data sources. Several templating engines are available for use with Express.js, including Pug, Handlebars, EJS, and Mustache.
Following is a basic example of the Express.js application:
// Import the required modules const express = require('express'); // Create an Express application const app = express(); // Define a route that responds with "Hello, World!" when you access the root URL app.get('/', (req, res) => { res.send('Hello, World!'); }); // Start the server and listen on port 3000 const port = 3000; app.listen(port, () => { console.log(`Server is running on port ${port}`); });
Here is the explanation of the code:
Line 2: In this code, the require
function is used to import the Express.js module.
Line 5: We created an instance of the Express application.
Line 8–10: A route is defined using the app.get()
method. This route is specified as '/'
, which is the root URL of the web server. When a client (e.g., a web browser) makes a GET request to the root URL, the code inside the callback function is executed. In this case, it sends the response "Hello, World!" back to the client.
Line 13–15: The app.listen()
method starts the Express server and makes it listen on port 3000
. When the server starts successfully, it logs a message to the console to indicate that it's running.
Free Resources