turtle
turtle

Reputation: 8073

Express route parsing

I'm trying to create a route for the following URL:

http://localhost:5000/api/querystring?parameter1=value1&parameter2=value2

My route looks like this:

app.get('/api/:querystring/:parameter1?/:parameter2?', function(req, res) {

   // do stuff
})

How can I create a route that matches the given URL?

Upvotes: 0

Views: 263

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191749

You can't include query string parts in the route ... you will have to leave them off.

app.get('/api/querystring' ...

Then in the callback you can look at req.query to see the parameters. If you were to compare to the query, the order of the query string parameters would matter. It shouldn't.

If you want /api/querystring?parameter1=foo and /api/querystring?parameter2=bar to use different routes, you will have to handle this by calling separate functions within the app.get route callback above.

Upvotes: 1

Related Questions