Im creating a caching middleware module for node.js. Im running into an issue with res.render
where I need to access the first two parameters from the function, in the callback. The format.
res.render(view, data, callback);
Where callback is a function of the format function(err, html){}
- but I need to pass in the view and data by reference. Ideally I dont want to overload res.render
as I want to publish this as a module for others to use, eg
res.render('home', {title: 'Hello World'}, cacheThis);
where cacheThis
is my middleware module.
Any thoughts/ideas?
UPDATE -
So I made some progress, I do need to overload res.render
though - I created my own implementation of it
app.use(function (req, res, next) {
var _render = res.render;
res.render = function (view, options, callback) {
if(!_.isUndefined(callback)){
/* WHAT GOES HERE? */
}
_render.call(res, view, options, callback);
};
next();
});
The bit Im missing is in passing the view
and options
parameters to the callback (which is function that has 2 arguments itself, err
(if any errors are returned from the render) and html
(the HTML result of the render)
Ultimately I want to modify the default res.render
to be
res.render(view, options, function(err, html, view, options));
Thanks for listening.