Express.js sendfile()与render() [英] Express.js sendfile() vs. render()
问题描述
我尝试了res.render('index.html')
和res.sendfile('index.html')
,它们似乎都在做相同的事情.我觉得Express.js文档不是很有帮助.
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.js documentation very helpful.
两者之间有什么区别?
推荐答案
当您具有模板引擎时,例如 Jade 在使用中.
The render
method works when you have a templating engine, such as Handlebars or Jade, in use.
模板引擎是解析给定模板文件并生成HTML输出的东西.这样,您可以根据程序中的某些变量来生成HTML网页.
A templating engine is something that parses a given template file and generates HTML output. This is so you can generate an HTML web page depending on some variables in your program.
在编写具有前端的应用程序时,此类模板通常与Express.js一起使用.
Such templates are often used with Express.js when writing applications that have a front-end.
另一方面,sendfile
方法仅将给定文件发送到客户端,而不管文件的类型和内容.
The sendfile
method, on the other hand, simply sends a given file to the client, regardless of the type and contents of the file.
由于您使用的是HTML文件,因此模板引擎无需特别解析.因此,render
的输出与sendfile
的输出相同(即,文件中写入的HTML).因此,两者都会产生相同的结果.
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.js sendfile()与render()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!