Use ExpressJs to render HTML Files
In NodeJs as well as in ExpressJs application there is a simple way of sending HTML or other type of file res.sendFile()
.
Using res.sendFile()
Inorder to use res.sendFile()
we need to pass in a path to the file. Path
is used for handling and transforming file paths. This module can be imported by using the syntax like const path = require('path')
.
So, Here is the example of using res.sendFile() to send a HTML file
This is our server.js file
For this first, you need to install dependencies like
npm i express
index.html will look like
and can run on the server by writing command node server.js