node.js 错误:找不到模块 html

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16111386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:15:04  来源:igfitidea点击:

Error: Cannot find module html

javascriptnode.jsexpress

提问by Cxodael

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

好久没用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)

The error occured when I launched test.html. Here's the code :

当我启动 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);

My path :

我自己的路 :

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

Why ?

为什么 ?

EDIT :

编辑 :

This is my new app.configure :

这是我的新 app.configure :

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

But it returns :

但它返回:

 path is not defined

回答by Akshat Jiwan Sharma

I am assuming that test.html is a static file.To render static files usethe static middleware like so.

我假设 test.html 是一个静态文件。要呈现静态文件,请使用像这样的静态中间件。

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

This tells express to look for static files in the public directory of the application.

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

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

但是,如果你想渲染视图,那么你必须使用适当的渲染器。渲染列表在consolidate 中定义。一旦你决定使用哪个库,只需安装它。我使用 mustache 所以这里是我的一个片段配置文件

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

这样做是告诉 express 到

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

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

  • 使用 mustache 渲染文件

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

回答by eagor

Simple way is to use the EJS template engine for serving .html files. Put this line right next to your view engine setup:

简单的方法是使用 EJS 模板引擎来提供 .html 文件。将此行放在您的视图引擎设置旁边:

app.engine('html', require('ejs').renderFile);

回答by alam

This is what i did for rendering html files. And it solved the errors. Install consolidateand mustacheby executing the below command in your project folder.

这就是我为渲染 html 文件所做的。它解决了错误。通过在项目文件夹中执行以下命令来安装consolidatemustache

$ sudo npm install consolidate mustache --save

And make the following changes to your app.jsfile

并对您的app.js文件进行以下更改

var engine = require('consolidate');

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

And now html pages will be rendered properly.

现在 html 页面将正确呈现。

回答by JohnnyCoder

I think you might need to declare a view engine.

我认为您可能需要声明一个视图引擎。

If you want to use a view/template engine:

如果要使用视图/模板引擎:

app.set('view engine', 'ejs');

app.set('view engine', 'ejs');

or

或者

app.set('view engine', 'jade');

app.set('view engine', 'jade');

But to render plain-html, see this post: Render basic HTML view?.

但是要渲染纯 html,请参阅这篇文章:渲染基本 HTML 视图?.