Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request Parameter in express js

I am using express.js and I am trying to fetch the request parameter using the following code.

app.configure(function () {
  app.use(express.static(__dirname + '/public'));
});

app.get('/', function (req, res) {
  console.log(req.params[0]);
  console.log(req.params.id);
  res.render('/public/index.html');
});

My url looks like this.

http://localhost:8080/?id=34.

I am not getting the request parameter. I tried all possibilities.

like image 318
tk120404 Avatar asked Dec 27 '12 11:12

tk120404


People also ask

What is request params in Express?

The req. params property is an object that contains the properties which are mapped to the named route "parameters". For example, if you have a route as /api/:name, then the "name" property is available as req.params.name. The default value of this object is {}.

How do I get req params in Express?

Your query parameters can be retrieved from the query object on the request object sent to your route. It is in the form of an object in which you can directly access the query parameters you care about. In this case Express handles all of the URL parsing for you and exposes the retrieved parameters as this object.

What is request in Express js?

Request object Express. js is a request & response objects parameters of the callback function and are used for the Express applications. The request object represents the HTTP request and contains properties for the request query string, parameters, body, HTTP headers, etc.


2 Answers

You need to reference req.query

req.params is for params embedded in the URL path.

like image 181
Dominic Barnes Avatar answered Oct 09 '22 02:10

Dominic Barnes


You can also use

req.param('id')

It will fetch path,body and query params for you.

like image 23
Abhijeet Avatar answered Oct 09 '22 00:10

Abhijeet