很难理解 express.js 中的“next/next()" [英] Having a hard time trying to understand 'next/next()' in express.js

查看:40
本文介绍了很难理解 express.js 中的“next/next()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个例子:

// 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'));
});

(等)

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

这是另一个:

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);
  });
});

取自 构建在节点上的简单记事本

这些是让我困惑的问题:

These are the questions that are puzzling me:

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

推荐答案

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

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).

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

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).

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

Each middleware can complete it's task and call the next middleware 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 因为它往往是最后执行的中间件(除非你想要像基准测试这样的东西).

In the router you don't usually call next because 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 要么不接受任何参数,要么将错误作为参数.

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

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

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:

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

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

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.

这篇关于很难理解 express.js 中的“next/next()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆