question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can not get req.params in router middleware

See original GitHub issue
// hasProjectPremission List this
hasProjectPremission = (req, res, next) => {
  console.log(req.params); // {}
  next();
}
router.use(apiBiz.hasProjectPremission);
router.post('/:projId/api/new', apiBiz.createApi); 

When request to the create api, I can’t get req.params in hasProjectPremission, it’s an empty object({})。

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
aPoCoMiLogincommented, Aug 18, 2017

Actually middleware can get parameters from router, but it needs to know which one.

for example:

// handle any additional path with optional catch all
router.use('/:projId(\\d+)(/*)?', (req, res, next) => { 
  console.log(req.params); // { '0': undefined, '1': undefined, projId: '1337' }

  next();
});

router.post('/:projId(\\d+)/api/new', (req, res) => res.sendStatus(200))

But IMO you should use router.param which will act like an middleware, executed right before route is handled.

router.param('projId', (req, res, next, projId) => {
  // executes before route handler

  next();
});

router.post('/:projId/api/new', (req, res) => res.send(req.params.projId)) // 1337
0reactions
hstarorgcommented, Aug 19, 2017

@aPoCoMiLogin Very thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Express middleware access to req.params - node.js
I have middleware that is being called before I initialize my routes ( app.use(middleware) ), and it can't get req.params ., but it...
Read more >
Express Explained with Examples - Installation, Routing ...
Middleware functions are useful pattern that allows developers to reuse code within their applications and even share it with others in the form ......
Read more >
Error handling - Express.js
You must catch errors that occur in asynchronous code invoked by route handlers or middleware and pass them to Express for processing. For...
Read more >
Parse Request middleware - API Dev Tools
use("/users/:id", myMiddleware) ), then req.params will have a property for each path parameter ( id in this example). Thus, req.params changes as it...
Read more >
Express Tutorial Part 4: Routes and controllers - MDN Web Docs
In the case above we complete the request using send() , so the next argument is not used (and we choose not to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found