gpt4 book ai didi

javascript - 包含在 Express 500 错误 - .html 扩展 View 引擎中

转载 作者:行者123 更新时间:2023-12-03 03:14:23 24 4
gpt4 key购买 nike

我有一个 Express 应用程序,其中我的 View 设置为 HTML,但我实际上使用的是 ejs 引擎,以保留 .html 扩展名。它看起来像这样:

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.engine('html', ejs.renderFile);
app.use(expressLayouts);

我想在我的模板中使用 ejs。我的文件夹结构如下:

views/includes/include.html

我的包含内容如下所示:

<% include includes/include %>

将 View 引擎设置为 ejs 时(例如 app.set('view engine', 'ejs'); ),包含将按预期工作。但是当使用我现在拥有的 View 引擎配置(app.set('view engine', 'html');
app.engine('html', ejs.renderFile);
)时,我得到了这个:

GET / 500 2.074 ms - 2580 Error: Could not find include include file.

在我的 View 引擎中,我是否需要将其显式设置为 ejs,而不是我现在采用的方法?如何让包含文件与 .html 扩展名一起使用?

最佳答案

我认为EJS源代码中的相关行在这里:

https://github.com/mde/ejs/blob/67ca0a80a5a68277774c693204ce49ef11bb84b4/lib/ejs.js#L112

如果 include 尚无扩展名,则会将 .ejs 附加到文件名末尾。似乎没有任何方法可以改变这一点,但您可以将 .html 扩展名显式添加到 include 中。

关于javascript - 包含在 Express 500 错误 - .html 扩展 View 引擎中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46832809/

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