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
    };
}