How to render HTML file in Express JS


Hi friends, in this tutorial, you will learn how to render HTML file in express JS. As I told earlier that express JS is a framework of node JS with various packages and functionalities which reduces the code and execute the programs faster.

Also read, How to install Express js in vs code with an example

So, we will use the features of express JS by loading the modules of express JS in order to load HTML files. If you do not know how to run node JS programs then you can click the link below.


Also, read, Node Js hello world program

Required steps to render HTML file in express JS

Step 1:- Load the express JS module of node JS with the help of the const keyword as shown below.

const express = require('express');

Step 2:- Now, call the express() method to use the packages and functionalities of express JS and assigned it to a new variable called ‘data’ as shown below.

const data = express();

Step 3:- Next, we will initialize the path module of Node JS using the const keyword as shown below.

const path = require('path');

Please note that by using the path module we can get the path of any file or folder location.

Step 4:- Now, we will use the path module to get the path of the root directory of the project with the help of the join() method as shown below.

const publicPath = path.join(__dirname);

On the above line of code, join(__dirname) is used to get the path of the project directory.

Step 5:- Now, create a public folder under the root directory of the project as shown below.

Step 6:- Now, we can access the path of the public folder inside the project directory by passing “public” as a second argument within the join() method as shown below.

const publicPath = path.join(__dirname,'public');

Step 7:- Create a simple HTML file inside the public folder as shown below.

about.html:-

<html>
    <title>About US</title>
    <head></head>
    <body>
        <h2>Welcome to about us page</h2>
    </body>
</html>

Step 8:- Next, we will use the static() function of express JS modules and pass the publicPath as a parameter inside the static() function to access all the HTML files that exist in the public folder as given below.

data.use(express.static(publicPath));

Complete Code:- (express.js)

const express = require('express');
const data = express();
const path = require('path');
const publicPath = path.join(__dirname,'public');
console.log(publicPath);
data.use(express.static(publicPath));
data.listen(3000);

Step 9:- Go to your browser URL and type https://localhost:3000/about.html and press ENTER.

Step 10:- You will see the HTML file output as shown below.

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below. Download vs code from here.


Leave a Comment

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

KUPU178

KUPU178

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

KUPU178

KUPU178

ceri188

ceri188

KUPU178

KUPU178

KUPU178

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

KUPU178

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

KUPU178

daftar ceri188

cery188

ceri188

KUPU178

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

HOKI311

THOR311

RATU311

KUPU178

THOR311

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

KUPU178

THOR311

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

KUPU178

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

THOR311

RATU311

KUPU178

KUPU178

RATU311

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

KUPU178

THOR311

HOKI311

KUPU178

RATU311

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311