Node.js / Express.js - 如何覆盖/拦截res.render函数? [英] Node.js / Express.js - How to override/intercept res.render function?

查看:338
本文介绍了Node.js / Express.js - 如何覆盖/拦截res.render函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Connect / Express.js构建一个Node.js应用程序,我想拦截res.render(view,option)函数来运行一些代码,然后将其转发到原始的render函数。

  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'});
});

它看起来像一个有创意的例子,但它符合我正在构建的整体框架。 / p>

我对于Java的OOP和Prototypal继承知识有点薄弱。我如何做这样的事情?






更新:经过一番实验,我想出了以下:

  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'});
});

似乎有效。不确定是否是最好的解决方案,因为我正在为每个请求创建一个新的响应包装器对象,这是一个问题吗?

解决方案

老问题,但发现自己也是一样的问题。如何拦截res渲染?
现在使用express 4.0x。



可以使用/写中间件。这个概念起初对我来说有点令人生畏,但经过一番阅读,这一点更有意义。只是为了阅读这个任何人的某些上下文,覆盖res.render的动机是提供全局视图变量。我希望 session 可以在我的所有模板中使用,而无需在每个res对象中输入。



基本的中间件格式是。

  app.use(function (req,res,next){
// ....
next();
});

下一个param和函数调用对执行至关重要。 next 是回调函数,允许多个中间件做事情而不阻止。为了更好的此处阅读的解释



然后可以用它来覆盖渲染逻辑

  app.use(function req,res,next){
// grab reference of render
var _render = res.render;
//覆盖逻辑
res.render = function(view,options, fn){
//做一些自定义逻辑
_.extend(options,{session:true});
//继续原始渲染
_render.call(这, view,options,fn);
}
next();
});

我已经使用express 3.0.6测试了这段代码。它应该与4.x没有问题。
您还可以使用

  app.use('/ myspcificurl',function(req,res , 下一个 ) {...} ); 


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.

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?

解决方案

Old question, but found myself asking the same thing. How to intercept res render? Using express 4.0x something now.

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.

The basic middleware format is.

app.use( function( req, res, 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();
} );

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆