Render HTML Files in Node JS Express

Render HTML Files in Node JS Express

The res.sendFile() allows users to render HTML files in node express js. In this tutorial, we will learn how to render HTML files in node js express using res.SendFile() function.

How to Render HTML Files in Node JS Express

Steps to render html file data or plain HTML in node js express:

Step 1 – Create Node JS App

Run the following command on cmd to create node js application:

mkdir my-app
cd my-app
npm init -y

Step 2 – Install Express

In this step, run following command to install express in node js app:

npm install express

Step 3 – Create Server.js File

In this step, you need to create server.js file and add the following code into it:

var express = require('express');
var app = express();
  
app.get('/',function(req,res) {
  res.sendFile(__dirname + '/index.html');
});
   
app.listen(3000, () => console.log(`App listening on port 3000`))

Step 4 – Create index.html File

In this step, create index.html file and update the following code into it:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
</head>
<body>
  
<p>This is sample html file -  Tutsmake.com!</p>
  
</body>
</html>

Step 5 – Start Development Server

You can use the following command to run development server:

//run the below command

npm start

after run this command open your browser and hit 

http://127.0.0.1:3000/

Conclusion

That’s it, we have learned how to render html files in node js express.

Recommended Node JS Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *