How to Improve API Development Skills — Step 1 Build a REST API

Denis Setianto
bhinneka-tech
Published in
4 min readMar 10, 2022

Build a REST API

Let’s break down the process of building a REST API from scratch using Node.js and Express.

We are assuming you have Node.js installed on your machine. If you haven’t installed it, click on the following link and install it simply.

Let’s start; create an empty directory and initialize your project by running the following command.

The next step is to create an empty JavaScript file (File name could be anything, in this case, it’s index.js) and install Express.

🔗 https://npmjs.com/package/express

Express is a minimal and easy-to-learn framework for Node.

Now that everything is ready, it’s time to code our actual API. Let’s start the server first. In the below code snippet, we are initializing our `app` and starting the local server at port 3000. Run `node index.js` to start the server.

For more context, listen() function is used to establish the connection on specified host and port. It takes two params:

— The first is the port number or host.

— The second (optional) is a callback function that runs after listening to a specified host or value.

Moving forward, let’s try to access “localhost:3000” in the browser and see what we are getting. We are getting a “404 Not Found” response which is correct as we haven’t defined any endpoints yet.

It’s not recommended that you use the browser to build or debug APIs due to security reasons. We prefer the Paw, a full-featured HTTP client. Go ahead and download it.

🔗 https://Paw.cloud

As now our server is running successfully at port 3000, let’s create an endpoint.

The `get` method allows us to create HTTP GET requests.

It accepts two params:

— The first is path/route.

— The second is a callback function that handles the request to the specified route.

The callback function itself accepts two arguments:

— The first is the request which is the data to the server.

— The second is the response which is the data to the client.

Suppose you want to display all the users whenever the client requests the “/users” endpoint.

To return the data from the server to the client, we have the `send` method.

In the code snippet below, we send an array of objects with `name` and `id` fields.

Perfect!

Let’s restart the server by running the `node index.js` command and see what we are getting in the Paw.

You can make as many endpoints as you want using the same technique.

For demonstration purpose, let’s quickly create a POST request endpoint.

As POST request is to create or add new data to the server. We first need to add middleware so that we can parse JSON body.

We are defining a POST endpoint at the “/user/3” route.

We implemented the logic of throwing a “400 Bad Request” status code if the user forgets to pass the name value in the request body.

Let’s try to access this endpoint now.

As you can see, we are getting a response.

Great! You can add as many endpoints as want.

Thankyou~

--

--