Currently I have a service that exposes these two endpoints
app.get('/test/', (req, res) => {
res.send('hello world');
});
app.get('/test/:id', (req, res) => {
res.send('hello world');
});
I have a middleware that logs all request to these endpoints. If the second endpoint is hit, I want to log /test/:id
and not /test/actualId
.
How can I extract the route from the req
object in this way?
Use request.route.path
which will output the path string you provided:
app.get('/test/', (req, res) => {
console.log(req.route.path)
// -> /test/
res.send('hello world');
});
app.get('/test/:id', (req, res) => {
console.log(req.route.path)
// -> /test/:id
res.send('hello world');
});
This will help you.
app.get('/test/:id', (req, res) => {
let actualId = req.query.id;
let reqPath = req.originalUrl || req.path
res.send('hello world :', actualId);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.