在Node中渲染HTML字符串? [英] Render HTML string in Node?
问题描述
好的,所以我已经下载了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屋!