Can I iterate over query string parameters using ExpressJS?

I know that if I pass in the url as below:

http://localhost:3000/customer?companyid=300&customerid=200 

So that I can use below in ExpressJS to extract data:

 res.send(util.format('You are looking for company: %s customer: %s', req.query.companyid, req.query.customerid)); 

But I would like to iterate over the parameters and process them without predetermining them in my query. It seems I can not find anything in the Express API, etc., which seems to work (maybe looking directly at it).

 http://localhost:3000/customer?companyid=300&customerid=200&type=employee 

Any comments / suggestions would be appreciated!

Thanks,

S

+4
source share
2 answers

In JavaScript, you can view the properties of an object using a for loop:

 for (var propName in req.query) { if (req.query.hasOwnProperty(propName)) { console.log(propName, req.query[propName]); } } 

Checking hasOwnProperty is to ensure that the property does not belong to the prototype chain of objects.

+18
source

req.query is just an object, so you can req.query over it just like any object, like

 for (var param in req.query) { console.log(param, req.query[param]); } 
+6
source

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


All Articles