You are currently viewing What is middleware and how to use it using Node.js

What is middleware and how to use it using Node.js

In my previous hands-on, I set up an express server using Node.js and added a very basic “Get” endpoint which returns a response. This hands-on can be found here.

Now, In this hands-on, I will add a middleware in our express server. But before that, let’s understand that what is the middleware .

What is Middleware

In programming, middleware is a software component that resides between two different applications or between different components. It facilitates communication and also provides some features when two application communicates.

Middleware in context of Web Programming

In the context of web programming, middleware allows us to add features to the server by intercepting incoming requests and modifying outgoing responses. 

Here I would like to mention that this concept of middleware is not related to Node.js only.  As per my knowledge, all web programming uses web middleware which includes but is not limited to Express.Js, ASP.Net, Flask, FastAPI, and so on.

Use Cases of Middleware

Now as we know what is middleware, it’s time to see what are the use cases of middleware. 

There could be a lot of use cases of middleware and in the following I am mentioning few of them.

Authentication and Authorization

Since we know that middleware allows us to intercept the incoming request before reaching the actual endpoint or targeted endpoint, we can perform authentication and authorization using the middleware. We can check

  • If the user is a valid user
  • We can check the access privileges
  • etc

Logging

Since we can intercept the incoming request, we can perform logging by using the middleware. In fact many opensource and free-to-use logging middleware is available for many frameworks and a few of them are given below:-

  • Infra.AspNetCore.RequestResponseLoggingMiddleware [For ASP.Net]
  • Morgan [For Node.js]
  • logging [A built-in Package in Python]
  • etc

Input Validation

In middleware, we have access to the whole request object, so we can perform any type of validation in the middleware. We can perform different kinds of tasks such as 

  • Check requests for SQL Injection or Javascript injection
  • Check the incoming data to see if it meets the certain schema etc
  • etc

Error Handling

Using middleware, we can catch errors that occur while processing the application. Just like the logging, we have lot of error-handling middleware in different technology stacks. Few of them are given below:-

  • express-error-handler [For Express Server]
  • Global Error handler [For Next.js]
  • CustomExceptionMiddleware [.Net Standard 2.0]
  • etc

In addition to above, we can use prewritten middleware or can write our own middleware for different kind of tasks such as:-

  • Caching
  • Routing
  • To perform different kind of actions based on our requirments before the request reaches to our end point.
  • etc

Middleware chaining

We can use more than one middleware. In case of more than one middleware, they are chained one after another such that one middleware perform its functionality and then passes the request to the next middleware in the chain. Each middleware can work its designated task and can choose whether to pass the request to the next middleware or not. 

Middleware can be explained in following image

Middleware chaning

If we break down the above image, it’s a chain of middleware that executes sequentially until the response is set in the last endpoint (i.e. which is the final endpoint or actual endpoint for which the request was generated).

In this diagram, we can see that there is a keyword which is “next”. This next indicated that the next middleware in the pipeline will be invoked as soon as the current middleware completes its job. This image represents how the express server (Express.js) works using middleware. However, the same concepts apply to other technology stack as well.

Example of Middleware in Express.js

The first step in writing the middleware in Express is to define our middleware function. Middleware functions accept three (3) input parameters which are given below:-

  • Request
  • Response
  • Next
We can see that the first two input parameters of middleware are the same parameters that exist in our normal router handler.  The only parameter which differentiates it from the normal router handler is the third parameter which is the “next” parameter. 

Define the Middleware function

In the following, I am defining a simple middleware that logs the type of method exposed along with its URL

// Define a custom middleware
const demoMiddleware = (req,res,next)=>{
    console.log(`${req.method} ${req.url}`);
    next();
}

In the above code, at line # 4, we have called the next function. This function is necessary to call in the middleware so that it can pass the request to the next middleware (if there exists any) or to the final endpoint.

Inject the middleware in the Pipeline

To inject the middleware in the pipeline, all we have to do is to use it using the “use” function of our “app”, where the app is the object of the express application. Write the following code to inject the middleware in the express application. Make sure to write this code after initializing your express application 🙂

app.use(demoMiddleware)

Complete Code

const express = require("express")
const dotenv = require("dotenv")
dotenv.config() // this will read the file and will place the values in the process.env
const port = process.env.port
//just for information
console.log("Port:" + port)

// Define a custom middleware
const demoMiddleware = (req,res,next)=>{
    console.log(`${req.method} ${req.url}`);
    next();
}


const app = express();
app.use(demoMiddleware)
app.get("/", function(req,res){
    res.send("Hello World")
})
app.listen(port,()=>{
    console.log(`Server is listening on Port: ${port}`)
})

Output

I’ve executed the above code and run the application. Once the application is up and running, I have executed the following:-

  • http://localhost:8089/
  • http://localhost:8089/Hello
After hitting the above two URLs, I got the following result in my console or terminal due to the middleware which I have injected in the express server.
Middleware output

In the above output, we can see that middleware successfully did its job by logging the method and URL against each request. i.e.

  • Get /
  • Get /Hello

Leave a Reply