简体   繁体   English

谁在Express中间件中提供next()函数?

[英]Who provides the next() function in Express middleware?

I'm confused about how next() works in Node.js and Express middleware. 我对next()在Node.js和Express中间件中的工作方式感到困惑。

There have been some other questions about middleware works, for example here , but I'm looking for a different answer. 关于中间件的工作还有其他问题, 例如在这里 ,但我正在寻找一个不同的答案。

The main question bugging me is, who is providing the next() function? 困扰我的主要问题是,谁在提供next()函数?

For example, in my standard generated express app, I'm given this code: 例如,在我的标准生成的express应用程序中,给出了以下代码:

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

But who calls this function and what is the nature of the next() that it provides? 但是谁调用了此函数,它提供的next()的本质是什么? How do I know what the next() will look like? 我怎么知道next()会是什么样子? I have no idea what next() does. 我不知道next()做什么。

An Express application is essentially a series of middleware calls. Express应用程序本质上是一系列中间件调用。

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. 中间件是可以访问请求对象(req),响应对象(res)和Express应用程序的请求-响应周期中的下一个中间件的函数,通常用一个名为next的变量表示。

As written above, Expressjs provides the next callback function. 如上所述,Expressjs提供了下一个回调函数。 Next is just a way of calling the next middleware in the flow. 下一步只是调用流程中的下一个中间件的一种方法。

For many examples see Express's Using middleware 有关许多示例,请参见Express的使用中间件。

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

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