How to deploy serverless nodejs api on azure.

Azure has function app which allows you to create serverless APIs, which means you can build and deploy APIs without managing servers.

Azure Function apps are event-driven compute units that let you run code on demand without managing servers.
They can be triggred via HTTP requests, database changes, or queue messages.We can create rest api via http trigger function app. Basically its a exposed functon where we write the actual logic and configure it has http trigger based and we call it serverless api. Focus on writing the code that matters, in your preferred language, and Azure Functions handles the rest, scaling your application up or down based on usage for cost-effective execution.

This blog focus on devlopng and deployng a http trggered function app or serverless api at your local in vs code and then deploy on azure.

Approach: We wll create a function app locally and then create function app on cloud and then pushing or binding the local function to the one on cloud.

Step 1 : Prerequsite and Installations

  • An Azure account with an active subscription.

  • Node.js 18.x or above.

  • Visual Studio Code

  • The Azure Functions extension v1.10.4 or above for Visual Studio Code.

  • Azure Functions Core Tools

    Step 2 : Create a folder and open in vs code.

    Step 3: Login to azure from vs code

  • You can use azure cli or through extention that we installed above.
    use ctrl +shift P and seach sign in and signin to azure

    Step 4 : Create a function app template locally via
    ctrl + shift followed by P. Select create new project. And it will ask the folder where to create. What language to be used . Which model to be used.Currently Nodejs has 2 models v3 and v4. you can choose anyone.Both are different in structure and routing sense.

    Select the folder

Select the programming language.

Select the model . We selecting Model V3 to keep it simpler.

Select the type of trigger whch is the most important here . There are many kind of triggers . since we want to create api so we will be choosing http triggered

Then it will ask the name of the api . which will become your end point. In model V3 . you can create many such function inside the folder and then these all can be binded to one function app on cloud and these folder will have separate funciton inside and and the name of the folder will a end point.

Afterasking the name , it will ask for Authorization level. so if you want you api to be called from a web app or publically available to be called then choose anonymous. if you want you api should be called by another function app only then choose function or if you want your api should be able to be called only via admin then choose admin. here I want my api to be publically accessible so I am choosing annonymous.

After the authorization level selection. It will create a template for you whcih looks like

Step 5: You can write your logic make changes in this template . So I am trying to return a json object with 200 status code and if something gone wrong then api will be responding with 500 status code with the error encountered.

  • Step 6: We wil run the code locally . So if you have installed azure core tools and extentions then your terminal will have access to func.
    so we can start our project locally by func start.
    func start

    By default our api will run on port 7071 . you can run on any other port via
    func start -p 7072Our Api will be up and running and look like below. It will console the endpoint url to the function app. you can hit ths url in postman and get the response.

    Step 7: Now time to create a function app on cloud to which this local function will be binded.

  • Go to the azure platform. Seach function app in the search bar and the search result. Your screen will look like below.

Click on Create

Type a function app name. it can be same or different than your local folder. this name will be addup to your default domain . it wont be you endpoint. endpoint will be the folder names that we created locally.
Then select techstack whch should be the same as we chose in local. Region. Hosting plans as serverless

Click on next and leave the default selection

Select another next and keep public access enabled

Keep monitoring settings default and go to next and make the deployment selection.

Keep default selection for tag and go to reveiw + create

After create you will see a screen like this

After completion which can go to resource and explore .

Now its time to bind our local function app to this function app.

Step 8: Right click your api folder and your see a options like this

Select Deploy to funciton app.This will bring a list of all fucntion app that you have created on your cloud platform. you have to select where you want to bind this funciton app.

After selection It will deploy this local function app to cloud and console a coud url that you can hit in postman.