API to trigger app.locals.use() middleware execution #1131

Closed
abh opened this Issue May 10, 2012 · 1 comment

Projects

None yet

3 participants

@abh
abh commented May 10, 2012

I'd like to use the app.locals data in places that don't call render() (for example if the response is made with res.json()). It'd be nice if there was a way to trigger and wait for app.locals.use() middleware to execute independently of calling render().

smagch commented Jun 14, 2012
function myMiddleware(req, res, next) {
  process.nextTick(function () {
    res.locals.foo = 'bar';
    next();
  });
}

app.locals.use(myMiddleware);

app.get('/', function(req, res) {
  if (req.xhr) {
    myMiddleware(req, res, function(err) {
      res.json({ hoge: res.locals.foo});
    });
  } else {
    res.render('foobar2000');
  }
});

If you want to execute just one middleware, you can manually execute it.
#1163 (comment)

@tj tj closed this Jul 6, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment