Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]);
like image 216
Himmators Avatar asked Apr 28 '26 20:04

Himmators


1 Answers

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

req.response = response;

And in your next...

var response = req.response;
like image 161
aw04 Avatar answered Apr 30 '26 08:04

aw04



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!