Learning Express.js-- installation and Hello World Example

in GEMS4 years ago

Hello Everyone

I hope everyone is doing fine. We are going to start a new tutorial on Express.js. This one of the most important libraries. In the previous post, We had a small post on Express.js which was just running a server on the localhost. In this series of tutorials, we are going to celebrate the Express.js. We will cover one topic in each post. So without wasting time let's begin with the installation and running the server.

  • First of all open the folder where you want to keep the project files and create a new folder if you don't have one.

image.png

  • Open the folder and copy its adress.

image.png

  • open Command Prompt and type cd copied address.

image.png

  • Now type npm init and it will ask for some information if you want to skip with default then just press enter on every question.

image.png

  • Now we need to install the Express.js file from Node package Module(NPM).

Type npm install express --save

  • Wait for it and let the files installed.

image.png

  • Type type nul > index.js in Command Prompt to create a file with name index.js.

  • Open the same folder in subline/vs Code/Atom. ( In my case I am using Subline)
    Here you can see the file we just created. We will write our code in this file which is our main file

image.png

const express = require('express')
const app = express()
const port = 5000

app.get('/', (req, res) => res.send('Welcome to Hive Blockchain'))

app.listen(port, () => console.log(`For Hive example the app is  listening at http://localhost:${port}`))

Copy-paste the above code in the index.js file.

  • Now run the code by typing node iindex.js in the Command Prompt

image.png

  • Now open this http://localhost:5000 in the browser.

image.png

  • If i want to add a second page I can just add this

image.png

  • Stop the server and rerun it

image.png

  • And I open this http://localhost:5000/hive I will see the message i entered in the code.

image.png

In the upcoming post I will explain how the thing works and what exactly our code is doing.


I think this much is enough for today. Now if you like the post please upvote and comment if you want to give me some advise
ntitled-1.png

Sort:  

Congratulations @pakgamer! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You published more than 60 posts. Your next target is to reach 70 posts.

You can view your badges on your board And compare to others on the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @hivebuzz:

The Hive community is in mourning. Farewell @lizziesworld!
Support the HiveBuzz project. Vote for our proposal!