简体   繁体   English

next()在Node.js中如何工作?

[英]How does next() work in Node.js?

I recently read a blog in Nodejitsu and I am wondering how this piece of code works. 我最近在Nodejitsu上阅读了一个博客,我想知道这段代码是如何工作的。

var fs = require('fs'),
http = require('http'),
httpProxy = require('../lib/node-http-proxy');


module.exports = function (logging) {
  // Code here is run when the middleware is initially loaded.
  var logFile = fs.createWriteStream('./requests.log');

  return function (request, response, next) {
    // Code here is run on each request.
    if (logging) {
      logFile.write(JSON.stringify(request.headers, true, 2));
    }
    next();
  }
}

And the explanation given for this piece of code is: 这段代码给出的解释是:

This middleware is for very simple logging - it will write the headers of each request to a log file. 该中间件用于非常简单的日志记录-它将每个请求的标头写入日志文件。

the above module exported can be used as, 上面导出的模块可以用作

httpProxy.createServer(
  require('./example-middleware')(true),
  8000, 'localhost'
).listen(9000)

How is the code in the above method with next() invoked in every request? 在每个请求中如何调用带有next()的上述方法中的代码? The usage is pretty simple: require the above module and it gets invoked every time. 用法非常简单:需要上述模块,并且每次都会调用它。

I'll simplify the actual process but the gist of it: 我将简化实际过程,但要点是:

When a request comes in, node passes the request and response object to the first middleware in the middleware stack. 当请求进入时,节点将请求和响应对象传递给中间件堆栈中的第一个中间件。 If that middleware sends a response or closes the connection in any way, then subsequent middleware are not called. 如果该中间件以任何方式发送响应或关闭连接,则不会调用后续的中间件。 Otherwise, that middleware has to tell node it's finished doing it's job to keep on moving through the middleware stack, so you call next() within your middleware to tell it to continue processing middleware. 否则,该中间件必须告知节点它已经完成了继续在中间件堆栈中移动的工作,因此您在中间件中调用next()来告诉它继续处理中间件。

Okay, so this is a pretty common thing. 好的,这是很常见的事情。 What this module contains is a single function, specified by this line, where we set module.exports to a function, module.exports = function (logging) { . 此模块包含的内容是此行指定的单个函数,其中我们将module.exports设置为一个模块, module.exports = function (logging) { The function returned by the module (and therefore returned by require() ) returns another function, which is the middleware for the HTTP proxy (this middleware allows you to transforms the request). 模块返回的函数(并因此由require()返回)返回另一个函数,该函数是HTTP代理的中间件(该中间件允许您转换请求)。 This middleware function gets called for every HTTP request made to the server. 对于服务器发出的每个HTTP请求,都会调用此中间件功能。 Quickredfox's answer provides a fairly good explanation of middlewares. Quickredfox的答案很好地解释了中间件。

So the require('./example-middleware')(true) actually calls the function assigned to module.exports , but does not call the function inside that, which is returned immediately and passed as a middleware into the httpProxy.createServer function. 因此require('./example-middleware')(true)实际上调用分配给module.exports的函数,但不调用其中的函数,该函数立即返回并作为中间件传递给httpProxy.createServer函数。 This is a good way to set up some options for your middleware using closures. 这是使用闭包为中间件设置一些选项的好方法。 If you have any more questions, feel free to comment. 如果您还有其他问题,请随时发表评论。 :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM