Expressjs ejs fn.apply错误“视图不是一个构造函数”
我正在尝试在原始app.get被调用之前完成一些工作。
我find了这个页面,我尝试了他们所做的工作,并且大部分工作,期望当我尝试使用渲染引擎时。
我正在使用的代码( app
是express()
)的结果:
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
确实存在
你只需要返回原来的函数调用,否则装饰的app.get
方法不会像原来那样返回任何东西:
app.get = function() { // Call the function as it would have been called normally: return originalfoo.apply(this, arguments); };