是`app.get(“/”,func1,func2);`与`app.get(“/”,func1)相同; app.get(“/”,func2);`?

时间:2014-01-26 07:22:10

标签: node.js express

我正在尝试在express.js中为我的网络应用程序创建一个路由系统,我需要知道我是否需要使用app.get / post / put / delete.apply以编程方式设置多个功能一条路线。

所以

app.get("/", function(req, res, next) {
    code();
    next();
});

app.get("/", function(req, res, next) {
    finish();
});

相同
app.get("/", function(req, res, next) {
    code();
    next();
}, function(req, res, next) {
    finish();
});

1 个答案:

答案 0 :(得分:1)

是的,它几乎一样。

如果可能,您可以使用app.use将设置功能“提升”为适当的中间件:

app.use(function(req, res, next) {
  code();
  next();
});

但是只有在需要为所有路线运行时,这才有用。

或者,如果您想将其重复用于某些路由,您可以执行以下操作:

var MyMiddleware = function(req, res, next) {
  code();
  next();
});

app.get("/", MyMiddleware, function(req, res) {
  finish();
});
相关问题