简体   繁体   English

无法在 express JS 中获取参数

[英]Unable to get param in express JS

Below is my code snippet.下面是我的代码片段。 When I make an HTTP request to http://localhost:3000/address it works, but when I make a request to http://localhost:3000/address/3 it doesn't work.当我向http://localhost:3000/address发出 HTTP 请求时它可以工作,但是当我向http://localhost:3000/address/3发出请求时它不起作用。 What I am doing wrong?我做错了什么?

router.get('/:route(address(es)?|documents?)/:id?', async (req, res) => {
    const {route, id} = req.params;
    console.log(route, id)
});

Change the brackets in address(es) to square ones in order to avoid the 404 error, so that the route looks like this: '/:route(address[es]?|documents?)/:id?'address(es)中的括号更改为方括号以避免 404 错误,使路由看起来像这样: '/:route(address[es]?|documents?)/:id?'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM