gpt4 book ai didi

javascript - Expressjs ejs fn.apply 错误 'View is not a constructor'

转载 作者:行者123 更新时间:2023-11-30 15:56:23 26 4
gpt4 key购买 nike

我正在尝试在调用原始 app.get 之前完成一些工作。

我找到了 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 确实存在

最佳答案

你只需要返回原始函数调用,否则修饰的 app.get 方法不再返回任何不同于原始的东西:

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

关于javascript - Expressjs ejs fn.apply 错误 'View is not a constructor',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38462486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com