-1

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.

TheLegendOfCode
  • 141
  • 2
  • 8

1 Answers1

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.com/en/guide/using-template-engines.html

robertklep
  • 198,204
  • 35
  • 394
  • 381