Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

javascript - Error: Cannot find module html

I have not used Node.js for a long time and never used express. When I started my application, it just returned :

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:Usersfr
ode_modulesexpresslibview.js:42:49)
  at Function.app.render (C:Usersfr
ode_modulesexpresslibapplication.js:483:12)
  at ServerResponse.res.render (C:Usersfr
ode_modulesexpresslib
esponse.js:755:7)
  at allClients (C:Usersfr
ode_modulesappschat.js:13:7)
  at callbacks (C:Usersfr
ode_modulesexpresslib
outerindex.js:161:37)
  at param (C:Usersfr
ode_modulesexpresslib
outerindex.js:135:11)

The error occured when I launched test.html. Here's the 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);

My path :

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

Why ?

EDIT :

This is my new app.configure :

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

But it returns :

 path is not defined
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 renderes is defined in consolidate.Once you have decided which library to use just install it.I use mustache so here is a snippet of my config file

var engines = require('consolidate');

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

What this does is tell express to

  • look for files to render in views directory

  • Render the files using mustache

  • The extension of the file is .html(you can use .mustache too)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...