Reputation: 260
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);
middleware looks like this:
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;
});
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
.
Hence, the question, how can I access c_name
from my middleware?
Upvotes: 4
Views: 2466
Reputation: 15639
You will need to add mergeParams
while initializing your router
your which will preserve the req.params
value
var router = express.Router({mergeParams: true});
Hope this helps!
Upvotes: 16