繁体   English   中英

为什么在运行 nodejs express 服务器时出现错误:找不到模块 'html'?

[英]Why do I get Error: Cannot find module 'html' when running nodejs express server?

我正在尝试为我的 HTML 应用程序创建服务器,但是当我 go 到 localhost:8080/map 时,我得到Error: Cannot find module 'HTML' 尽管如此, localhost:8080 页面的主页面工作正常。

我的代码:

const path = require('path');
const port = 8080;
const express = require('express');
const app = express();

app.use(express.static(path.join(__dirname, 'public')));
app.set("view engine", html);


app.get('/', (req, res) => {
    res.render('index.html')
})

app.get('/map/', (req, res) => {
    res.render('map.html')
})

var server = app.listen(8080, function () {
    console.log(`App listening at http://localhost:${server.address().port}/`)
 })

你正在使用 EJS,所以你需要这样做

app.set('view engine', 'html');

app.engine('html', require('ejs').renderFile);

使用app.engine(ext, callback)方法创建您自己的模板引擎。 ext指的是文件扩展名, callback是模板引擎function,它接受以下项目作为参数:文件的位置,选项object,以及回调ZC1C425268E68385D1AB5074C17A94。

因此,您实际上使节点运行时加载“html”模板引擎并且它工作正常。

请参考此 链接

此外,您在这一行app.set("view engine", html);上缺少引号。 . 它应该是app.set("view engine", "html");

 const express = require('express'); const app = express(); app.use('/', express.static(__dirname + '/')); app.get('/', function(req, res) { //res.render('index.html') //Such a static call assumes the existence of an index.html file at the call address: nodejs\node_modules\index.html }); app.get('/map', function(req, res) { res.send('Taylor Swift'); }); app.listen(8080); console.log(`App listening at http://localhost:8080/map`)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM