English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS Basic Tutorial

NodeJS Express.js

NodeJS Buffer&URL;

NodeJS MySql

NodeJS MongoDB

NodeJS File (FS)

Other NodeJS

Express.js Middleware

Express.js middleware

What is middleware?

Middleware is a function that can access the request and response objects and also use next in the application's request-Using this function in the response cycle.

In this tutorial, we will learn how to define middleware functions in a Node.js Express application and how to call middleware functions.

Middleware terminology

request – It is the HTTP request that arrives at the Express application when the client sends an HTTP request (such as PUT, GET, etc.). It contains properties such as query string, URL parameters, headers, etc.

response– The object represents the HTTP response sent by the Express application when it receives an HTTP request.

next – next is used to continue to the next middleware in the middleware stack.

request-response cycle(Request-Response cycle) – The operation cycle that is executed from the request entering the Express application until the response leaves the application for the request.

Middleware stack– In the request-Middleware function stack executed during the response cycle.

Define middleware function

As we have mentioned in the definition of middleware functions, it can access the request, response object, and the next function.

The syntax is the same as that of JavaScript functions. It accepts the request, response object, and the next function as parameters.

 
 function logger(req, res, next) { 
    
 }

 Here, logger is the function name, req is the HTTP request object, res is the node response object, and next is the request.-The next function in the response cycle.

The next function in the response cycle.

You can access all properties and methods of the request object req.

Similarly, you can access all properties and methods of the response object res.-The function call inside the next() middleware function is optional. If you use the next() statement, then at the request

function logger(req, res, next) { 
    // Your Code
    next() // Call the next function in the middleware stack. If you do not call the next() function, the execution of the given request will stop here.
 }

Call Middleware

In an Express application, you can use the use function on the application object to call middleware.

var express = require('express') 
var app = express() 
 
function logger(req, res, next) { 
   // Your Code
   next() 
 } 
 
app.use(logger)

Express.js Middleware Example

In this example, we will define a middleware named logger that records the current time and query string to the console.

app.js

var express = require('express') 
var app = express() 
 
// Define middleware function
function logger(req, res, next) { 
   console.log(new Date(), req.url) 
   next() 
 } 
 
// At each request-Call logger middleware in the response cycle
app.use(logger) 
 
// For the path “ /”Executed route
app.get('/', function(req, res) { 
   res.send('This is a basic Example for Express.js by w3codebox') 
 ) 
 
// Start the server
var server = app.listen(8000, function() { 
    console.log('Listening on port 8000...') 
 )

Start this application, then click the following URL in the browser.

  • http://localhost:8000/

  • http://localhost:8000/hello-/

The output will be

For listening8000//localhost:8000/The URL is/ Therefore, the output of logger is the current time and'/Similarly, this is also true for the url'/hello-/。',