How to Render HTML File in Node JS Express?
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Get Query String Value in Node.js?
- Node JS Express Route with Parameters Example
- How to Create Route in Node JS Express JS?
- How to Generate 4,6,8,10 Digit Random number in Node JS?
- Node JS Read and Write Json File Example
- Node JS Find and Update Object in Array Example
- How to Get Data from Json File in Node JS?
- How to Remove Element from Array in Node JS?
- How to Push Element in Array in Node JS?
- How to use Underscore JS in Node JS App?