最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error: Cannot find module 'html' with Node.JS and Express - Stack Overflow

programmeradmin1浏览0评论

I've been looking at responses on here for this question, but nothing has helped me. Some solutions have given me even more errors, too.

What I am trying to do is route to different pages with Node.JS. Having learnt a little bit on the MEAN stack, I decided to use Node, Express, and Angular (haven't gotten there yet). If I try to to to any place that isn't the '/' route, I get an error saying 'Cannot find module 'html'. Below is the code in question:

//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);
});

What am I not seeing here? Everything seems to look fine. All I have installed in my package.json is Express. I am able to get to the index page just fine, but anything other than that causes that error.

I've been looking at responses on here for this question, but nothing has helped me. Some solutions have given me even more errors, too.

What I am trying to do is route to different pages with Node.JS. Having learnt a little bit on the MEAN stack, I decided to use Node, Express, and Angular (haven't gotten there yet). If I try to to to any place that isn't the '/' route, I get an error saying 'Cannot find module 'html'. Below is the code in question:

//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);
});

What am I not seeing here? Everything seems to look fine. All I have installed in my package.json is Express. I am able to get to the index page just fine, but anything other than that causes that error.

Share Improve this question asked Apr 15, 2017 at 7:02 TheLegendOfCodeTheLegendOfCode 1412 silver badges9 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

The issue is caused by using res.render('SOMEFILE.html'), because you're not telling Express how to render files with a .html extension.

Since you're not using any templating (yet), you can use this:

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

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

When you want to start using templating, look here: http://expressjs./en/guide/using-template-engines.html

发布评论

评论列表(0)

  1. 暂无评论