,

Learn how to use middleware in Express JS with this tutorial

Posted by






Express JS Tutorial | Middleware

Express JS Tutorial | Middleware

Middleware is a crucial concept in Express.js, which is a web application framework for Node.js. In this tutorial, we will explore how middleware works and how to use it in your Express.js application.

What is Middleware?

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. These functions can perform various tasks such as parsing the request, adding additional headers to the response, or handling errors. Middleware functions can also terminate the request-response cycle by sending a response back to the client.

Using Middleware in Express.js

To use middleware in Express.js, you simply use the app.use() method to mount the middleware function. For example, the following code snippet shows how to use a middleware function to log the request method and URL for every incoming request:


const express = require('express');
const app = express();

app.use((req, res, next) => {
  console.log(`${req.method} ${req.url}`);
  next();
});

app.get('/', (req, res) => {
  res.send('Hello, World!');
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

In this example, the middleware function logs the request method and URL to the console and then calls the next() function to pass the request to the next middleware function. If you don’t call next(), the request-response cycle will be terminated, and no further middleware functions will be executed.

Built-in Middleware in Express.js

Express.js provides several built-in middleware functions that can be used in your application. Some common built-in middleware includes express.static for serving static files, express.json for parsing JSON request bodies, and express.urlencoded for parsing URL-encoded request bodies. These middleware functions can be used by simply calling app.use() and passing the middleware function as an argument.

Writing Custom Middleware

You can also write your own custom middleware functions to handle specific tasks in your Express.js application. Custom middleware functions can be used to authenticate users, perform input validation, or log requests. To write a custom middleware function, simply define a function with the following signature:


function myMiddleware(req, res, next) {
  // Middleware logic goes here
  next();
}

Once the custom middleware function is defined, you can use it in your application by calling app.use() as shown in the previous examples.

Conclusion

Middleware is a powerful feature of Express.js that allows you to handle requests and responses in a flexible and modular way. In this tutorial, we covered the basics of middleware in Express.js, how to use built-in middleware, and how to write custom middleware functions. With this knowledge, you should be able to effectively use middleware in your Express.js applications to handle various tasks and improve code reusability.