javascript - 如何进行 Monkeypatch Express View 查找?

标签 javascript node.js express

我正在尝试覆盖 Express View 查找功能,但无法做到这一点。目标是能够在多个目录中查找模板。

我想做的是,在 bootstrap.js 中:

function bootstrap(express) {
    var originalLookup = express.View.prototype.lookup;
    express.View.prototype.lookup = function(path) {
        console.log('Requested for', path);
        return originalLookup(path);
    };
};

module.exports = bootstrap;

我的app.js代码是:

var express = require('express'),
    routes = require('./routes'),
    bootstrap = require('./bootstrap'),
    app = module.exports = express.createServer();


// Configuration
require('./config/environment.js')(app, express);

// Bootstrap
bootstrap(express);

// Routes
require('./config/routes.js')(app);

// Start
app.listen(3000);

在我的 bootstrap.js 代码中,express.View.prototype.lookup 未定义。我不明白为什么。我做错了什么?

我刚刚开始使用node.js 和“高级”Javascript。我不得不承认我有点迷失在这里。

谢谢。

最佳答案

在当前版本(2.5.9)中,它不是 View.prototype.lookup,它只是 View.lookup。

~: ) node
> require('express').View
{ [Function: View]
  register: [Function],
  compile: [Function],
  lookup: [Function] }
> require('express').View.lookup
[Function]

关于javascript - 如何进行 Monkeypatch Express View 查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10113058/

相关文章:

javascript - 级联 promise

javascript - 如何从一个函数中的多个模式检索和传递数据

node.js - 无法在NodeJS中解决: 'TypeError: Converting circular structure to JSON'

javascript - jQuery 将属性集作为字符串插入到标签中

javascript - Google map API - 意外重定向

javascript - setTimeout 无法访问变量

javascript - 尝试加载 map 框时收到错误消息

javascript - Promises 不等待解析或拒绝 - javascript - ReactJs

json - 将字符串转换为 JSON 导致问题

node.js - 如何在 node.js 中使用 sequelize 编写 mysql 连接查询?