Express.js sendfile() vs. render()

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?


Solution 1:

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.