DEV Community

Cover image for Creating Serverless REST API with Azure Functions using nodeJS - HTTP Trigger

Creating Serverless REST API with Azure Functions using nodeJS - HTTP Trigger

ramprakashram profile image Ram ・4 min read

Hello guys, in this article I am gonna briefly talk about creating serverless REST API with azure functions using nodeJS right from creating an azure account to deploying and consuming the API. I am not gonna talk much about serverless compute and its benefits, as I guess there are plenty of articles that talk in great length about it. So let's dive into it.

Microsoft Azure provides a lot of cloud computing services using which we can build some components of our application with ease and more flexibility. Like other cloud service providers, you can upscale and downscale your resources with a mere click of a button. I would strongly recommend you to use VSCode while working on Azure Functions, as it contains lots of extensions which will make our life easy. Azure functions can now be written in five Stacks:

  • .NET Core
  • Node.js (We are gonna use this to create functions☺️)
  • Python
  • Java
  • Powershell Core

Creating and configuring Functions in VSCode

First, create an account on Azure. Microsoft provides a free account to play with its services, try to make use of it, and understand the core concepts before using it in your product. Once you create your account in Azure, we can then directly use VSCode for all the other things. You need the following extensions in VSCode:

Now you need to sign in to your Azure account from VSCode, launch the command palette(Press Ctrl+Shift+P), and type Azure: Sign In and hit enter to log in.
Azure Login From VSCode

Once you complete your login, let us now create our first Azure Function by opening the command palette again and type Azure Functions: Create Function
Create Azure Function From VSCode Cmd Palette
This will then trigger a couple of prompts asking you to

  • Select your programming language (choose javascript)
  • Select your template (Choose HTTP Trigger, this makes your function to be a REST API)
  • Give a name for your function (I gave azure-http-test)
  • Select authorization level (Choose Function)

And then your function app is created.
Function App Created

Let us breakdown the folder structure we got:

  • package.json - Since we choose javascript as our programming language, this was created. We can now treat this whole app as a normal node application.
  • host.json - Default configuration for our app (Better leave as it is now).
  • local.settings.json - Environment variables when you run your app locally for development.
  • azure-http-test - This folder is a REST API endpoint and it contains two files:
    • function.json - This is the configuration file for this API which contains bindings where you can define the method(GET, POST...,) of your REST API and URL here.
    • index.js - This where you write your logic for your API.

Let us now modify our app to make it like a simple POST API by changing the methods to post and adding a route to bindings in function.json. This route will be the path for this endpoint. You can access the body sent through POST API from the req.body object in index.js. Incase of GET API, after defining the parameters in bindings array of function.json, you access it in index.js through context.bindingData.parameterName .

Change Bindings
You can run the app in debug mode by pressing F5. Once you run the app, the terminal will show you the local endpoint for testing.

Deploying Functions to Azure

We have successfully created a simple POST API using azure functions, now let's do the deployment into Azure. Open the command palette again and type Azure functions: Deploy to Function App.
Deploy Azure Function
This will then ask you to select your subscription, once you select your subscription, the next prompt will ask for selecting the function app, if you haven't created any function app, select Create new Function App in Azure (Advanced) . This will then trigger a couple of prompts:

  • Enter a unique name for your function app
  • Select a runtime stack (choose Node.js)
  • Choose an OS
  • Choose a hosting plan (Use App service plan for free plan)
  • Choose an OS Service plan
  • Choose a Resource Group (It's for grouping your services, you can use this for sharing resources or billing purposes)
  • Choose a Storage Account (It holds all your blobs, files, etc..,)
  • Choose an Application Insights Resource (It helps you in monitoring your services, you can skip it if you want).

Once you complete all the above fields, the deployment will start, it will take a few minutes and then you can head back to your Azure portal to see the deployed function.
Functions Page

Et voilà!, you have successfully created and deployed your first serverless API, you can get the endpoint for your function by going into that function's overview page and click Get Function Url. You can see the logs from the Monitor section and you can set your environment variables for the different environment from the azure portal itself, go to configuration of your function app, and set the environment variables in the Application settings tab.

Azure functions have many other templates, which we will see in my next articles.

Thanks for reading.

Discussion (0)

Editor guide