How to send html page as a response from an API

May 21, 2022


Hi Folks, Welcome back once again to Codegamy, the place where learning anything is easy.

Todays topic is “How to send html page as response from an API”. So lets dive deep!.

Here I am using Node and Express JS for my API and this is the folder structure.

  1. Import Express JS and all the required modules.

2. Import path from node js module

3. use path.resolve() method to get the absolute path of the file

4. use res.sendFile() method to send the html file from restapi to client

Bhoom!! it all done, test the API response.

Full code snippet is given below:

Thank you for reading this. Hope you all learn something new today. Happy coding!!!.

Please follow us to learn new things.




Learn programming in a easy way