Skip to content

Express route param is undefined when I use express.Router() #4784

@ItsRealmy

Description

@ItsRealmy

So imagine if the current file I'm in is an express.Router() for the endpoint /post.

I tried to require a different router within the /post router called /:id (which is a url param), sorta like this: router.use("/:id", require("./idRouter"));. So, if I go to idRouter.js (which' route is /post/:id) and write something like this:

router.get("/like", (req, res) => {
  console.log(req.params.id)
  res.send({status:"SUCCESS"});
});

And then if I go to the endpoint in a browser (for example /post/12345/like) it does respond with {"status":"SUCCESS"} but req.params.id is undefined, so I can't use it. Is this a bug or is this deliberate (for some reason)?

Note: req.params is an empty object

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions