How to Render HTML File in Node JS Express

How to Render HTML File in Node JS Express

Render html file data in node js express; Through this tutorial, we will learn how to render html file in node js express.

How to Render HTML File in Node JS Express

Follow the following steps to render html file data or plain html in node js express:

  • Step 1 – Create Node JS App
  • Step 2 – Install Express
  • Step 3 – Create Server.js File
  • Step 4 – Create index.html File
  • Step 5 – Start Development Server

Step 1 – Create Node JS App

Execute the following command on terminal to install node js app:

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

Step 2 – Install Express

In this step, open again your terminal and execute the 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; Through this tutorial, we have learned how to render html file 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 *