Setup Node Server Locally
Published: 2021-12-30 17:21:13

Earlier time javascript was treated as a toy language by the developers cause everybody was using javascript only for some interactions on the web browser. A great man called Ryan Dhal developed a javascript runtime from the open-source code of the V8 javascript browser runtime engine and it is called nodejs. After the introduction of nodejs there was a revolutionary change of javascript uses because now javascript can run on the server-side also. Today we will learn how to setup a  node server locally:
**Server Setup**
First, we have to install the node in our computer if the node was not installed previously. Then we will go to the command panel from windows and go to the directory where we want to keep our project file. Then we will run the following command: **npm init -y**. 
It will create a package.json file into our project folder.
After that, we will open the folder in our code editor and create an index.js file. We will write all our javascript code here. Yes! Javascript in the backend. Now we will use express.js which is a node js framework that allows us to write code for a node server smartly and easily. We will install cors node package from our terminal with npm i cors. Cors is a node package that allows cross-origin data transfer. We are using this for now for testing purposes which should not be used in production if you don’t allow cross-origin. 
Now we will write the following code in our index.js file: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/32ishm9cx3rucbjmhugg.png)
We have required the express and called express() function & also declare a port number. When we will run the server then it will run on port 5000. After that use the following code:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke7d5fdgs8wm3j114gqg.png)
We have write two methods, one is app.get() & another is app.listen(). The first fucntion return a response when we will call the server on the browser or HTTP request. The listen function will listen for any changes on the server.
**Run the server locally**
Now go to the package.json file & in scripts objects add the following scripts into the file to execute command

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8xpd1hykjcwjl15y10g.png)
 
Now go to the terminal and write the scrip i.e: npm start and we will see that server will start on the command panel and “server is running at port 5000” is consoled log. Now we go to the browser and type localhost:500 and hit enter. Then we will see “server is running printed”. Now, let’s change the response text on the **app.get()** method to another text “Hello World” and reload the browser. But you will see that it is still sowing previous text. To see the update we have to restart the server and we can see the changes on the browser with updated response. We have to change the codes so many times for our development purpose and for every change we have to restart the server which is not developer-friendly. That’s why we have written a second script “start-dev” where the command is nodemon index.js. Nodemon is a node package that automatically re-start the server if it finds any changes on the server code. To achieve this we have to install nodemon globally with this command npm insall nodemon -g
After installation of nodemon go to the command panel and give command npm start-dev. The server will start again and we can see the changes also in the browser without manually restarting the server.
To allow cross-origin require the cors in to the project and pass the function inside app.use as parameter. Now cross-origin is open.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jx37vg16m4ped6qbdnyw.png)
After that we will use express.json() to read the HTTP body request which come into json format.** app.use(express.json())**.  Now our server is ready & we can send request to the server and receive a response from the server.