Let's build a Node.js project (REST API)
Tags: node, development, api, postman
Published: 2023-04-02 11:42:19

---
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85ftcfjy501i4kv6g8rw.png
title: Let's build a Node.js project (REST API)
published: true
date: 2023-03-20 08:05:25 UTC
tags: nodejs, development, API, Postman
canonical_url: 
---

Backend lovers, this is for you! Today I am starting with this development series where I will be talking about all things **BUILD**. This is the very first article in this series. We will be building a **node.js** project and learning how to develop a **REST API**. We will later test this API using **Postman**. I am using **VS Code** as IDE, but you can use your favorite text editor.

Let's begin...🏎

![gif](https://media.giphy.com/media/gLiyt8QwcHA5ZvLwaF/giphy.gif)

1. Let's create a folder called "node-demo" where we will create our node project.

2. Now run the command `npm init -y` in the terminal. This will create a package.json file that will contain the entire information about the project like its version, dependencies, license, author, etc. Right now it will insert some default info for each prop since we passed the `-y` flag but if you want you can change them later as per your need.
![npm init](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4b6dekqjoz9mouj3naq.jpg)

3. Now let's set up a server using `express` module. Run `npm install express` to install express. You will be able to see it in the dependencies object inside the package.json file after the installation is complete. There will be 2 more additions in the folder called `node_modules` and `package-lock.json` file.

4. Now create a file called `index.js` in the root folder. Here we will import our express package and start setting up the server. We are following the "commonJS" format hence we will use **require** instead of **import**. If you want to use import instead, you must specify the `"type":"module"` in your package.json file. Alternatively, you can rename the file index.js to `index.mjs`.
![index file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxgds4hbsehgdna97i07.jpg)

5. Let's call the express function to create a server object. We can call this `app`. Now using this object, we will set up our REST API. This server will be listening to the requests made by the user on some port on our `localhost`.

6. The first is a very simple `GET` request mechanism. This will simply send a response to the user when the request is made to the URL path - `/`. We are also making this server listen to the port `3000`. Now we can run the express server by writing the command `node index.js` in the terminal. If everything works, it will print a simple message in the terminal "server is listening..".
![server running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmi4xxniusx3xfh65tqc.png)

7. We can use Postman to test the server. Postman is an API testing platform. You can go and create a new workspace or just use the default "My Workspace" to make API calls. We will create a collection called "node-demo" and then add a request.
![Postman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzbmect8i2ucnwrk03yu.jpg)

8. Since we are going to make requests to a server that is running on localhost, make sure that you use the Postman's Desktop Agent else you won't be able to make any requests. Now make a GET request to this URL: `http://localhost:3000` and click Send. You will see the response "Hello! I'm the server" below. This means that our server is working!!
![Postman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oifdik1qu0czw3c6h6qy.jpg)

9. Yay! We just made our first API request and it was successful. Let's move ahead. Now we can look into another type of request called POST.

10. We will now make a POST request where we will send some data. GET can be also used but it's less secure since **it doesn't provide data encryption** as POST does. Now before we make changes to the index.js file I want you to install another quite handy dependency. It's called `nodemon` and it automatically refreshes the server whenever there will be any change in the code. We will install it as a dev dependency since it's not meant for production. So run the command `npm install nodemon --save-dev` in the terminal. After this, we can create a script in the package.json file as `"dev":"nodemon index.js"`.
![package.json](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ccr9op7hhv1wfgt5qznn.jpg)

11. Below is the POST mechanism. Now make sure to use a middleware provided by express called "json" which is responsible for handling incoming requests with JSON payloads and is based on body-parser. Add this line to your index.js file: `app.use(express.json())`. Without it, your POST request would fail if you are trying to send some JSON as the body. Now, let's start the server again by running the script dev - `npm run dev` this time.
![server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0u5p5i860kguz28otcrj.jpg)

12. Let's make a POST request with Postman. This time we will have to send a body as well. We will send it as **raw -> JSON** type. After clicking Send you can see the response below.
![Postman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mc6jiikg40x4ovq57cp4.jpg)

13. Great! You have now learned 2 major request types: GET and POST. There are many other types as well like PUT, PATCH, DELETE, etc but for now, we will focus only on these 2. What about sending a GET request with some query params? For this, we will modify the same GET callback we defined earlier and tell it to look for the query object to send a response accordingly.
![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pawb7e5nu7usq3fukpk.jpg)

14. No make a GET request to the same URL in Postman but remember to pass query params. Set the key as "name" and the value as "Ananya". On clicking Send you should get the first response as defined in the callback.
![POstman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yvqhuvgnptnaftj7daqb.jpg)

15. Cool! Let's look into Path params now. What if you want to send some dynamic value to the URL maybe some kind of "id"? To access it on the server side we will have to do something like this:
![code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gao11u5mzaue3vhw5k1s.jpg)

16. Below is the Postman request with the Path variable "id".
![Postman](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4caawnwwdvvxb7lv5d4m.jpg)

Awesome! You just built your very first server with help of node.js and express. You also learned about Postman and how to test APIs with it. Hope this article helped you get a basic understanding of Node.js.

![gif](https://media.giphy.com/media/l4HodBpDmoMA5p9bG/giphy.gif)

You can subscribe to my newsletter to get notifications for my future blog posts! Till then keep learning..😊