How to parse variables in querystring using Express?

本秂侑毒 提交于 2019-12-04 15:35:50

问题


I have a request being sent to the server:

"/stuff?a=a&b=b&c=c"

Using express, how do I get these values?

I have tried the following...

app.get( "/stuff?:a&:b&:c", function( req, res ){});

...however it does not seem to recognize the route.

Thanks (in advance) for your help.


回答1:


It's not a good idea to use a query string inside a route.

In Express logic you need create a route for "/stuff". The query string will be available in req.query.




回答2:


You can declare your route directly with /stuff, then query parameters are accessible through req.query, which is a JSON object. Here's your example:

app.get("/stuff", function(req, res) {
    var a = req.query.a;
    ...
});

In your case, req.query is equal to:

{ a: 'a',
  b: 'b',
  c: 'c' }

In Express' documentation (either 4.x and 3.x) you can find additional examples: Express - req.query.



来源:https://stackoverflow.com/questions/14669669/how-to-parse-variables-in-querystring-using-express

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!