I've been working on an endpoint but I want it to work like bot/:id/vote
How would I get the ID to pass into that router so it goes like https://example.com/bot/512333785338216465/vote
const vote = require("../routers/voting");
app.use("/bot/:id/vote", vote);
You need to use the mergeParams(default: false)
option when creates a new router object.
Preserve the
req.params
values from the parent router. If the parent and the child have conflicting param names, the child’s value take precedence.
So the working code is:
const express = require('express');
const app = express();
const { Router } = express;
const vote = Router({ mergeParams: true });
vote.get('/', (req, res) => {
console.log(req.params); //=> { id: '512333785338216465' }
res.sendStatus(200);
});
app.use('/bot/:id/vote', vote);
app.listen(3000, () => console.log('Server started at http://localhost:3000'));
package version: "express": "^4.17.1",