[英]API queries in Nodejs/Express
为什么不起作用?
/search?name=:name
但这有效:
/search/name=:name
如何使前者与之配合使用?
(问号)
参见https://expressjs.com/en/guide/routing.html
查询字符串不是路由路径的一部分。
如果要使用查询字符串,请使用req.query
:
app.get('/search', function (req, res) {
console.log(req.query);
});
您想要的是两个路由参数:
app.get('/foo/:bar', (req, res) => { //GET /foo/helloworld
console.log(req.params.bar); //helloworld
//...
});
或GET参数:
app.get('/foo', (req, res) => { //GET /foo?bar=helloworld
console.log(req.query.bar); //helloworld
//...
});
您现在正在做的是将它们混合在一起,这是行不通的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.