谁在Express中间件中提供next()函数? [英] Who provides the next() function in Express middleware?
问题描述
我对next()
在Node.js和Express中间件中的工作方式感到困惑.
I'm confused about how next()
works in Node.js and Express middleware.
关于中间件作品还有其他问题,例如在这里,但我正在寻找其他答案.
There have been some other questions about middleware works, for example here, but I'm looking for a different answer.
困扰我的主要问题是,谁在提供next()
功能?
例如,在我的标准生成的express
应用中,给出了以下代码:
For example, in my standard generated express
app, I'm given this code:
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
但是谁调用了此函数,它提供的next()
的本质是什么?我怎么知道next()
会是什么样子?我不知道next()会做什么.
But who calls this function and what is the nature of the next()
that it provides? How do I know what the next()
will look like? I have no idea what next() does.
推荐答案
Express应用程序本质上是一系列中间件调用.
An Express application is essentially a series of middleware calls.
中间件是一种可以访问请求对象(req)的功能, 响应对象(res),以及 Express应用程序的请求-响应周期,通常表示为 一个名为next的变量.
Middleware is a function with access to the request object (req), the response object (res), and the next middleware in line in the request-response cycle of an Express application, commonly denoted by a variable named next.
如上所述,Expressjs提供了下一个回调函数.接下来只是调用流程中的下一个中间件的一种方法.
As written above, Expressjs provides the next callback function. Next is just a way of calling the next middleware in the flow.
有关许多示例,请参见Express的使用中间件
For many examples see Express's Using middleware
这篇关于谁在Express中间件中提供next()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!