Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  147] [ 2]  / answers: 1 / hits: 136645  / 11 Years ago, fri, april 19, 2013, 12:00:00

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:Usersfrnode_modulesexpresslibview.js:42:49)
at Function.app.render (C:Usersfrnode_modulesexpresslibapplication.js:483:12)
at ServerResponse.res.render (C:Usersfrnode_modulesexpresslibresponse.js:755:7)
at allClients (C:Usersfrnode_modulesappschat.js:13:7)
at callbacks (C:Usersfrnode_modulesexpresslibrouterindex.js:161:37)
at param (C:Usersfrnode_modulesexpresslibrouterindex.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

More From » node.js

 Answers
11

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).


[#78769] Thursday, April 18, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eden

Total Points: 730
Total Questions: 117
Total Answers: 117

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
;