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.
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 {}.
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.
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.
You need to reference req.query
req.params
is for params embedded in the URL path.
You can also use
req.param('id')
It will fetch path,body and query params for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With