Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't access req.params of app.use() inside middleware

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?

like image 917
properchels Avatar asked Sep 15 '25 16:09

properchels


1 Answers

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!

like image 121
David R Avatar answered Sep 17 '25 06:09

David R