Building first web server with NodeJS
Tags: node, javascript, beginners
Published: 2019-06-20 19:10:59

This is the first part of the series. In this series, we will start with the basics of NodeJS and move towards building a **Restful API** with all the features like **Authentication**, **Database** integration and much more.

Now the first question comes in mind is **Why Node ?**

NodeJS is a runtime environment of **JavaScript** so the developers already familiar with **JavaScript** can easily learn and use **NodeJS**.
Another benefit is **Single threaded event loop** that is responsible for abstracting I/O from external requests. There are many more reasons as well but these two are sufficient I think.

Now without wasting any time, we will directly jump to building an amazing restful API.

We will use [express](https://expressjs.com/) to build this API. If you have any queries regarding *express* go to their homepage and try to read the documentation.

# Environment setup

1. Download and Install [NodeJS](https://nodejs.org/en/download/). Go for the LTS(long term support) version if you are confused.

2. Download and install your favorite code editor. I prefer [VSCode](https://code.visualstudio.com/)

3. Create a project directory.

4. Open terminal (windows user can use git bash), browse to project directory and write *npm install express* and hit enter.

5. That's it for now.

# Hello World in Node

As you are already familiar with JavaScript hello worlds. It will be the same for Node.

```javascript
console.log('Hello World')
```

# First Web Server with Node and express

```javascript
const express = require('express')
const app = express()

app.get('/', (req, res) => {
    res.send('Hello World')
})

const port = process.env.PORT || 3000
app.listen(port, () => console.log(`App is listning on port ${port}`))
```

### Explaination

In the very first line of the above code, we imported the *express* module which returns a function that we stored in a variable called **express**. In the second line, we called **express** function which returns an object and we stored it in a variable called *app*. After that, we have written the code to handle *GET* request. This get method takes two arguments.

1. The path or the URL i.e. *'/'* (Root of the website)

2. A callback function that will be called when we have an *HTTP* *GET* request. Now, this callback has two arguments *req* i.e. request and *res* i.e. response. The response is used to send the responses we want.

Now there are many different types of requests and we will talk about them in the upcoming posts.

In the last two lines, we are setting a port to listen to the requests.
We passed the port number and an optional callback function (it will log a message to the console) in the listen method.

Now to check things working properly

* Run the program *node <program name>*

* Open your favorite browser and goto *localhost:3000*. You will see a **Hello World** that indicates everything is working fine.

Finally **Congrats** you have successfully created a web server that can respond to your get requests.

Happy learning.

![See you soon](https://media.giphy.com/media/26vUF60UHCAnsXOpy/giphy.gif)