How Middlewares Work In ExpressJS

Hello there! 👋
My name is Anyanwu-Ebere Chiemekam and I am a mobile and backend developer. In this article, I will be talking about how to use Middlewares in a server built using ExpressJS.

ExpressJS logo

ExpressJS is a popular, unopinionated Javascript framework that enables developers to build serverside applications on NodeJS using Javascript.

Middlewares in ExpressJS are functions that get executed during the lifecycle of a request made to the ExpressJS server. These functions have access to the request object, response object, and a next function (which when called proceeds to complete the request). They can be used to modify the request and response objects.

Here I will show you how to write your own middlewares and how to use third-party middlewares in your Express.js projects.

Example 1

const express = require("express");
const server = express();

server.use((req, res, next) => {
console.log("hello world!");
next()
})

server.listen(3000)

In this first example, we see how the syntax for using middlewares in Express.js looks like. We use the .use() method from express to implement a middleware. If you notice, the middleware looks a little bit identical to a regular callback function an endpoint gets except for the extra next parameter, which is basically just a function that should be called whenever you want to exit the middleware and proceed to complete the request. Not calling the next function will make your request not to proceed and be completed.
Another important thing to note is that middlewares run in the order in which they are written in the file.

const express = require("express");
const server = express();

// Middleware1
server.use((req, res, next) => {
console.log("i run first!");
next()
})

// Middleware2
server.use((req, res, next) => {
console.log("i run second!");
next()
})

// Middleware3
server.use((req, res, next) => {
console.log("i run third!");
next()
})

server.listen(3000)

When the middleware in the snippet above is run, the console will look like the below.

i run first!
i run second!
i run third!

Example 2

In the example above, the middleware function is run as soon as the user makes a request to any route on the server. But there is also a way to make a middleware run when a specific route is hit.
We can do this by including the middleware function as the second and a third parameter to a route method.

const express = require("express");
const server = express();

const middleware1 = (req, res, next) => {
console.log("i will run only on the profile page");
next();
}

server.get("/profile", middleware1, (req, res) => {
res.send("profile page");
})

server.listen(3000)

Example 3

In these past examples, we have written our own middleware functions. In this example, I will show you how to use third-party middlewares in your projects.
You can find a list of third-party middlewares that work with express here
We will work with the cookie-parser middleware in this example.

  • The cookie-parser module helps Express.js work well with cookies. It populates req.cookies with an object which has cookie names as its keys. You can read more about it on it's NPM page here.

To include the cookie-parser module in your project all you need to do is visit get it from NPM using npm i cookie-parser.
After doing this, you need to include it in your project, and we can get this done like this.

const cookie_parser = require("cookie-parser");

This will import the cookie-parser module to your project file.
After this is done, you need to tell express that you want the cookie-parser middleware to be run by the express framework whenever a request is made. And we can get this done by doing what we did in Example1.

const express = require("express");
const server = express();
const cookie_parser = require("cookie-parser");

server.use(cookie_parser);

server.listen(3000)

After this is done anytime a request is made to the server the cookie-parser middleware will populate req.cookies with an object containing all cookies.

Thank you for reading, let's connect!

Here is my Twitter
And Instagram
And Linkedin