Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
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

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

about 4 years ago · Santiago Trujillo Report

0

You are getting params and query mixed up.

Query approach

Your code should look like this when using query values for the example url: www.example.com?token=123&foo=bar

router.get('/', function(req, res, next) {
    console.log(req.query);
    console.log(req.query.token); // to log value of token
    console.log(req.query.foo);   // to log value of foo
    res.send(req.query);
});

Params approach

Your code should look like this when using params values for the example url: www.example.com/123

router.get('/:token', function(req, res, next) {
    console.log(req.params);
    console.log(req.params.token); // to log value of token
    res.send(req.params);
});
about 4 years ago · Santiago Trujillo Report

0

Instead you can use a middleware to log the path params:

const logger = (req, res, next)=>{
  console.log(req.params)
  res.send(req.params)
  next()//<----very important to call it.
};

app.use(logger); //<----use to apply in the app

router.get('/', (req, res, next)=>res.send('Logged.'));
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!