无法访问中间件内部的app.use()的req.params [英] can't access req.params of app.use() inside middleware
本文介绍了无法访问中间件内部的app.use()的req.params的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为我的应用程序构建动态路由. app.js中的代码如下:
I am trying to build dynamic routes for my application. With the piece of code in app.js looks like this:
app.use('/:c_name/api/',middleware);
中间件如下:
var router = require('express').Router();
router.get('/:m/:c/:a',(req, res, next) => {
console.log(req.params);
var controller = "./fe-ser/leg/fe/cli/"+req.params.c_name+"/main/process/"+req.params.module+"/controllers/"+req.params.controller;
});
现在要解析中间件内部的路由,我需要c_name
的值,但无法使用req.params.c_name
访问它. req.params
仅包含m
,c
和a
.
now to resolve the route inside the middleware, I need the value of c_name
but I can't access it using req.params.c_name
. req.params
only contains m
, c
and a
.
因此,问题是,如何从中间件访问c_name
?
Hence, the question, how can I access c_name
from my middleware?
推荐答案
在初始化router
时,您将需要添加mergeParams
,以保留req.params
值
You will need to add mergeParams
while initializing your router
your which will preserve the req.params
value
var router = express.Router({mergeParams: true});
希望这会有所帮助!
这篇关于无法访问中间件内部的app.use()的req.params的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文