Express sendfile()vs render() [英] Express sendfile() vs render()

查看:100
本文介绍了Express sendfile()vs render()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过res.render('index.html')和res.sendfile('index.html'),而且他们似乎都在做同样的事情。我没有找到Express文档非常有帮助。两者之间有什么区别?

解决方案

当您使用模板引擎(如handlebars.js或翡翠



模板引擎是一个与express(其中有些人称之为Express插件)相结合的节点模块,它解析模板文件并创建HTML输出。 / p>

sendfile方法只需将文件发送给客户端。



由于您使用的是HTML文件,没有什么特别要被模板引擎解析。因此,渲染的输出与sendfile的输出(即,在文件中写入的HTML)相同。因此,两者都产生相同的结果。


I tried both res.render('index.html') and res.sendfile('index.html') and they both seem to be doing the same thing. I don't find the Express documentation very helpful. What's the difference between the two?

解决方案

The render method works when you have a templating engine in use such as handlebars.js or jade.

A templating engine is a node module assosiated with express (which some people refer to as an express plugin) which parses the template file and genereated the HTML output.

The sendfile method simply sends the file to the client.

Since you are using an HTML file, there is nothing particularly to be parsed by the templating engine. So, the output of render is same as that of sendfile (i.e., the HTML written in the file). Hence, both produce the same result.

这篇关于Express sendfile()vs render()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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