cover-img

Step-by-step tutorial to create a backend server and deploy it

In this blog we will learn how to create REST APIs in Node.js and then deploy them. #develevate

23 November, 2022

12

12

1

🌐 Understanding how the web works

Let's say you visit a website eg. showwcase.com in your browser. You will notice that it takes a couple of seconds for the actual content to load. What's happening under the hood?
The blogs, comments, and images you see being rendered are on a remote server. So, on visiting the URL, it first requests those resources. How? Through APIs.
API stands for Application Programming Interface.
Sounds complicated? Let me make it easy for you.
Let's say you visit a restaurant with your friends. Whom do you call to order the food? The waiter. So the waiter takes your order and then brings the food to the table after a few minutes. But, did the waiter cook the food? No, there are cooks who do that. The waiter serves as a middleman between you and the cook.
img

Waiter serving as a middleman between you and cook

This is exactly what the API does. But instead of you and the cook, it's the front end (i.e the website) and the back end (i.e the server).
img

Sending requests to the server and getting response from the server

✍️ Let's write our first APIs

In this tutorial, we will be building REST APIs (RESTful APIs).
REST refers to certain practices and constraints, and not standards or protocols.
One thing to remember about REST APIs is that they are stateless. Hence, there will be no state stored between two API calls, all the API calls are unrelated.
One of the easiest ways to write APIs is to use the Express.js framework.

Initialize the project

Create a directory where you want to put the source code. To initialize a node project in a directory run the command:
It will create a package.json file in the directory.
Now, we will install some dependencies:
We already discussed Express. We also need cors to allow cross-origin requests.
Now, open package.json and add a script called start.
Also, create a file index.js where we will have our core logic.

Implementing API Endpoints

Open the index.js file and paste this:
Going through the code:

1.

We are importing the libraries we installed.

2.

Creating a new app using the express() method.

3.

Now, to receive the request and response as JSON we need to specify that with app.use(express.json())

4.

Then, we implement our first API endpoint "GET /api". GET is one of the several HTTP Methods. Other HTTP methods are POST, PUT, DELETE , etc.

5.

We add another endpoint "/" and just respond with a 200 status for health check in Render.

6.

In the app.get() method, we also pass a callback function which will have the actions that we want to perform when the request is made by the front end. In our case, we will simply return with the message "Hello World!".

7.

Finally, we make the server listen on port 8000.
Now, in your terminal run the command:
It should show something like this:
img

output of npm run start in the terminal

Now visit the URL localhost:8000/api in the browser and you will see the response of your API.
img

Hello World on visiting the url localhost:8000/api

🚀 Deploying your application

Congratulations !! for coming this far. Now you might want to deploy your application so that anyone can your API. To do that we will need some services provided by platforms like Render.
Visit the following website and then create an account and connect your GitHub or GitLab whichever you use.
Before being able to deploy you need to upload your code on GitHub or GitLab.
Now visit the render dashboard and select "Web Services"
img

Selecting Web Services in render dashboard

After that select your repository from the list
img

Selecting your project from the list

After that fill in the details:

Name: choose any name you want to identify your project.

Build Command: set it to yarn.
Leave the rest of the values as it is. Click the "Create Web Service" button at the very bottom.
Now, wait for a few minutes for it to build and start your server.
img

Deployed Server

Once it shows click on the link and add /api and there you go:
img

Live server deployed

Congratulations 🎉 !! You have successfully launched your backend server.
At render, they perform health checks on the endpoint '/', that's we added the extra route of / in the index.js file so that the health check returns a 200 status.
Hope you liked this blog of mine, do checkout my profile for more blogs that I have written on various new technologies, and open source. Thank you and have a nice day !!

tutorials

tutorial

develevate

12

12

1

tutorials

tutorial

develevate

Arnab Sen
Final year undergrad pursuing B.Tech in CSE and passionate about Software Development.

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.