node.js 很难理解 express.js 中的“next/next()”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8710669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:57:41  来源:igfitidea点击:

Having a hard time trying to understand 'next/next()' in express.js

node.jsexpress

提问by alexchenco

This is an example of it:

这是一个例子:

// Configuration
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

(etc.)

(等等。)

app.get('/memo', function(req, res) {
  console.log("index");
  Memo.find({}, function(err, data) {
    if(err) return next(err);
    res.render('index', { memos: data });
  });
});

And here is another one:

这是另一个:

app.get('/memo/list', function(req, res, next) {
  console.log("get memos");
  Memo.find({}, function(err, data) {
    if(err) return next(err);
    res.json(data);
  });
});

Taken from a simple memo pad built on node

取自在节点上构建简单备忘录

These are the questions that are puzzling me:

这些是让我困惑的问题:

  1. What does exactly next/next();do? What would happen if it is not present?
  2. Why is the second part taking nextas a parameter and the first isn't?
  1. 具体有什么作用next/next();?如果它不存在会发生什么?
  2. 为什么第二部分next作为参数而第一部分不是?

EDIT:

编辑:

回答by alessioalex

Express uses middleware functions that have callbacks (functions that get called when an action is completed), and next has that purpose (it's a callback that triggers the next middleware in the Express stack). All Express middleware (that is Connect compatible) have 3 params: request, response, next (which is optional).

Express 使用具有回调的中间件函数(在操作完成时调用的函数),并且 next 具有该目的(它是触发 Express 堆栈中的下一个中间件的回调)。所有 Express 中间件(与 Connect 兼容)都有 3 个参数:请求、响应、下一个(可选)。

For example the static middlware serves static files, the csrf middleware checks for a param when receiving POST requests and the router middleware that handles the routes (what you've pasted above is part of that).

例如,静态中间件提供静态文件,csrf 中间件在接收 POST 请求时检查参数以及处理路由的路由器中间件(您上面粘贴的内容是其中的一部分)。

Each middleware can complete it's task and call the nextmiddleware in the queue if some conditions are met (for example the static middleware won't call a next middleware, because it will take care on itself to serve the files, so that the router won't get called).

每个中间件都可以完成它的任务并next在满足某些条件时调用队列中的中间件(例如静态中间件不会调用下一个中间件,因为它会照顾自己为文件提供服务,因此路由器不会t 被调用)。

In the router you don't usually call nextbecause it tends to be the last middleware executed (unless you want something like benchmarking).

在路由器中,您通常不会调用,next因为它往往是最后执行的中间件(除非您想要基准测试之类的东西)。

If you wanted to create a middleware that denies access to all users that aren't logged in, you would have to call next()only if the user is logged in (so that the next middleware is called, the router in this case and the user can access the page they're looking for), else you would probably redirect them to the login page.

如果您想创建一个拒绝所有未登录用户访问的中间件,则必须next()仅在用户登录时调用(以便调用下一个中间件,在本例中为路由器,用户可以访问他们正在寻找的页面),否则您可能会将他们重定向到登录页面。

nexttakes either no parameters at all or an error as a parameter.

next要么完全不接受参数,要么将错误作为参数。

Edit: based on your configuration the router is before the static middleware, so if you want files to be served you need to declare a wildcard route that calls next() when the route isn't matched:

编辑:根据您的配置,路由器位于静态中间件之前,因此如果您希望提供文件,您需要声明一个通配符路由,当路由不匹配时调用 next() :

app.get('*', function (req, res, next) {
  // no route is matched
  // so call next() to pass to the static middleware
  next();
});

Note: I don't recommend you put the static file server after the router, I suggest you put it before so you can define your own custom 404 routes.

注意:我不建议你把静态文件服务器放在路由器之后,我建议你放在之前,这样你就可以定义自己的自定义404路由。