我正在尝试设置一个简单的路由应用程序,但在呈现页面时我一直在运行 int 错误。
Error: Module "html" does not provide a view engine.
奇怪的是我已经在我的 app.js 文件中指定了 View 引擎,但我仍然收到错误
// app.js
var express = require('express');
var app = express();
var router = express.Router();
// Need to import the route file
var chef = require('./chef');
app.use('/chef', chef);
// Set directory to contain the templates ('views')
app.set('views', __dirname + '/views');
// Set view engine to use
app.set('view engine', 'html');
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
// chef.js
var express = require('express');
var routes = express.Router();
routes.get('/', (req, res) => {
//res.send("I'm here!")
res.render('chef');
});
module.exports = routes;
// views/chef.html
Some HTML file here here ..
在 chef.js 文件中,当我只想测试路由是否有效时,我取消了 res.send ... 的注释,它向 DOM 发送“我在这里”。
但是,每当我尝试 res.render 呈现 chef.html 页面时,我都会收到上述错误。我觉得这很奇怪,因为我在 app.js 中设置了 View 引擎。
关于如何呈现我的 HTML 文件的建议?
最佳答案
使用 res.sendFile('/fileName.html');
而不是 res.render()
为了发送文件,我们使用了 res.sendFile(fullPath)
,如果您使用的不是 HTML 语言,那么您应该使用 res.render()。
res.render()
用于ejs, pug
等模板
关于javascript - 错误 : Module "html" does not provide a view engine (Express),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46848773/