在Node中渲染HTML字符串? [英] Render HTML string in Node?

查看:285
本文介绍了在Node中渲染HTML字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我已经下载了Express,用process.env.PORT || 8080设置了端口,并设置了应用程序变量var app = express().现在,我要完成的工作不是通过文件呈现HTML,而是可以通过字符串来实现?

Alright, so I have downloaded Express, set the port with process.env.PORT || 8080, and set the app variable var app = express(). Now, what I'm trying to accomplish is instead of rendering HTML through a file, could I do it through a string?

var html = "<!DOCTYPE html>\n<html>\n    <head>\n    </head>\n <body>\n      <h1>Hello World!</h1>\n   </body>\n</html>";
app.get('/',function(req,res){
   res.render(html);
});

有没有办法做到这一点?

Is there a possible way to do this?

推荐答案

文档中指定的res.render方法:呈现视图并将呈现的HTML字符串发送给客户端.需要使用模板引擎,例如:jade,ejs,handlebars ..,但是如果您的目的是仅输出一些html,则可以使用res.send代替.

the res.render method as specified in the doc : Renders a view and sends the rendered HTML string to the client. So you need to use a template engine eg : jade,ejs, handlebars.. but if your purpose is to only output some html you can do it with res.send instead.

这篇关于在Node中渲染HTML字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆