我已经很长时间没有使用Node.js了,也从未使用过express。当我启动应用程序时,它刚刚返回:
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)
启动test.html时发生错误。这是代码:
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);
我自己的路 :
node_modules/
express/
socket.io/
apps/
chat.js
test.html
为什么呢
编辑:
这是我的新app.configure:
app.configure(function(){
app.use(express.static(path.join(__dirname, 'public')));
});
但它返回:
path is not defined
我假设test.html是一个静态文件。要渲染静态文件,请像这样使用静态中间件。
app.use(express.static(path.join(__dirname, 'public')));
这告诉express在应用程序的公共目录中查找静态文件。
指定后,只需将浏览器指向文件的位置即可显示。
但是,如果要渲染视图,则必须使用适当的渲染器。渲染列表在合并中定义。一旦确定要使用的库就安装它。我使用了胡子,这是我的摘录配置文件
var engines = require('consolidate');
app.set('views', __dirname + '/views');
app.engine('html', engines.mustache);
app.set('view engine', 'html');
这是告诉表达
寻找要呈现在views目录中的文件
使用小胡子渲染文件
文件的扩展名是.html(您也可以使用.mustache)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句