【问题标题】:Why do I get Error: Cannot find module 'html' when running nodejs express server?为什么在运行 nodejs express 服务器时出现错误:找不到模块 \'html\'?
【发布时间】:2022-08-15 18:38:53
【问题描述】:

我正在尝试为我的 HTML 应用程序创建一个服务器,但是当我转到 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}/`)
 })

    标签: javascript node.js express server


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多