Render HTML string in Node?

旧街凉风 提交于 2019-12-12 08:21:46

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/38308600/render-html-string-in-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!