如何在 Express 中使用 HTML 作为视图引擎?

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

How do I use HTML as the view engine in Express?

htmlnode.jsexpress

提问by Julio

I tried this simple change from the seed and created the corresponding .html files (e.g. index.html).

我从种子中尝试了这个简单的更改并创建了相应的 .html 文件(例如 index.html)。

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

and this file remained the same:

这个文件保持不变:

exports.index = function(req, res){
  res.render('index');
};

but while running I get

但在跑步时我得到

500 Error: Cannot find module 'html'

500 错误:找不到模块“html”

Is my only option to use 'ejs'? My intent was to use plain HTML in conjuction with AngularJS.

使用“ejs”是我唯一的选择吗?我的意图是将纯 HTML 与 AngularJS 结合使用。

回答by Dan Kohn

The answers at the other link will work, but to serve out HTML, there is no need to use a view engine at all, unless you want to set up funky routing. Instead, just use the static middleware:

另一个链接上的答案会起作用,但要提供 HTML,根本不需要使用视图引擎,除非您想设置时髦的路由。相反,只需使用静态中间件:

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

回答by AnandShanbhag

To make the render engine accept html instead of jade you can follow the following steps;

要使渲染引擎接受 html 而不是 jade,您可以按照以下步骤操作;

  1. Install consolidateand swigto your directory.

     npm install consolidate
     npm install swig
    
  2. add following lines to your app.js file

    var cons = require('consolidate');
    
    // view engine setup
    app.engine('html', cons.swig)
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'html');
    
  3. add your view templates as .html inside “views” folder. Restart you node server and start the app in the browser.

  1. consolidateswig安装到您的目录中。

     npm install consolidate
     npm install swig
    
  2. 将以下行添加到您的 app.js 文件中

    var cons = require('consolidate');
    
    // view engine setup
    app.engine('html', cons.swig)
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'html');
    
  3. 将您的视图模板作为 .html 添加到“views”文件夹中。重新启动您的节点服务器并在浏览器中启动应用程序。

Though this will render html without any issue, I would recommend you to use JADE by learning it. Jade is an amazing template engine and learning this will help you achieve better design & scalability.

虽然这将毫无问题地呈现 html,但我建议您通过学习使用 JADE。Jade 是一个了不起的模板引擎,学习它可以帮助您实现更好的设计和可扩展性。

回答by Sudhanshu Gupta

In your apps.js just add

在您的 apps.js 中添加

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

Now you can use ejs view engine while keeping your view files as .html

现在您可以使用 ejs 视图引擎,同时将视图文件保持为 .html

source: http://www.makebetterthings.com/node-js/how-to-use-html-with-express-node-js/

来源:http: //www.makebetterthings.com/node-js/how-to-use-html-with-express-node-js/

You need to install this two packages:

你需要安装这两个包:

`npm install ejs --save`
`npm install path --save`

And then import needed packages:

然后导入需要的包:

`var path = require('path');`


This way you can save your views as .htmlinstead of .ejs.
Pretty helpful while working with IDEs that support html but dont recognize ejs.


通过这种方式,您可以将视图保存为.html而不是.ejs
在使用支持 html 但不识别 ejs 的 IDE 时非常有用。

回答by Connor Leech

try this for your server config

试试这个为你的服务器配置

app.configure(function() {
    app.use(express.static(__dirname + '/public'));         // set the static files location
    app.use(express.logger('dev'));                         // log every request to the console
    app.use(express.bodyParser());                          // pull information from html in POST
    app.use(express.methodOverride());                      // simulate DELETE and PUT
    app.use(express.favicon(__dirname + '/public/img/favicon.ico'));
});

then your callback functions to routes will look like:

那么您对路由的回调函数将如下所示:

function(req, res) {
    res.sendfile('./public/index.html');
};

回答by Ashish Rawat

No view engine is necessary, if you want to use angular with simple plain html file. Here's how to do it: In your route.jsfile:

如果您想在简单的纯 html 文件中使用 angular,则不需要视图引擎。操作方法如下: 在您的route.js文件中:

router.get('/', (req, res) => {
   res.sendFile('index.html', {
     root: 'yourPathToIndexDirectory'
   });
});

回答by didinko

I recommend using https://www.npmjs.com/package/express-es6-template-engine- extremely lightwave and blazingly fast template engine. The name is a bit misleading as it can work without expressjs too.

我建议使用https://www.npmjs.com/package/express-es6-template-engine- 极光波和极快的模板引擎。这个名字有点误导,因为它也可以在没有 expressjs 的情况下工作。

The basics required to integrate express-es6-template-enginein your app are pretty simple and quite straight forward to implement:

集成express-es6-template-engine到您的应用程序中所需的基础知识非常简单且易于实现:

const express = require('express'),
  es6Renderer = require('express-es6-template-engine'),
  app = express();
  
app.engine('html', es6Renderer);
app.set('views', 'views');
app.set('view engine', 'html');
 
app.get('/', function(req, res) {
  res.render('index', {locals: {title: 'Welcome!'}});
});
 
app.listen(3000);
这是位于index.htmlindex.html“views”目录中的文件内容:

<!DOCTYPE html>
<html>
<body>
    <h1>${title}</h1>
</body>
</html>

回答by Dinesh Kanivu

Answer is very Simple. You Must use app.engine('html') to render *.html Pages. try this.It must Solve the Problem.

答案很简单。您必须使用 app.engine('html') 来呈现 *.html 页面。试试这个。它必须解决问题。

app.set('views', path.join(__dirname, 'views'));
**// Set EJS View Engine**
app.set('view engine','ejs');
**// Set HTML engine**
app.engine('html', require('ejs').renderFile);

the .html file Will work

.html 文件将工作

回答by Shivam Chhetri

Comment out the middleware for html i.e.

注释掉html的中间件即

//app.set('view engine', 'html');

Instead use:

而是使用:

app.get("/",(req,res)=>{
    res.sendFile("index.html");
});

回答by JerryFZhang

HTML files can be rendered using ejs engine:

可以使用 ejs 引擎呈现 HTML 文件:

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

And make sure your files under "/views" are named with ".ejs".

并确保“/views”下的文件以“.ejs”命名。

For example "index.ejs".

例如“index.ejs”。

回答by Wael Chorfan

html is not a view engine , but ejs offers the possibility to write html code within it

html 不是视图引擎,但 ejs 提供了在其中编写 html 代码的可能性