很难在express.js中了解'next / next()' [英] Having a hard time trying to understand 'next/next()' in express.js

查看:108
本文介绍了很难在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使用具有回调的函数当一个动作完成时被调用),接下来就有这个目的(这是一个回调,触发Express堆栈中的下一个中间件)。所有Express中间件(即Connect兼容)有3个参数:request,response,next(这是可选的)。

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

例如,静态middlware提供静态文件,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).

每个中间件都可以完成它的任务,并在满足某些条件的情况下调用队列中的下一个中间件(例如,静态中间件将不会调用下一个中间件,因为它将自行管理)文件,以便路由器不会被调用)。

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天全站免登陆