Lee Dennis
Lee Dennis

Reputation: 95

How to deal with an unknown amount of query strings in express

I have a url that accepts an unknown number of query strings. Here is an example of a possible url

/data/mydb.json?arg0="foo"&arg1=15&transaction=function(x, y)

and the whole function body would be the rest of the url but I'll spare you the rest. The requests to /data/:filename will always accept the arguments and function as the query strings. Since req.query doesn't seem to take variables as an argument how can I deal with this? The function taken by the last query string will not always take 2 arguments. Any help is much appreciated.

Upvotes: 1

Views: 528

Answers (1)

chilleren
chilleren

Reputation: 81

The easiest way would be to replace arg0="foo"&arg1=15 with args=["foo",15], then pass them in to the function with .apply.

If you know the names of the args, then you can use req.query to get the params. If you don't know the names and they need to be passed into the function in order, then you might have to use req.originalUrl and parse it manually, since you probably shouldn't trust that req.query will maintain the correct order of the params.

Upvotes: 1

Related Questions