简体   繁体   English

关于 nodeJs 和渲染模板的快速问题

[英]Quick question about nodeJs and rendering templates

So I'm learning the express framework for nodejs and am curious if I need a templating engine like ejs or pug to use the res.render('index.ejs') function to serve html.所以我正在学习 nodejs 的 express 框架,并且很好奇我是否需要像 ejs 或 pug 这样的模板引擎来使用res.render('index.ejs')函数来提供 html。

It seems like when I tried to serve up a basic html page without the template, it failed when I navigated to the page, res.render(index.html) .似乎当我尝试提供一个没有模板的基本 html 页面时,当我导航到页面res.render(index.html)时它失败了。

So I guess my question is, if I use res.render() in express, do i have no choice but to use a templating engine?所以我想我的问题是,如果我在 express 中使用res.render() ,我是否别无选择,只能使用模板引擎?

You need to use res.send file found here res.render is usually used to render a template via a template engine.您需要使用这里找到的res.send 文件 res.render 通常用于通过模板引擎渲染模板。 So it is up to how you design the project - whether you send all your html code from server via a template engine - or use a client side app like angular .因此,这取决于您如何设计项目 - 是通过模板引擎从服务器发送所有 html 代码 - 还是使用客户端应用程序,如 angular 。

i had the same problem and they recommended me to try this我有同样的问题,他们建议我试试这个

 app.engine('html', require('ejs').renderFile);

it did not work for me, but maybe it will work for you.它对我不起作用,但也许对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM