Concept of Middleware in NodeJS
One of the core concepts of express which we need to learn is the concept of middleware or middleware function.
** Middleware function**
A middleware function is a function that takes a request object and either it returns a response to the client or passes control to the next middleware function. Let's see an example of a middleware function.
const express = require('express');
const app = express();
app.use(express.json());
app.get('/', (req, res) => {
res.send('Middleware function')
})
const port = process.env.port || 7000
app.listen(port, () => {console.log(`listening to port ${port}`)});
In the above example, we use two middleware functions they are express.json() and app.get().
In express, every route handler is a middleware function it receives a request object and it sends a response to the client.
express.json() when express call json() method it calls a middleware function. The job of the middleware function is to read the request and if there is a JSON object present in the body it parses the body of the request into a JSON object and set it to req.body property.
Express includes some built-in middleware functions but we can also create custom middleware functions that we can put at the front of the request processing pipeline. So every request comes to the server will go through the middleware function with this we can perform login, authentication, authorization and many.
Creating custom middleware function
Let me show you how to create a custom middleware function.
const express = require('express');
const app = express();
app.use(express.json());
app.use(function(req, res, next){
console.log("Logging")
next(); // If next is to pass control to the next function. If we dont do this our request will end up hanging
})
app.use(function(req, res, next){
console.log("Authentication")
next(); // If next is to pass control to the next function. If we dont do this our request will end up hanging
})
app.get('/', (req, res) => {
res.send('Middleware function')
})
const port = process.env.port || 7000
app.listen(port, () => {console.log(`listening to port ${port}`)});
In this we will create our own custom middleware functions. We can create this middleware functions in seperate files. So that we will not dump our code in index.js file.
const express = require('express');
const app = express();
const logger = require('./logger.js')
const authentication = require('./authentication.js')
app.use(express.json());
app.use(logger)
app.use(authentication)
app.get('/', (req, res) => {
res.send('Middleware function')
})
const port = process.env.port || 7000
app.listen(port, () => {console.log(`listening to port ${port}`)});
}
//logger.js file
function log(req, res, next){
console.log("Logging")
next();
export.modules = log
In the same way, need to create a module for authentication middleware.
//authentication.js file
function authentication(req, res, next){
console.log("Authentication")
next();
}
export.modules = authentication
These two files need to be imported in the index.js file and just call these functions. This makes our index.js file cleaner.
In below link, you can find some third-party middleware functions which are used in nodejs
https://expressjs.com/en/resources/middleware.html