我可以使用纯 HTML 在 Express 框架中编写视图吗?
所以我开始使用 Node.js 和 Express 框架,但我不知道如何摆脱 Jade 和所有其他模板......我想用纯 HTML 编写我的视图。我该怎么做呢?
So I started to use node.js along with the Express framework, but I dont know how to get rid of Jade and all the other templates... I want to write my views in plain HTML. How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想使用纯 HTML,则根本不需要使用视图——只需将 HTML 页面作为静态文件提供即可。如果您希望能够使用 Node.js 变量,或者在文件中包含文件,那么您必须拥有某种模板语言。
If you want to use plain HTML, you don't need to use views at all -- just serve the HTML pages as static files. If you want to be able to use your node.js variables, or include files within files, then you'll have to have some kind of templating language.
我知道这是一个老问题,但也许其他人也像我一样遇到过这个页面,并且正在寻找一个好的解决方案,让您可以将典型的原始 HTML 编写为模板,甚至可以让您将变量添加到 HTML 中,例如
<% = myvariable %>
他们可以从 Node.js javascript 发送。或者他们可以只使用简单的原始 HTML。解决方案是 EJS。
你可以这样做:
在 Node.js 项目的根目录中安装 ejs:
(
--save
还会将 ejs 保存到您的package.json
文件(如果存在))将 app.js 中的模板引擎设置为 ejs
现在,您可以在路由文件中分配模板变量
然后您可以在 /views 目录中创建 html 视图。
以下是使 EJS 和 Express 正常工作的过程的完整逐步演练:http://robdodson.me/blog/2012/05/31/how-to-use-ejs-in-express/
请参阅http://www.embeddedjs.com/了解更多信息,当然你也可以Google“ejs express”无引号。
I know this is an old question, but maybe others have come across this page as I did and are looking for a good solution that lets you write typical raw HTML as your template, and maybe even lets you add variables into your HTML like
<b><% = myvariable %></b>
that they can send in from their Node.js javascript. Or they could just use plain raw HTML.The solution is EJS.
You can do it this way:
Install ejs in the root directory of your Node.js project:
(
--save
also saves ejs to yourpackage.json
file if it exists)Set your template engine in app.js as ejs
Now in your route file you can assign template variables
Then you can create your html view in /views directory.
Here's a full step by step walk through of the process of getting EJS and Express working: http://robdodson.me/blog/2012/05/31/how-to-use-ejs-in-express/
See http://www.embeddedjs.com/ for more information, and of course you can Google "ejs express" no quotes.