,

Express JS and Node JS Tutorial: How to Create and Serve HTML Pages (Full Course in Hindi/Urdu, Part 34)

Posted by

Creating, Serving HTML Pages using Express JS

Node JS Tutorials/Full Course in Hindi/Urdu – Part 34

In this tutorial, we will learn how to create and serve HTML pages using Express JS in Node JS.

Step 1: Install Express JS

First of all, you need to install Express JS in your Node JS project. You can do this by running the following command in your terminal:

npm install express

Step 2: Create an HTML file

Next, create an HTML file that you want to serve using Express JS. You can create a simple HTML file like this:


<!DOCTYPE html>
<html>
<head>
<title>My HTML Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

Step 3: Serve the HTML page using Express JS

Now, create a new file in your project called app.js and add the following code to it:


const express = require('express');
const app = express();

app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});

Save the changes and run the following command in your terminal:

node app.js

Now, open your web browser and navigate to http://localhost:3000 to see your HTML page being served by Express JS.

Conclusion

That’s it! You have successfully created and served an HTML page using Express JS in Node JS. You can now build more complex web applications using Express JS and Node JS.

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@sadafangel2030
5 months ago

👍👍👍👍

@naghmanrobinson403
5 months ago

Informative 👍