2016-07-19 6 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

関連する問題