Reputation: 41
I have the following code:
var express = require('express');
var app = express();
app.use(express.bodyParser());
app.use(express.cookieParser());
var port = Number(process.env.PORT || 5000);
app.get('/test/:id', function(req, res) {
res.send(req.params);
});
app.listen(port);
console.log("Listening on port " + port);
When I hit http://localhost:5000/test/12345
in my browser, I see:
[]
But I'm expecting to see the populated req.params object with id: 12345
. Any idea what I'm doing wrong?
Thanks.
Upvotes: 4
Views: 212
Reputation: 26819
The reason why you are not getting anything using res.send(req.params)
is because req.params
is an instance of Array
and default mechanism tries to covet an array into string which is giving you []
.
What you could do to see all params is as follows:
app.get('/test/:id', function(req, res) {
res.send(require('util').inspect(req.params));
});
That will give you output like (using this URL http://localhost:5000/test/12345
):
[ id: '12345' ]
That approach will also work with routes having more parameters e.g. the following code snippet and that request http://localhost:5000/test/name/234
app.get('/test/:name/:id', function(req, res) {
res.send(require('util').inspect(req.params));
});
will give you:
[ name: 'name', id: '234' ]
Alternatively, if you would like to get JSON formatted output of a JavaScript object, you can use res.json
. But note that json
will also generate []
if you perform res.json(req.params)
as it will render req.params
array in a standard way.
I hope that will help.
Note that as of 9th April 2014 (Express version 4.0.0) req.params
is an object instead of an array so you should not have similar issues with v4.0.0 and your code should work as expected.
app.get('/test/:id', function(req, res) {
res.send(req.params);
});
Upvotes: 1
Reputation: 4064
The response object will be named after your id
. If you check it in your browser's traffic you can see a file called 12345
. Though you can check that your server gets the id by logging it on the server side.
app.get('/test/:id', function(req, res) {
console.log(req.params);
res.send(req.params);
});
If you want to pass variables to a view you might want to use the res.render(...)
function where you can pass in arguments.
To get the 123451
send the res.send(req.params.id);
.
Upvotes: 1
Reputation: 6668
Checked and saw that req.params is actually an associative array, which is almost similar to object but not quite. Not sure why express defined it as such. The only benefit it gets is the added length property for an array. And res.send will try to JSON.stringify your output, which in turn will see that it's an array and only take care of indexes which are numeric. Hence the empty [] as return value.
You can validate this by doing -
var myParams = {};
Object.keys(req.params).forEach(function(key){
myParams[key] = req.params[key];
});
res.send(myParams);
You can also see that JSON.stringify only cares about numeric index by defining your route as such -
app.get('/test/:0', function(req, res) {
res.send(req.params);
});
Upvotes: 3
Reputation: 3414
The problem isn't your middleware. Neither bodyParser nor cookieParser provide the named url parameter parsing. That being said, your code looks correct to me. Are you absolutely certain you restarted your node server after making the code change?
Upvotes: 0