How to Render HTML File in Node JS Express?

23-Mar-2023

.

Admin

How to Render HTML File in Node JS Express?

Hi Dev,

Now, let's see a tutorial of render html file in node js express. Here you will learn How to Render HTML Files in Node JS Express. step by step explain the reget html file in node js express. I’m going to show you how to redirect html file using node js express.

In this post, we will learn to render html files in node js express. you can understand the concept of How to Render HTML Files in Node JS Express. We will look at an example of a regretful html file in node js express. This post will give you a simple example of how to redirect html files using node js express. Let's get started with node js express used to render html file example.

Step 1 : Create Node Express js App


Execute the following command on the terminal to install the 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 the node js app:

npm install express

Step 3 : Create Server.js File

In this step, you need to create a server.js file and add the following code to 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

CIn this step, create index.html file and update the following code into

tag:

This is sample html file - Tutsmake.com!

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/

#Node.js Express