ExpressJS allows you to develop custom web server according to your need. You don’t need to install multiple packages to handle HTML files. If you have Node.js installed, you are good to go.
In this short tutorial i am going to explain how to render HTML files in ExpressJS.
About sendFile() function.
ExpressJS provides sendFile() function which will basically send HTML files to browser which then automatically interpreted by browser. All we need to do is in every route deliver appropriate HTML file.
For.eg :
When user hit main URL deliver index.html :
When user hit main URL deliver index.html :
This code is for example purpose. It will cause directory resolution error.
Our project :
I am going to develop simple website consist of pages i.e Home page. I will use Bootstrap for designing.
Directory structure :
package.json
and Run Project using :
Here is our Express Server :
Here is our index.html file :
Here is the output :
Here is our index.html file :
Here is the output :
Conclusion:
There are scenarios where you need to develop web server which delivers HTML files like how your apache do. However this is not the optimal use of Node.js but you can use such feature to achieve custom web server for your own application.







Thanks for sharing this ....i learnt a lot ....
ReplyDeleteYour Welcome Sir...!!!
Delete