28

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
  • I have not tried this, but you should try /stuff?a=:a&b=:b&c=:c Commented Feb 3, 2013 at 5:29

2 Answers 2

39

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.

Sign up to request clarification or add additional context in comments.

1 Comment

To add to this, Express explicitly states "Query strings are not considered when performing these matches" - expressjs.com/api.html
34

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.