本文介绍了错误:找不到模块html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用Node.js很长时间,从未使用过express。当我开始申请时,它刚刚返回:

 错误:在Function.Module找不到模块'html'
._resolveFilename(module.js:338:15)
在Function.Module._load(module.js:280:25)
在Module.require(module.js:364:17)
at require(module.js:380:17)
在新的视图(C:\Users\fr\\\
ode_modules\express\lib\view.js:42:49)
在Function.app.render(C:\Users\fr\\\
ode_modules\express\lib\application.js:483:12)
在ServerResponse.res.render(C:\ Users \\\\
ode_modules\express\lib\response.js:755:7)
在所有客户端(C:\Users\fr\\\
ode_modules\apps\chat.js: 13:7)
在回调(C:\Users\fr\\\
ode_modules\express\lib\router\index.js:161:37)
在param(C: \Users\fr\\\
ode_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 = socket.io')听(服务器)。

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')));
});

但返回:

 路径未定义


解决方案

我假设test.html是一个静态文件。要将静态文件静态中间件所以

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

这告诉快递在应用程序的公共目录中查找静态文件。



一旦指定了这一点,只需将浏览器指向文件的位置即可显示。



如果您想渲染视图,然后您必须使用适当的渲染器。renderes列表在中定义。一旦您已经决定使用哪个库只是安装它。我使用胡子,所以这里是我的配置文件的一个片段

  var engines = require ( '巩固'); 

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

这是做什么告诉快递到




  • 查找文件在视图目录中呈现


  • 使用胡子渲染文件


  • 该文件的扩展名为.html(您也可以使用.mustache)



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:\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)

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
解决方案

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)

这篇关于错误:找不到模块html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 03:32