Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I iterate over the query string parameters using ExpressJS?

I know that if I pass a URL like the below:

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

That I can then in ExpressJS use the below to extract the 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 paramters and handle them without having to predefine them in my query. I can't seem to find anything in the Express API, etc that seems to work (probably looking right over it).

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

Any comments / suggestions would be appreciated!

Thanks,

S

like image 452
scarpacci Avatar asked Jun 29 '13 21:06

scarpacci


1 Answers

In JavaScript you can look over 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]);
    }
}

The hasOwnProperty check is to make sure that the property is not from the objects prototype chain.

like image 144
adamse Avatar answered Oct 11 '22 12:10

adamse