在 Express 中使用 HTML 而不是 Jade

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

Using HTML in Express instead of Jade

htmlnode.jsexpresspug

提问by Sanchit Gupta

How to I get rid of Jade while using Express with Node.JS? I want to just use plain html. In other articles I have seen that people recommended app.register() which is now deprecated in the latest version.

在 Node.JS 中使用 Express 时如何摆脱 Jade?我只想使用纯 html。在其他文章中,我看到人们推荐 app.register() ,它现在在最新版本中已弃用。

回答by Biwek

You can do it this way:

你可以这样做:

  1. Install ejs:

    npm install ejs
    
  2. Set your template engine in app.js as ejs

    // app.js
    app.engine('html', require('ejs').renderFile);
    app.set('view engine', 'html');
    
  3. Now in your route file you can assign template variables

    // ./routes/index.js
    exports.index = function(req, res){
    res.render('index', { title: 'ejs' });};
    
  4. Then you can create your html view in /views directory.

  1. 安装 ejs:

    npm install ejs
    
  2. 将 app.js 中的模板引擎设置为 ejs

    // app.js
    app.engine('html', require('ejs').renderFile);
    app.set('view engine', 'html');
    
  3. 现在在你的路由文件中你可以分配模板变量

    // ./routes/index.js
    exports.index = function(req, res){
    res.render('index', { title: 'ejs' });};
    
  4. 然后你可以在 /views 目录中创建你的 html 视图。

回答by Martin Sookael

Jade also accepts html input.
Just add a dot to the end of line to start submitting pure html.
If that does the trick for you then try:

Jade 也接受 html 输入。
只需在行尾添加一个点即可开始提交纯 html。
如果这对您有用,请尝试:

doctype html              
html. // THAT DOT
    <body>     
        <div>Hello, yes this is dog</div>
    </body>

PS - No need to close HTML - that's done automagically by Jade.

PS - 无需关闭 HTML - 这是由 Jade 自动完成的。

回答by laconbass

As of express 3 you can simply use response.sendFile

从 express 3 开始,您可以简单地使用 response.sendFile

app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});)

From the official express api reference:

来自官方 express api 参考

res.sendfile(path, [options], [fn]])

Transfer the file at the given path.

Automatically defaults the Content-Type response header field based on the filename's extension. The callback fn(err)is invoked when the transfer is complete or when an error occurs.

res.sendfile(path, [options], [fn]])

在给定路径传输文件。

根据文件名的扩展名自动默认 Content-Type 响应头字段。fn(err)当传输完成或发生错误时调用回调。

Warning

警告

res.sendFileprovides client-side cache through http cache headers but it does not cache file contents on server-side. The code above will hit the disk on each request.

res.sendFile通过 http 缓存标头提供客户端缓存,但它不会在服务器端缓存文件内容。上面的代码将在每次请求时命中磁盘

回答by kevin.groat

In my opinion, using something as big as ejs just to read html files is a bit heavy-handed. I just wrote my own template engine for html files that's remarkably simple. The file looks like this:

在我看来,使用像 ejs 这样大的东西来读取 html 文件有点笨手笨脚。我刚刚为非常简单的 html 文件编写了自己的模板引擎。该文件如下所示:

var fs = require('fs');
module.exports = function(path, options, fn){
    var cacheLocation = path + ':html';
    if(typeof module.exports.cache[cacheLocation] === "string"){
        return fn(null, module.exports.cache[cacheLocation]);
    }
    fs.readFile(path, 'utf8', function(err, data){
        if(err) { return fn(err); }
        return fn(null, module.exports.cache[cacheLocation] = data);
    });
}
module.exports.cache = {};

I called mine htmlEngine, and the way you use it is simply by saying:

我叫我的 htmlEngine,你使用它的方式只是说:

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

回答by josh3736

app.register()hasn't been depreciated, it has just been renamed to app.engine()since Express 3 changes the way template engines are handled.

app.register()没有折旧,它刚刚被重命名为,app.engine()因为 Express 3改变了模板引擎的处理方式

Express 2.x template engine compatibility required the following module export:

exports.compile = function(templateString, options) {
    return a Function;
};

Express 3.x template engines should export the following:

exports.__express = function(filename, options, callback) {
  callback(err, string);
};

If a template engine does not expose this method, you're not out of luck, the app.engine()method allows you to map any function to an extension. Suppose you had a markdown library and wanted to render .md files, but this library did not support Express, your app.engine()call may look something like this:

var markdown = require('some-markdown-library');
var fs = require('fs');

app.engine('md', function(path, options, fn){
  fs.readFile(path, 'utf8', function(err, str){
    if (err) return fn(err);
    str = markdown.parse(str).toString();
    fn(null, str);
  });
});

Express 2.x 模板引擎兼容性需要以下模块导出:

exports.compile = function(templateString, options) {
    return a Function;
};

Express 3.x 模板引擎应导出以下内容:

exports.__express = function(filename, options, callback) {
  callback(err, string);
};

如果模板引擎没有公开这个方法,你也不是不走运,这个app.engine()方法允许你将任何函数映射到一个扩展。假设你有一个 markdown 库并且想要渲染 .md 文件,但是这个库不支持 Express,你的app.engine()调用可能看起来像这样:

var markdown = require('some-markdown-library');
var fs = require('fs');

app.engine('md', function(path, options, fn){
  fs.readFile(path, 'utf8', function(err, str){
    if (err) return fn(err);
    str = markdown.parse(str).toString();
    fn(null, str);
  });
});

If you're looking for a templating engine that lets you use 'plain' HTML, I recommend doTbecause it is extremely fast.

如果您正在寻找允许您使用“纯”HTML 的模板引擎,我推荐doT,因为它非常快

Of course, keep in mind that the Express 3 view model leaves view caching up to you (or your templating engine). In a production environment, you probably want to cache your views in memory so that you aren't doing disk I/O on every request.

当然,请记住 Express 3 视图模型将视图缓存留给您(或您的模板引擎)。在生产环境中,您可能希望将视图缓存在内存中,这样您就不会对每个请求都进行磁盘 I/O。

回答by Pablo Vallejo

You can use EJS with express which templates are HTML but support variables. Here is a good tutorial in how to use EJS in express.

您可以将 EJS 与 express 哪些模板是 HTML 但支持变量一起使用。这是一个关于如何在 express 中使用 EJS 的好教程。

http://robdodson.me/blog/2012/05/31/how-to-use-ejs-in-express/

http://robdodson.me/blog/2012/05/31/how-to-use-ejs-in-express/

回答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 Soubhagya Kumar Barik

first check the compatibility version of template engine by using below line

首先使用以下行检查模板引擎的兼容性版本

express -h

then you have to use no view from the list.select no view

那么您必须使用列表中的无视图。选择无视图

express --no-view myapp

now you can use your all html,css,js and images in public folder.

现在您可以使用公共文件夹中的所有 html、css、js 和图像。

回答by S Meaden

Well, it sounds like you want to serve static files. And there is a page for that http://expressjs.com/en/starter/static-files.html

好吧,听起来您想提供静态文件。并且有一个页面 http://expressjs.com/en/starter/static-files.html

Bizarre that nobody is linking to the documentation.

奇怪的是没有人链接到文档。

回答by Priyanshu Chauhan

You can also directly include your html file into your jade file

您也可以直接将您的 html 文件包含到您的 jade 文件中

include ../../public/index.html

Original answer: Express Generator Without Jade

原答案:无玉快递发电机