Press "Enter" to skip to content

Deploy your REST API to Azure App Service

0

In the previous article Using K2 with NoSQL (MongoDB + dynamic REST API) I described how you can easily create a REST API to integrate K2 with a NoSQL database. However, in the demonstrated example you had to make a call to http://localhost:3000. This means that the application should be hosted somewhere locally, which might be a suitable approach when you use K2 Five. But it is definitely unacceptable, when using K2 Cloud. That is why I decided to explain to you, how you can quickly deploy your REST API to Azure App Service.

1. Create a web app in the Azure portal

  1. Sign into into Azure portal, using your account.
  2. Click the Create a resource link at the top of the left-hand navigation. Everything you create on Azure is a resource.
  3. Click Web > Web App. The portal redirects you to the Create New Web App page.
  4. When you create a new web app, the Azure portal requests some information to create the app for you. In this section, you can provide the following basic information:
    • App name: I named my app blog-k2-mongodb. This name should be unique, because it will become the part of url, which you can use to access your app.
    • Subscription: In this field, you need to select your subscription.
    • OS: In this field, you need to decide whether to use Windows or Linux to host your new web app. This setting directly affects the App Service plan that you are going to select or create below. I selected Linux, just to show that it does not matter for Node.js where to run.
    • Publish: You can choose to either publish code, or a Docker container image. I decided simply to publish the code.
    • Runtime Stack: We are using Node.js to run our REST API.
  5. Create or Select an App Service plan. By default, the portal selects the most recent App Service plan that you created. Click on the App Service plan/Location field to navigate to the App Service plan page. I decided to create a new Service Plan:
    • App Service plan: My name of the service plan is
      blog-k2-mongodb-app-service-plan .
    • Location: Select the geographical location where the App Service plan will set up the virtual machine(s) that are required to run your application. My choice is Northern Europe.
  6. Click OK to use your new service plan and press Create to create your app. After you will see in your resources, that the app and the new Service Plan have been created.

2. Deploy your code to App Service

Automated deployment, or continuous integration, is a process used to push out new features and bug fixes in a fast and repetitive pattern with minimal impact on end users. Azure supports automated deployment directly from several sources. The following options are available:

  • Visual Studio Team Services (VSTS): You can push your code to VSTS, build your code in the cloud, run the tests, generate a release from the code, and finally, push your code to an Azure Web App.
  • GitHub: Azure supports automated deployment directly from GitHub. When you connect your GitHub repository to Azure for automated deployment, any changes you push to your production branch on GitHub will be automatically deployed for you.
  • Bitbucket
  • OneDrive
  • Dropbox
  • External repository

My application is already on GitHub, that is why I will use that option. Here are the steps:

  1. Navigate to All Resources -> Your App (blog-k2-mongodb) -> Deployment Center
  2. Select GitHub as a Source Control. You will have to authorize and allow Azure to connect to your GitHub repositories
  3. In the Build Provide section I selected App Service Kudu build server.
  4. In the Configure section I selected:
    • Organization: dudelis
    • Repository: blog-k2-express-mongodb
    • Branch: master (I would recommend to create a separate branch for your azure deployment, so that the app is deployed only when you push your changes to that specific branch)
  5. Click Sync button to upload your application and run it. Now after every change of you application and pushing of the changes to the branch, your application will be automatically updated and redeployed.

3. Update swagger and run from K2

After you deployed your REST API to Azure App Service, the you can now use the new url. In my case this is https://blog-k2-mongodb.azurewebsites.net. In order to make K2 use another url, you need to make a minor change to the swagger file, which I created in the previous post, namely you need to change your host and add https into the schemas, because now the azure app is reachable via https.

{
"swagger": "2.0",
"info": {
"title": "K2 and MongoDB integration",
"description": "Sample file",
"version": "0.0.1"
},
"host": "blog-k2-mongodb.azurewebsites.net",
"schemes": ["http", "https"],
...

Refresh the Service Instance start testing.

You can also test the GET methods via browser. But for this you will have to enter login and password upon the browser request.

Browser test

You can find the code as well as the swagger file in my repo blog-k2-express-mongodb.

Feel free to ask any questions about how to Deploy your REST API to Azure App Services or any others. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

4 × 5 =