Javascript Nodejs:错误:找不到模块“html”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26779846/
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-08-22 23:21:41  来源:igfitidea点击:

Nodejs: Error: Cannot find module 'html'

javascriptnode.jsexpress

提问by dafriskymonkey

im using nodejs and im trying to serve only html files (no jade, ejs ... engines).

我正在使用 nodejs 并且我试图只提供 html 文件(没有 jade、ejs ... 引擎)。

heres my entry point (index.js) code:

这是我的入口点(index.js)代码:

var express = require('express');
var bodyParser = require('body-parser');

var app = express();

app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));

app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(bodyParser.json());

app.use(express.static(__dirname + '/public'));

app.get('*', function(req, res){
    res.render('index.html');
});

app.listen(app.get('port'), function() {
});

This is doing just fine when i hit the url "localhost:5000/", but when i try something like "localhost:5000/whatever" i got the following message: Error: Cannot find module 'html'

当我点击 url "localhost:5000/" 时,这很好,但是当我尝试类似 "localhost:5000/whatever" 的内容时,我收到以下消息: 错误:找不到模块 'html'

im new to nodejs, but i want allroutes to render the index.htmlfile. How can i do that ???

我是 nodejs 的新手,但我希望所有路由都呈现index.html文件。我怎样才能做到这一点 ???

Thank you.

谢谢你。

回答by vmontanheiro

You need to specify your view folder and parse the engine to HTML.

您需要指定您的视图文件夹并将引擎解析为 HTML。

var express = require('express');
var bodyParser = require('body-parser');

var app = express();

app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(bodyParser.json());



app.get('*', function(req, res){
    res.render('index.html');
});

app.listen(app.get('port'), function() {
});

回答by chetan92

Use render only when using a rendering engines like jade or ejs. If you want to use plain HTML, place it in the public folder or serve it as a static file.

仅在使用 jade 或 ejs 等渲染引擎时才使用渲染。如果您想使用纯 HTML,请将其放在公共文件夹中或作为静态文件使用。

res.sendFile('index2.html', {root : __dirname + '/views'});

回答by Codemaker

First of all you need to install ejs engine. For that you can use the following code

首先你需要安装ejs引擎。为此,您可以使用以下代码

npm install ejs

After that you need to add app engine and set the view directory.

之后,您需要添加应用引擎并设置视图目录。

The changed code is given below,

更改后的代码如下,

var express = require('express');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.urlencoded({ extended: true }));

app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

app.listen(app.get('port'), function() {
});