Express JS : Create a Simple Server

Express JS : Create a Simple Server

If you curios about Express JS, join with me and learn it.

ยท

2 min read

Table of contents

No heading

No headings in the article.

Hola guys , we are back .

In this section , we are going to learn to create a simple server using express js .

Express Js is external package. It is not included when you download node js. so, you need to download separately using npm. Open you terminal in project folder. and type ...

npm install express

Here, we use npm(Node package manager) to download external package called express .Express JS is a NodeJS module and express is the name of the module.

Now ,we have installed express package, and let's see how to use it .

Step1: create a file in VScode with an name , "server.js" .

Step2: paste the following code.

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

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

Step3: Run the app(server.js) with the following command:

node server.js

Example app listening on port 3000 will be printed in terminal or cmd

Finally congratulations , you have started backend application running at the port 3000. Now, I will explain the code.

const express = require('express')

In above code, we are importing express in server.js file which we installed using npm.

const app = express()

In the above , we are creating instance of express and assigning to the const variable app .

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

The functionapp.listen() starts a port and host, in our case the localhost for the connections to listen to incoming requests from a client. We can define the port number such as 3000.

Great , you have climbed the first step of Express JS. Now, what we achieved can be also done in simple Javascript. In Next blog, I will show you why we use express js over a simple Javascript .

Thank you guys , please subscribe to my blog. so that you will get all my posts and blogs.

Did you find this article valuable?

Support HARRISH G by becoming a sponsor. Any amount is appreciated!

ย