Express.js sendfile() vs. render()

node.jsExpress

node.js Problem Overview


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.

What's the difference between the two?

node.js Solutions


Solution 1 - node.js

The render method works when you have a templating engine, such as Handlebars or Jade, in use.

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.

Such templates are often used with Express.js when writing applications that have a front-end.

The sendfile method, on the other hand, simply sends a given file to the client, regardless of the type and contents of the file.

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSoubriquetView Question on Stackoverflow
Solution 1 - node.jsChetan BhasinView Answer on Stackoverflow