我想访问app.get(index.js
)中app.use(idCtrl.js
)中定义的param。下面我有一个我的代码的简化版本。该路线有效并将我发送给控制器以获取/1234
等网址,但我无法从:id
访问req.params
。
var express = require('express');
var app = express();
var idCtrl = require('./idCtrl');
...
app.use('/:id([0-9]{4})', idCtrl);
var express = require('express');
var router = express.Router();
...
router.get('/', function(req, res){
// I wanna access :id from the app.use in index.js
});
...
答案 0 :(得分:0)
处理这种情况的正确方法是
首先,设置你的参数
router.param('id',function(req,res,next,id)
{
//d owhatever you want with your param here
req.paramID = id; // save it in your req
next(); // Continue to your get('/:id')
};
然后,使用参数
设置路线router.get('/:id', function(req, res){
console.log(req.paramID); // You got your param here
});