48

When I started my application, and visited localhost:8333 in my browser, it threw an error:

Error: Cannot find module 'html'
  at Function.Module._resolveFilename (module.js:338:15)
  at Function.Module._load (module.js:280:25)
  at Module.require (module.js:364:17)
  at require (module.js:380:17)
  at new View (C:\Users\fr\node_modules\express\lib\view.js:42:49)
  at Function.app.render (C:\Users\fr\node_modules\express\lib\application.js:483:12)
  at ServerResponse.res.render (C:\Users\fr\node_modules\express\lib\response.js:755:7)
  at allClients (C:\Users\fr\node_modules\apps\chat.js:13:7)
  at callbacks (C:\Users\fr\node_modules\express\lib\router\index.js:161:37)
  at param (C:\Users\fr\node_modules\express\lib\router\index.js:135:11)

Here's my code:

var io = require('socket.io');
var express = require('express');

var app = express(),
http = require('http'),
server = http.createServer(app),
socket = require('socket.io').listen(server);

app.configure(function(){
    app.use(express.static(__dirname));
});
app.get('/', function(req, res, next){
    res.render('./test.html');
});

server.listen(8333);

This is my project folder structure:

node_modules/
    express/
    socket.io/
    apps/
        chat.js
        test.html

This is my new app.configure:

app.configure(function(){
    app.use(express.static(path.join(__dirname, 'public')));
});

But that code fails with this error:

path is not defined
Community
  • 1
  • 1
Madnx
  • 1,532
  • 4
  • 16
  • 23

7 Answers7

74

I am assuming that test.html is a static file. To render static files, use the static middleware like so:

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

This tells Express to look for static files in the public directory of the application.

Once you have specified this, simply point your browser to the location of the file and it should display.

If, however, you want to render the views, then you have to use the appropriate renderer for it. The list of renderers is defined in the consolidate.js library.

Once you have decided which library to use just install it. I use mustache so here is a snippet of my app file:

var engines = require('consolidate');

app.set('views', __dirname + '/views');
app.engine('html', engines.mustache);
app.set('view engine', 'html');

This tells Express to—

  • Look for files to render in the views directory
  • Render the files using mustache
  • The extension of the file is .html (you can use .mustache too).
Community
  • 1
  • 1
Akshat Jiwan Sharma
  • 15,430
  • 13
  • 50
  • 60
34

A simple way is to use the EJS template engine for serving .html files. Put this line right next to your view engine setup:

app.engine('html', require('ejs').renderFile);
Community
  • 1
  • 1
eagor
  • 9,150
  • 8
  • 47
  • 50
13

Install the ejs module from npm:

npm install ejs

Then just paste below two lines in your main file (app.js or main.js):

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

app.engine('html', require('ejs').renderFile);
Community
  • 1
  • 1
vaibhav
  • 199
  • 1
  • 3
3

Install consolidate and mustache by executing the below command in your project folder.

sudo npm install consolidate mustache --save

And make the following changes to your app.js file

var engine = require('consolidate');

app.set('views', __dirname + '/views');
app.engine('html', engine.mustache);
app.set('view engine', 'html');

And now HTML pages will be rendered properly.

Community
  • 1
  • 1
alam
  • 302
  • 2
  • 14
2

I think you might need to declare a view engine. If you want to use a view/template engine:

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

or

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

But to render plain HTML, see this post.

Community
  • 1
  • 1
User 1058612
  • 3,739
  • 1
  • 27
  • 33
2

Use res.sendFile() instead of res.render(). What you're trying to do is send a whole file.

Community
  • 1
  • 1
0

Try installing the html module from npm:

npm i html
Community
  • 1
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 30 '22 at 03:54
  • Incorrect, html doesn't provide an engine. – Lance Jun 08 '23 at 00:48