简体   繁体   中英

Node.js Express middleware: app.param vs app.use

在Express中间件内部的调用链中,是否总是在app.use之前调用app.param方法?

I tested with this program changing the order of app.use vs app.param with express 4.10.2. The param always runs first, which makes sense because the route handler expects to be able to do req.params.foo and in order for that to work the param handlers need to have run.

var express = require('express');
var app = express();

app.use("/:file", function (req, res) {
  console.log("@bug route", req.params.file);
  res.send();
});

app.param("file", function (req, res, next, val) {
  console.log("@bug param", val);
  next();
});



app.listen(3003);

Run this and test with curl localhost:3003/foo and you get the output:

@bug param foo
@bug route foo

您可以通过日志记录对其进行测试,但是我可以肯定地说,在4.0中,一切都按照设置应用程序时声明的顺序调用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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