错误:找不到模块 html [英] Error: Cannot find module html

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

问题描述

我很久没用过 Node.js,也没用过 express.当我启动我的应用程序时,它刚刚返回:

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)

当我启动 test.html 时出现错误.这是代码:

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

我的路径:

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 是一个静态文件.要呈现静态文件 使用像这样的静态中间件.

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

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

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.

如果你想渲染视图,那么你必须使用合适的渲染器.渲染列表定义在 合并.一旦你决定使用哪个库,就安装它.我使用 mustache 所以这是我的配置文件的一个片段

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

这是告诉快递给

  • 在视图目录中查找要渲染的文件

  • look for files to render in views directory

使用 mustache 渲染文件

Render the files using mustache

文件的扩展名是.html(你也可以使用.mustache)

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆