node.js - Send variables to all routes in exressjs -
using nodejs/expressjs build apis web app, want send variables apis, such site title , description , on.
i stumbled upon old solution using dynamichelper() no longer in use. new approach so?
easiest thing put in middleware attaches response object locals (those show in views automatically). like:
app.use(function(req,res,next) { res.locals = { title : 'your title', description : 'your description' }; return next(); });
** edit account api endpoints have since each endpoint responsible own object, like:
app.get('/whatever', function(req,res){ var json = {}; // whatever build json json.metadata = res.locals; // or whatever common stuff res.send(json); }
this keeps 'common' stuff in 1 part of json response.
Comments
Post a Comment