How to setup back-end server using express and node js.
Published: 2021-10-20 15:02:41

I will be explaining to you how to setup your back-end server using express and node js. 
   Before we proceed let us understand some of the terms we be using here.

## what is node
Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that is built on Chrome's V8 JavaScript engine. It uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.

It has its own package ecosystem, called npm (Node Package Manager), which is the largest ecosystem of open-source libraries in the world.

Typical tasks for a server are to handle user requests, open a file on the server, access the database, and return the content to the client. But not all web-servers work the same way. In the case of Node.js, it is important to stress its single-threaded, non-blocking, and asynchronous I/O.

For example, consider this comparison of how PHP handles the events of a file request versus how Node.js handles one.

### php:
1. The server requests a file from the file system.
2. The server waits until the system opens and reads the file.
3. The server returns the data to the client.
4. The server handles the next request. opens and reads the file.

### node.js
1. The server requests a file from the file system.
2. The server handles the next request.
3. When the system opens and reads the file, the server will return the data to the client.

Since we now understand what node.js is, let's do justice to express.

## What is express:
Express.js, or simply Express, is a back end web application framework for Node.js, released as free and open-source software under the MIT License. It is designed for building web applications and APIs. It has been called the de facto standard server framework for Node.js. 

Lastly let know what server actually mean.

## what is web-server:
A web server is software and hardware that uses HTTP (Hypertext Transfer Protocol) and other protocols to respond to client requests made over the World Wide Web. The main job of a web server is to display website content through storing, processing and delivering webpages to users.

Now that we now understand what all the terms mean, let create our web back-end server using express and node.js.

Firstly let's download and install all the application we will be using for this project. They include;

### 1. Node.js
![node js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tiu0p310d79v0she18y.png)
[Download node.js](https://www.google.com/url?sa=t&source=web&rct=j&url=https://nodejs.org/&ved=2ahUKEwjgyc6eqtnzAhUVgP0HHS1iBXgQFnoECAYQAQ&usg=AOvVaw1tY2p-vJFWJmxWlq4sTxCn)

### 2. Vs code - text editor
![vs code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xdzg1nyo858toxjqi14s.jpeg)
[Download vs code]( https://www.google.com/url?sa=t&source=web&rct=j&url=https://code.visualstudio.com/&ved=2ahUKEwjCn5_6rtnzAhUDgf0HHcPiDrwQFnoECAkQAg&usg=AOvVaw15O90sm1ios8AUpw56hCml).

After the installation process is done, let's open **command line** or **git** to setup our back-end server project
```git
   cd Desktop
   mkdir back_server
   cd back_server
   npm init --yes
   npm install --save express
   touch index.js
   Code .
```

Let's explain the functions of the command above.
**cd desktop**: change directory to desktop.

**mkdir back_server**: create a folder 📁 called _back_server_ in desktop.

**npm init --yes**: initialize node inside back_server. --yes is simply use to skip process.

**npm install --save express**: it's use to install express in our back_server folder 📂. --save is use to save express as dependency inside the package.json file.

**touch index.js**: create index.js file inside back_server folder.

**code .**: Open vs code editor.

Now that our setup is ready, let's start Some coding.

Open the index.js file with vs code 
```javaScript
const express = require('express');
const port = 3000;

//initialize express
const server = express();

//index router
server.get('/', (req, res) =>{
   res.send('Hello world');
});

server.listen(port, ()=>{ `server is running at localhost:${port}`});
```

Let explain the code about.....
* We require express module 
* initialize it to a constant _server_
* create an index router /
* send a response of _hello world_
* Then the server is pin to port 3000 to listen to any server call.

Finally let's run our server
```git
   node index
```
Let's lunch our browser and enter **_localhost:3000_**
![browser](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pv22voqot48xgw2prnio.PNG)
 
Wow😲 that cool our server is now running fine. Please leave your comments and advice. Thank you🙏