Step-by-Step Guide: Deploying a Node.js REST API on Azure Cloud

Are you looking to deploy your Node.js API in Azure? Azure app service is a powerful platform for hosting your web application in Microsoft Azure cloud. You may speed up the process of delivering your Node.js Express application to Microsft Azure by combining it with GitHub for version control and continuous deployment. We will walk you through the steps to achieve this seamless deployment procedure in this article.

Prerequisites

Before you begin, make sure you have the following in place:

  1. Node.js Installed: Ensure that you have Node.js and npm(node package manager) installed on your local development machine. You can download and install Node.js from official website: Node.js Download
  2. GitHub Account: You need a GitHub account to host your application source code.
  3. Azure Account: Sign up for an Azure account if you donā€™t already have one. You can get started with a free account, which includes a limited amount of resources.

Step 1: Create a Simple Node.js Express API

Letā€™s start by creating a simple Node.js Express API. If you already have an API, you can skip this step. Otherwise, follow these instructions:

  1. Create a new directory for your API project.
  2. Open your terminal in the project directory and run the following commands to initialize your project and install Express:
mkdir my-express-api
cd my-express-api
npm init -y
npm install express --save

3. Create a JavaScript file (e.g., app.js ) and set up a basic Express server. Here’s a simple example in JavaScript:

const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

app.get('/', (req, res) => {
res.send('Hello, World!');
});

app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});

3. Update the package.json with the following script.

"scripts": {
"start":"node app.js"
},

4. Test your API locally by running npm start. You should see “Server is running on port 3000” in the console.

5. Also test your API locally, you can use the curl command-line tool. Open a new terminal window and run the following curl command to make a GET request to your API:

curl http://localhost:3000

This command sends a GET request to your locally running Express API. You should receive the response ā€œHello, World!ā€ from your API.

Testing your API locally with curl allows you to ensure that it’s functioning correctly before deploying it to the Azure app service. With these additional instructions, you can verify that your locally hosted Express API responds as expected using the curl command-line tool.

Step 2: Create a GitHub Repository

If you havenā€™t already, create a GitHub repository to host your Node.js Express APIā€™s source code. Follow these steps:

  1. Sign in to GitHub.
  2. Click the ā€œ+ā€ sign in the upper right corner.
  3. Select ā€œNew repository.ā€
  4. Follow the instructions to create a new repository.

Step 3: Push Your Code to GitHub

With your GitHub repository set up, itā€™s time to push your Node.js Express APIā€™s code to it. Open your terminal and navigate to your projectā€™s root directory. Use these commands to initialize your Git repository, add your files, and push to GitHub:

git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin <your-github-repo-url>
git push -u origin main

Replace <your-github-repo-url> with the URL of your GitHub repository.

Step 4: Set Up an Azure App Services

Now, letā€™s create an Azure App service to host your Node.js Express API:

  1. Sign in to the Azure portal (https://portal.azure.com).
  2. Search App services on the Azure portal click on Create and select Web App from the dropdown as follows.
Deploying a Node.js Express API to Azure Web App with GitHub
Figure 1.2

3. Create a new Web App, providing a unique name, subscription, resource group, and other necessary details as follows. Select the Runtime stack as Node 18 LTS.

Deploying a Node.js Express API to Azure Web App with GitHub
Figure 1.3

4. Then, choose the pricing plan that is best for you. Because this is for educational purposes, Iā€™ve chosen a free-price plan. You can find a suitable pricing plan by clicking on theExplore pricing plans

Deploying a Node.js Express API to Azure Web App with GitHub
Figure 1.4

5. When you finish these steps, click the Review + Create button, and the web app with the name you entered will be created. The web app will take some time to complete the deployment.

6. After the deployment is complete, navigate to your Web app service by clicking on the service name you specified.

Deploying a Node.js Express API to Azure Web App with GitHub
Figure 1.5

7. After that you will able to the the Web App service overview page for your project. Next Search Deployment Center From the sidebar click on that to configure the Azure continuous deployments.

Deploying a Node.js Express API to Azure Web App with GitHub
Figure 1.6

Step 5: Configure Azure Deployment Center

  1. On that go to Settings tab and select the Source as GitHub as follows.
Deploying a Node.js Express API to Azure Web App with GitHub
Figure 1.7

2. After that you have to link your GitHub account with your Azure account by following the necessary steps.

3. Next, youā€™ll need to choose the repository where your Node Express API source code is hosted and specify the branch for setting up the CI/CD pipeline as follows.

Deploying a Node.js Express API to Azure Web App with GitHub
Figure 1.8

4. When all the steps are done click on the save button. This will create a GitHub action on your repository. And deploy your Node.js app on Azure Web app services.

Deploying a Node.js Express API to Azure Web App with GitHub
Figure 1.9

5. Azure will automatically trigger a deployment whenever you push changes to the specified branch (master) in your GitHub repository.

Step 6: Complete the Configuration

When the deployment is finished, Search Configuration from the sidebar and click on that to configure the startup command.

Then go to the general Settings tab and enter the Startup command to start our Node.js application. After that click on the Save button. This will start the Node.js application.

Deploying a Node.js Express API to Azure Web App with GitHub
Figure 1.10

Step 7: Access Your API

After a successful deployment, your Node.js Express API should be accessible through the Azure Web Appā€™s URL. You can find this URL in the Azure portalā€™s overview page for your Web App.

Deploying a Node.js Express API to Azure Web App with GitHub
Figure 1.11

You can test your Node app by using the curl command in your terminal.

Figure 1.12

Step 8: Configure Custom Domains (Optional)

If you have a custom domain for your API, you can configure it to point to your Azure Web App. This step involves updating your DNS settings in your domain registrarā€™s control panel.

Conclusion

Congratulations! You have successfully deployed your Node Express API in Azure App Service with GitHub. Using GitHub for version control and continuous deployment to deploy a simple Node.js Express API to Azure Web App streamlines the deployment process, making it easy to manage and update your API. You can efficiently host and maintain your API on the cloud if you follow these procedures.

Remember to treat any Azure environment-specific configurations (e.g., database connection strings) correctly. With this setup in place, you can concentrate on designing your API while Azure handles the deployment.

Resources

Example Node API Code Repository –https://github.com/Prasadkpd/Sample_API


How to deploy a Node Express API on Azure Web App with GitHub was originally published in Enlear Academy on Medium, where people are continuing the conversation by highlighting and responding to this story.

Author

Tech enthusiast, passionate about exploring opportunities to learn, teach, help, and take experiences. šŸŒ

Write A Comment