Node.js/Express.js - 如何覆盖/拦截 res.render 函数? [英] Node.js / Express.js - How to override/intercept res.render function?
问题描述
我正在使用 Connect/Express.js 构建一个 Node.js 应用程序,我想拦截 res.render(view, option) 函数以在将其转发到原始渲染函数之前运行一些代码.
I'm building a Node.js app with Connect/Express.js and I want to intercept the res.render(view, option) function to run some code before forwarding it on to the original render function.
app.get('/someUrl', function(req, res) {
res.render = function(view, options, callback) {
view = 'testViews/' + view;
res.prototype.render(view, options, callback);
};
res.render('index', { title: 'Hello world' });
});
它看起来像一个人为的例子,但它确实适合我正在构建的整体框架.
It looks like a contrived example, but it does fit in an overall framework I'm building.
我对 JavaScript 的 OOP 和原型继承的了解有点薄弱.我该怎么做这样的事情?
My knowledge of OOP and Prototypal inheritance on JavaScript is a bit weak. How would I do something like this?
更新:经过一些实验,我得出了以下结论:
Update: After some experimentation I came up with the following:
app.get('/someUrl', function(req, res) {
var response = {};
response.prototype = res;
response.render = function(view, opts, fn, parent, sub){
view = 'testViews/' + view;
this.prototype.render(view, opts, fn, parent, sub);
};
response.render('index', { title: 'Hello world' });
});
似乎可以.不确定这是否是最佳解决方案,因为我正在为每个请求创建一个新的响应包装器对象,这会不会有问题?
It seems to work. Not sure if it's the best solution as I'm creating a new response wrapper object for each request, would that be a problem?
推荐答案
老问题,但发现自己也在问同样的问题.如何拦截res渲染?现在使用 express 4.0x 的东西.
Old question, but found myself asking the same thing. How to intercept res render? Using express 4.0x something now.
您可以使用/编写中间件.这个概念起初对我来说有点令人生畏,但经过一些阅读后,它变得更有意义了.只是对于其他阅读本文的人来说,覆盖 res.render 的动机是提供全局视图变量.我希望 session
在我的所有模板中都可用,而不必在每个 res 对象中输入它.
You can use/write middleware. The concept was a bit daunting to me at first, but after some reading it made a little more sense. And just for some context for anyone else reading this, the motivation for overriding res.render was to provide global view variables. I want session
to be available in all my templates without me having to type it in every res object.
基本的中间件格式是.
app.use( function( req, res, next ) {
//....
next();
} );
下一个参数和函数调用对执行至关重要.next
是回调函数,允许多个中间件在不阻塞的情况下做他们的事情.要获得更好的解释,请阅读此处
The next param and function call are crucial to execution. next
is the callback function, to allow multiple middleware to do their thing without blocking. For a better explanation read here
这可以用来覆盖渲染逻辑
This can then be used to override render logic
app.use( function( req, res, next ) {
// grab reference of render
var _render = res.render;
// override logic
res.render = function( view, options, fn ) {
// do some custom logic
_.extend( options, {session: true} );
// continue with original render
_render.call( this, view, options, fn );
}
next();
} );
我已经使用 express 3.0.6 测试了这段代码.它应该可以毫无问题地与 4.x 一起使用.您还可以使用
I've tested this code, using express 3.0.6. It should work with 4.x without issue. You can also override specific URLs combos with
app.use( '/myspcificurl', function( req, res, next ) {...} );
这篇关于Node.js/Express.js - 如何覆盖/拦截 res.render 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!