Overview

Middleware functions are functions that have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle. The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.

Middleware functions can perform the following tasks:

  • Execute any code.
  • Make changes to the request and the response objects.
  • End the request-response cycle.
  • Call the next middleware in the stack.

If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.


Var express = require(‘express’);

var app = express();

app.get(‘/’ , function(req ,res , next){ next(); });

app.listen(1337);

 

Explained : 


app.get(............) // get/post HTTP method for which the middleware function applies.

App.get( ‘/’ , fun...);

(‘/path url ’ , .......) //Path (route) for which the middleware function applies.

function(req , res , next )

req - HTTP request argument to the middleware function, called "req" by convention.

res - HTTP response argument to the middleware function, called "res" by convention.

next - Callback argument to the middleware function, called "next" by convention.

Example : 

Here is an example of a simple “Hello World” Express application. The remainder of this article will define and add two middleware functions to the application: one called myLoggerthat prints a simple log message and another called requestTime that displays the timestamp of the HTTP request.


var express = require('express')
var app = express() app.get('/', function(req, res) {
res.send('Hello World!')
})

app.listen(1337);

Middleware function Logger : 

Here is a simple example of a middleware function called “Logger”. This function just prints “LOGGED” when a request to the app passes through it. The middleware function is assigned to a variable named.myLogger


var Logger = function(req, res, next) {
console.log('LOGGED') next()
}

Notice the call above tonext(). Calling this function invokes the next middleware function in the app. The next()function is not a part of the Node.js or Express API but is the third argument that is passed to the middleware function. The next() function could be named anything, but by convention, it is always named “next”. To avoid confusion, always use this convention.

To load the middleware function, call,app.use() specifying the middleware function. For example, the following code loads the myLogger middleware function before the route to the root path (/).


var express = require('express')

var app = express() var Logger = function(req, res, next) {
console.log('LOGGED')
next()
}
app.use(Logger)

// middleware calling
app.get('/', function(req, res) {
res.send('Hello World!')
})

app.listen(1337);

Every time the app receives a request, it prints the message “LOGGED” to the terminal. The middleware functionLoggersimply prints a message, then passes on the request to the next middleware function in the stack by calling the next()function.

Configurable middleware

If you need your middleware to be configurable, export a function which accepts an options object or other parameters, which, then returns the middleware implementation based on the input parameters.

The middleware can now be used as shown below.

var mw = require('./my-middleware.js')

Refer to cookie-session and compression for examples of configurable middleware.

I have followed the URL: https://expressjs.com/en/guide/writing-middleware.html