【发布时间】:2018-10-28 10:32:20
【问题描述】:
我有 nodejs express 服务器,包含一个通用中间件。在那里,我想获得路线名称。例如:
app.use(function (req, res, next) {
//using some npm pkg allowing me to run function after res.status was sent. This code runs at the very end of the request (after the response)
onHeaders(res, function () {
//console #1
console.log(req.route.path); })
next();
});
app.use((req, res, next) => {
//This code will run before the handler of the api, at the beginning of the request
//console #2
console.log(req.route.path);
});
app.post('/chnagePass/:id', handeler...) //etc
因此,在请求结束时发生的控制台 #1 打印 /chnagePass/:id。 控制台 #2 不起作用,此时 req.route 未定义。
但我想在控制台 #2 上获取此路径名(在我的情况下,通过路由的名称,我可以决定某些配置的超时时间)。
此时我如何获取路线名称(例如 /chnagePass/:id)?有可能吗?
非常感谢!
【问题讨论】:
标签: javascript node.js express routes