node.js - Set object into Request in NodeJS -
i'd set in request object contains options on web interface... object have accessible in views... , doesn't work code below...
fs.readfile(optionfile, 'utf8', function (err, data) { if (err) throw err; var options = json.parse(data); request.options = options; }); <title><%-request.options.title%></title> edit #1
and don't want kind of thing :
response.render('myview', {request: request}); edit #2
exports.locals = function(app){ app.use(function(request, response, next){ fs.readfile('./configs/options.json', 'utf8', function (err, data) { if (err) throw err; var options = json.parse(data); response.locals.options = options; }); response.locals.request = request; response.locals.path = url.parse(request.url).pathname; response.locals._ = _; next(); }); }; <title><%- options.title %></title>
if want pass data templates, should use either app.locals or res.locals.
app.locals allows define global set of local variables templates, while res.locals allows bind local variables request in express middleware:
app = express(); app.locals.options = json.parse(fs.readfilesync(optionfile, 'utf8')); then you'll able use them in ejs template:
<title><%- options.title %></title>
Comments
Post a Comment