I am using Nodejs .
Server.js
app.get('/dashboard/:id', routes.dashboard);
Routes / index.js
exports.dashboard = function(req, res){
}
I want to be able to pass the 'id' variable from app.js to the dashboard function . How do I go about doing this ?
Assuming ExpressJS, you shouldn't need to pass it.
For each parameter placeholder (like :id
), req.params
should have a matching property holding the value:
exports.dashboard = function (req, res) {
console.log(req.params.id);
};
Though, this assumes the requested URL matches the route by verb and pattern.
Just ensure that your GET
call from the client is something like this: /dashboard/12345
.
12345
is the id you want to pass to dashboard
.
So, you can access it like this in server:
exports.dashboard = function(req, res){
var id = req.params.id;
console.log('ID in dashboard: %s', id);
}