Express框架-middleware 中间件

Middleware is a function with access to the request object (req), the response object (res), and the next middleware in the application’s request-response cycle, commonly denoted by a variable named next.

中间件是函数,在应用程序请求响应周期内可以访问request请求对象和response对象并且执行下一个中间件

中间件可以做那些事

  • Execute any code. 执行任何代码
  • Make changes to the request and the response objects. 修改请求和响应对象
  • End the request-response cycle. 结束请求响应周期
  • Call the next middleware in the stack. 回调下一个中间件

简单的中间件

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

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

app.listen(3000);  

Here is a simple example of middleware called “myLogger”. All it does is print “LOGGED”, when a request to the app passes through it. The middleware is assigned to a variable named myLogger.

下面是中间件示例叫myLogger,当一个请求到达时,它总是打印LOGGED,并把中间件赋给名称为myLogger的变量

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

当调用next方法时,将会执行下一个函数,next方法不是Node 或者 Express 的API,但是可以通过第三个参数传入中间件,可以在任何地方回调它

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

app.use()可以载入中间件函数,比如在root路由中载入中间件

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);  

Now every time the app receives a request, it will print “LOGGED” to the terminal.

现在每次app接收到请求时都会在终端打印LOGGED

The order of middleware loading is important: middleware loaded first is executed first.

中间件载入顺序是非常重要的,首先要载入才能执行

If myLogger was loaded after the route to the root path, the request would have never reached it, and the app wouldn’t print “LOGGED”, because the route handler of the root path would have terminated the request-response cycle.

如果myLogger在root路由之后在载入,App永远都不会执行这个中间件,并且不会打印LOGGED,因为中间件不在请求-响应周期内

The middleware myLogger simply printed a message and passed on the request to the next middleware in the stack by calling next().

中间件myLogger 只是简单打印一个消息并且使用next()回调下一个中间件

The next example adds a property called requestTime to the request object. So, we’ll call this middleware “requestTime”.

下一个示例添加一个属性称为requestTime

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

......

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!<br>';
  responseText += '<small>Requested at: ' + req.requestTime + '</small>';
  res.send(responseText);
});

app.listen(3000);  
你的欣赏是我最大的动力

Yanxiong Huang

My name is Yanxiong Huang. graduated from Nanyang middle school.Love Linux,familiar with Node.js,Docker,Serverless... and more Web technology.Contact Me:QQ 31356617;Email:huangaynxiong2013@gmail.com

guangzhou,china http://www.myfreax.com

乐在分享