Joel Divekar Joel Divekar - 2 months ago 9
Node.js Question

Getting request query parameters count

My API server is accepting GET request and parsing the parameters. But I am not able to get count of parameters passed.

var express = require('express'),
https = require('https');

var app = express();

app.get('/api', function(req, res) {
console.log(req.query);
console.log(req.query.name);
};


When request is received with the parameters, it displays the data as follows

{ id: '123', name: 'joel', age: '45' }
joel


But if I try to get the count of parameters passed like

console.log(req.query).length;


it fails. Kindly help me get the count of parameters passed. Thanks.

Answer

You can check this..this code worked..

app.get('/api', function(req, res) {
    console.log(req.query);
    console.log(Object.keys(req.query).length);
});

output:

{ id: '123', name: 'joel', age: '45' }

3