Error: Cannot find module 'html' with Node.JS and Express

Error: Cannot find module 'html' with Node.JS and Express

我一直在查看此问题的回复,但没有任何帮助。一些解决方案也给了我更多的错误。

我想做的是使用 Node.JS 路由到不同的页面。在了解了一些 MEAN 堆栈后,我决定使用 Node、Express 和 Angular(还没有到那儿)。如果我尝试去任何不是“/”路线的地方,我会收到一条错误消息“找不到模块 'html'”。以下是有问题的代码:

//dependencies
var express = require('express');
var path = require('path');
var engines = require('consolidate')

//variables
var RUN_PORT = 8080;
var VIDEO_IN_PORT = 45679;
var CONTROL_OUT = 45678;
var app = express();

app.use(express.static(path.join(__dirname, 'public')));

//index.html routing
app.get('/', function(req, res){
    res.render("index.html");
});

//robot_control.html routing
app.get('/robot_control', function(req, res){
    res.render("robot_control.html");
});

//error.html routing
app.get('/error', function(req, res){
    res.render("error.html");
});

//showing that the program is running on the RUN_PORT
app.listen(RUN_PORT, function(){
    console.log("Running on port " + RUN_PORT);
});

我在这里没有看到什么?一切似乎都很好。我在 package.json 中安装的只是 Express。我能够很好地访问索引页面,但除此之外的任何其他原因都会导致该错误。

问题是由使用 res.render('SOMEFILE.html') 引起的,因为您没有告诉 Express 如何呈现扩展名为 .html 的文件。

由于您还没有使用任何模板,您可以使用这个:

// Somewhere at the top:
const path  = require('path');
const VIEWS = path.join(__dirname, 'views');
...

// In your route handlers:
res.sendFile('index.html', { root : VIEWS });

如果您想开始使用模板,请看这里:http://expressjs.com/en/guide/using-template-engines.html