Write middleware for use with Express applications

overview

A middleware function has access to the request object  ( req), the response object  ( res), and the next middleware function in the application's request/response cycle. The next middleware function is usually  next represented by a variable named .

Middleware functions can perform the following tasks:

  • execute any code.
  • Make changes to the request and response objects.
  • End the request/response loop.
  • The next middleware in the call stack.

If the current middleware function does not end the request/response loop, it must be called  next()to pass control to the next middleware function. Otherwise, the request will remain pending.

The following example shows the elements of a middleware function call:

The HTTP method to which the middleware function applies.

The path (route) to which the middleware function applies.

middleware function.

The callback argument of the middleware function, called "next" by convention.

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

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

Here is a simple example of a "Hello World" Express application for which two middleware functions are defined:


var express = require('express');
var app = express();

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

app.listen(3000);

to develop

Below is a simple example of a middleware function called "myLogger". This function only displays "LOGGED" when the application's request passes through it. The middleware function is assigned to  myLoggera variable named .


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

Note the above  next() call to . When this function is called, the next middleware function in the application will be called. next() Functions are not part of the Node.js or Express API, but are a third argument passed to middleware functions. next() The function can be named anything, but by convention it is always named "next". To avoid confusion, always use this convention.

To load a middleware function, call  app.use() and specify the middleware function. For example, the following code loads middleware functions before routes for the root path (/)  myLogger .


var express = require('express');
var app = express();

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

app.use(myLogger);

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

app.listen(3000);

Every time the application receives a request, it displays the message "LOGGED" on the terminal.

The order in which middleware is loaded is important: the middleware function loaded first is also executed first.

If loaded after the root path's route  myLogger, the request never reaches the function and the application will not display "LOGGED" because the root path's route handler terminates the request/response loop.

The middleware function  myLogger just displays the message and then  next() passes the request to the next middleware function in the stack by calling the function.

The next example  requestTime adds a property named to the request object. Let's name this middleware function "requestTime".


var requestTime = function (req, res, next) {
  req.requestTime = Date.now();
  next();
};

Now, the application uses  requestTime middleware functions. reqAdditionally, callback functions for root route routes use properties added to (request object) by middleware functions  .


var express = require('express');
var app = express();

var requestTime = function (req, res, next) {
  req.requestTime = Date.now();
  next();
};

app.use(requestTime);

app.get('/', function (req, res) {
  var responseText = 'Hello World!';
  responseText += 'Requested at: ' + req.requestTime + '';
  res.send(responseText);
});

app.listen(3000);

When you make a request to the application root, this application is currently displaying the request's timestamp in the browser.

The possibilities for middleware functions are endless because you have access to the request object, response object, the next middleware function in the stack, and the entire Node.js API.

Guess you like

Origin blog.csdn.net/zdwzzu2006/article/details/132645555