[英]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.