2016-07-19 34 views
1

我試圖在調用原始app.get之前完成一些工作。Expressjs ejs fn.apply錯誤'查看不是構造函數'

我找到了this頁面,我嘗試了他們所做的工作,它主要工作,期望當我嘗試使用它的渲染引擎時。

我使用(appexpress()結果)代碼:

const express = require('express'); 
const app = express(); 
const ejs = require('ejs'); 
app.set('view engine', 'ejs'); 

var originalfoo = app.get; 
app.get = function() { 
    // Do stuff before calling function 
    console.log(arguments); 
    // Call the function as it would have been called normally: 
    originalfoo.apply(this, arguments); 
    // Run stuff after, here. 
}; 

app.get('/home', function (req, res) { 
    //res.send('hello world') // This works 
    res.render('index'); // This crashes 
}); 

res.render給了我這個錯誤:TypeError: View is not a constructor

有誰知道我怎麼能解決這個問題?

PS:/views/index.ejs確實存在

回答

2

你只需要返回原來的函數調用,否則飾app.get方法不再返回任何東西不像原來:

app.get = function() { 
    // Call the function as it would have been called normally: 
    return originalfoo.apply(this, arguments); 
}; 
+0

這確實奏效了! 非常感謝! – CreasolDev