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? The usage is pretty simple: require the above module and it gets invoked every time.
在每个请求中调用next()的上述方法中的代码如何?用法很简单:需要上面的模块,每次调用它。
2 个解决方案
#1
2
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()以告诉它继续处理中间件。
#2
1
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) {
. 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). This middleware function gets called for every HTTP request made to the server. Quickredfox's answer provides a fairly good explanation of middlewares.
好的,所以这是很常见的事情。该模块包含的是由该行指定的单个函数,其中我们将module.exports设置为函数,module.exports = function(logging){。模块返回的函数(因此由require()返回)返回另一个函数,该函数是HTTP代理的中间件(此中间件允许您转换请求)。为服务器发出的每个HTTP请求都会调用此中间件函数。 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. 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
因此require('./ example-middleware')(true)实际上调用了分配给module.exports的函数,但是没有调用其中的函数,函数立即返回并作为中间件传递给httpProxy.createServer函数。这是使用闭包为中间件设置一些选项的好方法。如果您还有其他问题,请随时发表评论。 :d
#1
2
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()以告诉它继续处理中间件。
#2
1
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) {
. 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). This middleware function gets called for every HTTP request made to the server. Quickredfox's answer provides a fairly good explanation of middlewares.
好的,所以这是很常见的事情。该模块包含的是由该行指定的单个函数,其中我们将module.exports设置为函数,module.exports = function(logging){。模块返回的函数(因此由require()返回)返回另一个函数,该函数是HTTP代理的中间件(此中间件允许您转换请求)。为服务器发出的每个HTTP请求都会调用此中间件函数。 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. 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
因此require('./ example-middleware')(true)实际上调用了分配给module.exports的函数,但是没有调用其中的函数,函数立即返回并作为中间件传递给httpProxy.createServer函数。这是使用闭包为中间件设置一些选项的好方法。如果您还有其他问题,请随时发表评论。 :d