Himmators
Himmators

Reputation: 15006

How to share a variable between routing-functions?

I'm building a serverside parser of an API. Every get-request to my site needs to start with a request to that api, the request to the api is always the same, so I want to put it in it's own function.

How can I pass the response-variable from the api-function to the render-function?

var api = function (req, res, next) {
  http.get({...}), function(response){}
  next();
}

var render = function (req, res, next) {
  app.res.render('master',response);
  next();
}

app.get('/example/d', [api, render]);

Upvotes: 3

Views: 36

Answers (1)

aw04
aw04

Reputation: 11177

You can assign it to req before calling next().

req.response = response;

And in your next...

var response = req.response;

Upvotes: 1

Related Questions