How to say Express NOT to parse a query string of a query?

I know that Express checks the query string (for example,? A = 1 & b = 2) and parses it if it is present by default. And this req.query is an object that contains key / value pairs. Is there a way to disable this behavior and completely ignore qs?
I need this because I am analyzing the client query string, and since I get a huge number of queries, and qs are quite long, I don’t want fragmentary server resources when parsing the query string (this means that Express will need to decode the URI components in the string, split a line, make a for loop for each pair of key values, make another split for each pair, create a new object, etc. which is very expensive). Is it possible?

+6
source share
1 answer

You can set up a query parser (see doc ):

app.disable('query parser') 

Place it after express initialization and in front of the router.

You can also pass an empty function to the query parser if in the future you need some tweaking:

 app.set('query parser', function(qs, options) { // qs is a query string, process it here }); 
+4
source

Source: https://habr.com/ru/post/988761/


All Articles