Joe
Joe

Reputation: 13101

Node - ExpressJS - How to see all parameters and values from URL request?

I have Node / Express code:

var express = require('express');
var app = express();
var port = process.env.PORT || 8080;

app.listen(port);
console.log('Server started! At http://localhost:' + port);

app.get('/api/users', function(req, res) {

    var user_id = req.param('id');
    console.log(user_id);

    res.send(user_id );
}); 

When I got to URL in browser (or curl) to http://localhost:8080/api/users?id=4 - output is 4.

How to see any parameter and value that user might enter? e.g. if someone enters: http://localhost:8080/api/users?par1=123&par2=321 it should return par1 = 123 and par2 = 321 etc.

Upvotes: 2

Views: 73

Answers (1)

Eugene Tsakh
Eugene Tsakh

Reputation: 2879

You can use query param of request:

console.log(req.query);

It will contain an object

{
  par1: '123',
  par2: '321'
}

Upvotes: 4

Related Questions