geeky_monster
geeky_monster

Reputation: 8792

How to pass parameter to routes?

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 ?

Upvotes: 1

Views: 6528

Answers (2)

user405398
user405398

Reputation:

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);
}

Upvotes: 3

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123463

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.

Upvotes: 5

Related Questions