Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell Express NOT to parse the query string of the request?

I know that Express checks for query string (e.g. ?a=1&b=2) and parses it if present by default. And that req.query is the object which contains the key/value pairs. Is there a way of disabling this behavior and ignore the qs completely?
I need this because I parse the query string client side, and since I receive a huge amount of requests and the qs are pretty long I don't want to waste server resources each time parsing the query string (which means that Express would need to decode URI components in the string, split the string, do a for loop for each key value pair, do another split for each pair, create a new object etc. which is very expensive). Is this possible?

like image 565
Core_dumped Avatar asked Jun 09 '15 06:06

Core_dumped


1 Answers

You can configure the query parser (have a look to the doc):

app.disable('query parser')

Place it after the express initialization and before the router.

You could also pass an empty function to the query parser just in case you need some kind of fine tuning in the future:

app.set('query parser', function(qs, options) { 
 // qs is a query string, process it here
});
like image 92
javierfdezg Avatar answered Oct 05 '22 23:10

javierfdezg