问题描述
我不知道这是否是一个真正的noob问题,但我在node.js和Express中看到了很多关于使用Express的文档。但我所看到的是,他们总是使用另一种称为Jade的语言来呈现HTML文件。为什么?我想知道它的必要性是否使用Jade,或者我可以使用HTML在Express中呈现模板。 不,它不是必须使用Jade Express。这只是一个受欢迎的选项,因为是,由与。
他们也倾向于保持最新的互相,如添加为。
但是,还有许多其他视图引擎提供对Express的内置支持。而且,可以是中介/ ,以便您拥有:
没有必要在Express中使用视图引擎,但可以有所帮助。
Express可以简单地),因为视图通常保存在他们自己的文件中。
尽管如果你想使用。此方法取决于或您已经配置了。
app.set('view engine','jade'); //或ejs,swig等
#...
res.render('a-view'); //寻找基于`'view engine``的`a-view.jade`
app.engine('jade',require('巩固')。jade);
#...
res.render('a-view.jade'); //匹配扩展名到`.engine()`
I don't know if this a really noob question, but I have seen a lot of documentation about use Express in node.js and Express. But What I see is that they always use another lenguage called "Jade" for rendering an HTML file. Why? I'd like to know if its necesary use Jade or I can render templates in Express with HTML.
No, it's not necessary to use Jade with Express. It's just a popular option since Jade is the default for generated applications and is maintained by the same developer as Express.
They also tend to stay up-to-date with each other, such as the addition of template inheritance in jade as express dropped support for layouts.
But, there are a number of other view engines that offer built-in support for Express. And, the consolidate project can be the mediator/glue so you have even more options:
It's not necessary to use a view engine with Express, but can be helpful.
Express can simply .send() a value as the response:
res.send(new Buffer('whoop')); res.send({ some: 'json' }); res.send('some html');
But, a view engine like Jade can help with generating more complex, data-driven content from a view/template. They can also help to keep your project organized by intent (separation of concerns), since views are typically kept in their own files.
Albeit, a view engine is necessary if you want to use res.render(). This method depends on the 'view engine' application setting or that you've configured an app.engine().
app.set('view engine', 'jade'); // or ejs, swig, etc. # ... res.render('a-view'); // looks for `a-view.jade` based on `'view engine'`
app.engine('jade', require('consolidate').jade); # ... res.render('a-view.jade'); // matches the extension to the `.engine()`
这篇关于Express和node.js中的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!