How to create a simple server using Express.js

pakgamer(63)
Published in
GEMS
Words
197
Reading
1 min
Listen
Play
6y

Hello Everyone

In this post, we will learn about how to create your own server and link your local HTML page with it. In this tutorial, We are going to use Express.js. So without wasting time let's begin the tutorial.

Untitled.png

  • Open the VS code( Visual Studio Code) and open a black folder where you want to store your files.

  • Open the terminal. First, we need to install Express.js. Make sure you have already installed the node.js in your PC.

  • To install the express.js type npm install express.js --save in the terminal.

image.png

  • After successful installation, the response should something like this.

image.png

  • create a new file index.js

image.png

  • Now copy-paste the code given below.
const express=require("express");

const app =express();

app.get("/",function(re,res){
    res.send("welcome to the page. This is a post on Hive blockchain")
})

app.listen(8000,function(req,res){
    console.log("server is running");
    
});

image.png

  • After running the above script the server should be running on 8000 port and a message will be appear on the terminal which is server is running

image.png

  • Now open your browser and search http://localhost:8000/

image.png

  • As you can see our server is responding. We can also change the text size by adding <h1>

image.png

  • Stop the server and run again to apply the changes.

image.png

This much is enough for today. We will explore the express.js more in the upcoming posts. If you like the post please do upvote and comment
ntitled-1.png

How to create a simple server using Express.js | Ecency