How To Create Node.js Azure Functions

Recently, I worked on a requirement that required me to create a Node.js Azure Function. In this Azure tutorial, I  will discuss how to create Node.js Azure Functions with an excellent Example.

How To Create Node.js Azure Functions? We can create the Node.js Azure Functions using the Azure Portal along with the Visual Studio Code IDE.

How To Create Node.js Azure Functions

As part of this functionality, we will create Azure Functions using Node.js and use the IDE as Visual Studio Code for our development activities.

1. Log in to Azure Portal (https://portal.azure.com/)

2. From the Home page, click on + Create a resource from the New Window, and then click “Compute”. Now, choose the “Function App”.

azure function nodejs

3. Provide the details below on the Create Function App window on the Basics tab.

  • Choose your Correct Subscription.
  • Resource Group: Choose your Existing Resource Group, or click the Create new link to create a new one if you don’t have one created already.
  • Function App name: You need to provide a valid Function App name.
  • Publish: You can choose the Code option.
  • Runtime stack: Choose the Node.js Option here.
  • Version: You can choose the latest version
  • Region: Select the Region for your Function App.

Click on the Next: Hosting > button now.

How To Create Node.js Azure Functions

4. On the Hosting tab, click on the Create New link to choose an existing storage account or create a new one.

Next, select the operating system as Windows and the Plan type as Consumption (Serverless).

Keep the other configuration as it is, and then, finally, click on the Review + Create button.

node.js azure function example

5. Now, it will validate the details for all the fields. Then, click the Create button in the next window to create the Azure Function App.

The deployment has been completed successfully. Click on the Go to resource button to see the function app we created.

node js azure function

Now, the Azure function App is created successfully.

node.js azure function

Our Function App is now ready; this is the time to create the Azure function.

Here, there are two approaches to create the Azure Function

First Approach

To create the Azure function, click on the Functions link from the left navigation and click the + Add button on the Azure Function App page.

azure function nodejs example

Now, we need to choose the trigger for the Azure Function. Choose the HTTP trigger template from the list of templates. Of course, you can choose any of the triggers based on your choice based on your requirement. But for this example, I am choosing the HTTP trigger.

azure function javascript example

Now, provide a name for the New Function, choose the Authorization level as Function on the New Function window, and finally, click the Create Function button to create the Azure function.

azure function node js

Now you can able to see the Azure function created successfully.

azure function node js

Now, you can click on the Code + Test link to see if the Azure function is working fine. You can see the File created and your function code. This is the index.js file code, the entry point to the Azure function.

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseMessage
    };
}
azure functions node js

Below is the Function.json code. This file will contain all the configurations for the Azure function.

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
azure functions node js

If you want to test the function, click the Test/Run button at the top to see if it works. Then select the HTTP method, provide the name value in the function’s body, and click the Run button.

azure function node

Below is the expected output with the proper response code 200 OK.

node azure function

Second Approach

How To Create Node.js Azure Functions using Visual Studio Code

Let’s discuss the second approach to creating the Azure function, i.e., using the Visual Studio Code IDE.

On the Azure Function App page, click the Switch to Classic Experience drop-down and select the Continue to Classic Experience button from the top navigation.

node js azure function

Click the + New function button, as highlighted below, to create the Azure function.

azure function node.js

Select VS Code as the development tool for the Azure function and then click on the Continue button.

azure functions node.js

Now select the Direct publish option and then click on the Continue button.

run node js azure function locally

As mentioned below, if you didn’t install all this stuff, then you can install Visual Studio Code, Node.js, .NET Core 2.1, and Azure Functions Core Tools, Azure Functions extensions for Visual Studio Code, as mentioned below, and then click on the Finish button.

azure function nodejs

Once you click on the Install the Azure Functions extension for Visual Studio Code link, a pop-up with the message “Visual Studio Code is required to install this extension” will appear. Click on the Continue button.

azure functions javascript

Click on the Open Visual Studio Code button to open Visual Studio Code.

azure functions node.js example c#

Now, it will open Visual Studio from the local machine with the below pop-up. Since I have already installed the Azure Functions Extensions, it is showing the Uninstall button for me. Otherwise, it will show you the Install button to install that in your Visual Studio Code in the below popup.

azure functions node

Now, click on the Azure button on the left in Visual Studio Code. It will prompt you to enter your Azure login Credentials. Assuming you are already logged in, now follow the steps below.

Click on the Azure button from the left navigation and then click on the Create Function button as highlighted below.

nodejs azure functions

Then, it will ask you to choose a project location. After selecting the project location, select JavaScript from the pop-up below

azure functions node.js

Now, you need to choose the trigger for your Azure Function. Select the HTTP trigger option here.

azure function javascript

Now, provide a name for your Azure Function.

azure functions node

Select the Authorization Level as a Function

azure function npm install

Now, if you are able to see the Azure Function Created successfully.

Below is the Code for the index.js file.

Create Node js Azure Function using ViSual studio Code

Here is the code for the function.json file. This is a very important file, and it includes all the configurations needed for your Azure Function.

node.js azure function

Test Node.js Azure Function Locally

Now, the Azure Function is created. To make sure the function is working as expected, we can test it locally. Press F5 to run the function. Now, you can see the Azure Function project ran successfully, providing us with the Azure Function URL below.

http://localhost:7071/api/MyNewAzureFunctionVS

How to test the Nodejs Azure Function Locally

Now, Open Your Favorite browser and paste the above URL. You can see we got the expected output. We tried executing the URL below with the name value as a query string parameter. http://localhost:7071/api/MyNewAzureFunctionVS?name=Sakti

azure functions nodejs example

Deploy Node js Azure Function to Azure from Visual Studio Code

Now, to deploy the Node js Azure Function to Azure, you need to follow the below steps

You can click on the Deploy To function App button as highlighted below.

How to Deploy Node js Azure Function

Or else, for the same option, right-click on the Function name —-> Click on the Deploy to Function App option as highlighted below.

How to Deploy Node js Azure Function using Visual Studio Code

Now select the Azure Function App you created in the Azure Portal. You can search with your Azure Function App name and then select that.

Deploy the Node.Js Azure Function to Azure

The next step is to click on the Deploy button in the pop-up below

How to Deploy the Node.Js Azure Function to Azure

Now, it successfully deployed the Azure Function to the selected Azure Function App.

How to Deploy the Node.Js Azure Function to Azure Portal

Run Node js Azure Function From Package Configuration

After deploying the Azure function, you can click on the Website Run from Package option in the Application settings, as highlighted. This creates a zip file with all the necessary code every time it’s getting deployed. This is one of the best options that improve the performance of the Node.js Azure Function.

Run Node js Azure Function From Package Configuration

There are multiple ways to create Azure Function. You can check out Create Azure Function using Visual Studio Code and PowerShell, Creating AzureFunction using Azure Portal, and How To Create Azure Functions In Visual Studio for more information.

Wrapping Up

Well, in this article, we have discussed how to create Node.js Azure FunctionsHow To Create Node.js Azure Functions using Visual Studio Code, and how to Deploy The Node.js Azure Function to Azure from Visual Studio Code. I hope you have enjoyed this Article!!!

Azure Virtual Machine

DOWNLOAD FREE AZURE VIRTUAL MACHINE PDF

Download our free 25+ page Azure Virtual Machine guide and master cloud deployment today!