Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express js 4x :req.params returns empty object

Trying to get URl parameters in express js,but got empty object.

var password= require('./routes/password');
app.use('/reset/:token',password);

password.js

router.get('/', function(req, res, next) {
    console.log(req.params);
    res.send(req.params);
});

console.log(req.params) output is {}

Access url :http://localhost:3000/reset/CiVv6U9HUPlES3i0eUsNwK9zb7xVZpfHsQNuzMNWqLlGA4NJKoagwbcyiUZ8

like image 526
Jabaa Avatar asked Apr 07 '17 04:04

Jabaa


1 Answers

By default, nested routers do not get passed any parameters that are used in mountpaths from their parent routers.

In your case, app is the parent router, which uses /reset/:token as a mountpath, and router is the nested router.

If you want router to be able to access req.params.token, create it as follows:

let router = express.Router({ mergeParams : true });

Documented here.

like image 150
robertklep Avatar answered Sep 19 '22 13:09

robertklep