简体   繁体   English

Nodejs / Express中的API查询

[英]API queries in Nodejs/Express

why doesn't this work? 为什么不起作用?

/search?name=:name

but this works : 但这有效:

/search/name=:name

how to make the former work with the ? 如何使前者与之配合使用? (Question mark) (问号)

See https://expressjs.com/en/guide/routing.html 参见https://expressjs.com/en/guide/routing.html

Query strings are not part of the route path. 查询字符串不是路由路径的一部分。

If you want to use the query string, use req.query : 如果要使用查询字符串,请使用req.query

app.get('/search', function (req, res) {
  console.log(req.query);
});

What you want is either route parameters: 您想要的是两个路由参数:

app.get('/foo/:bar', (req, res) => { //GET /foo/helloworld
    console.log(req.params.bar);     //helloworld
    //...
});

Or GET parameters: 或GET参数:

app.get('/foo', (req, res) => {  //GET /foo?bar=helloworld
    console.log(req.query.bar);  //helloworld
    //...
});

What you are doing right now is mixing them up, which doesn't work. 您现在正在做的是将它们混合在一起,这是行不通的。

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

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