How to parse variables in querystring using Express?

后端 未结 2 582
谎友^
谎友^ 2021-01-01 11:10

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 th

2条回答
  •  攒了一身酷
    2021-01-01 11:49

    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.

提交回复
热议问题