How to get number of request query parameters in express.js? How to get number of request query parameters in express.js? express express

How to get number of request query parameters in express.js?


To get all query parameter:

Object.keys(req.query)

To get number of all params:

Object.keys(req.query).length

Then you can iterate through all parameters:

for(p in req.query) {  //... do something}

UPD:

surround your request with quotes to make right query

curl -X GET "localhost:9090/mypath?param1=123&param2=321"

without quotes the & in terminal makes the command run in the background.


If you hit /mypath?param1=5&param2=10, then the request.query will yield {param1: 5, param2:10}.

This means that the request.query is a JavaScript object with the key as the name of the param, and value as the value of the param. Now you can do anything with it as you want: Find the length or iterate over it as follows:

for (var key in request.query) {  if (request.query.hasOwnProperty(key)) {    alert(key + " -> " + request.query[key]);  }}

Finding only the length might not work for you that well because you may have param1 and param3, with param2 missing. Iterating will be better IMO.


You want the number of non-undefined params right?It is as simple as this;

var no = 0;for (var key in req.query) {     if(req.query[key]) no++;}