Question

I'd like to override the View class in the Express framework, used in Node.js. I want to augment the lookup method, but I can't see a way to do this without altering the Express and App modules. I'd favour deriving from the Express framework, but I can't figure out a neat way to do this.

Any ideas?

Thanks

Was it helpful?

Solution

It seems to me you should be able to:

var View = require('express/lib/view');

// Keep reference to original lookup method
var _lookup = View.prototype.lookup;

// Override lookup method
View.prototype.lookup = function (path) {
    // Your implementation here
};

Update:

Run this as a demonstration:

var View = require('express/lib/view');
var _lookup = View.prototype.lookup;
var express = require('express');

View.prototype.lookup = function (path) {
    console.log('LOOKUP!!! ' + path);

    return _lookup.call(this, path);
};

var app = express();

app.get('/', function (req, res) {
    res.render('foo.jade');
});

app.listen(3000);

Run

node app & sleep 1 && curl localhost:3000

I hope this will demonstrate the viability of this way of overriding a method.

OTHER TIPS

It depends on which version of Express you are using.

You can easily augment the view lookup code only if your app is using Express prior to version 3

Since Express 3.0 that's not doable anymore.

You can check one of my old related answers for sample code: Multiple View paths on Node.js + Express

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top