How to Render HTML File in Node JS Express?

By Hardik Savani June 16, 2021 Category : Node JS

In this article we will cover on how to implement how to render html file in node js express. i explained simply step by step node js route to html page. you can understand a concept of node js redirect to html page. you will learn node js express render html.

In this example, i will give you very simple example of how to render html file in node js express project. we will use sendFile() to render html file, let's see bellow example.

Create Node Project:

you can run bellow command to create node app.

mkdir my-request-app

cd my-request-app

npm init

Install Express:

npm install express

Create Server.js File

let's create server.js file with get and post route.

server.js

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`))

Create index.html File

index.html

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title></title>

</head>

<body>

<p>This is simple html file call by ItSolutionStuff.com!</p>

</body>

</html>

Output:

localhost:3000/

This is simple html file call by ItSolutionStuff.com!

i hope it can help you...

Tags :
Shares