Azure Functions with Node.js

Getting Started with Azure Functions and Node.js

This tutorial will guide you through creating and deploying your first Azure Function using Node.js. We'll cover setting up your development environment, writing a simple HTTP-triggered function, and deploying it to Azure.

Prerequisites

Before you begin, ensure you have the following installed:

  • Node.js: Download and install from nodejs.org. We recommend using an LTS version.
  • Azure Functions Core Tools: Install globally using npm:
    npm install -g azure-functions-core-tools@4 --unsafe-perm true
  • Azure CLI: Install from Microsoft Docs.
  • A Code Editor: Such as Visual Studio Code.

Step 1: Create a New Project

1

Open your terminal or command prompt and navigate to the directory where you want to create your project. Then, run the following command to initialize a new Azure Functions project:

func init MyNodeJsFunctionApp --worker-runtime node

This command creates a new folder named MyNodeJsFunctionApp with the necessary project files.

Step 2: Create an HTTP Trigger Function

2

Navigate into your project directory and create a new HTTP-triggered function:

cd MyNodeJsFunctionApp
func new --template "HTTP trigger" --name MyHttpTrigger

This creates a new function named MyHttpTrigger with default code.

Step 3: Understand the Function Code

3

Open the MyHttpTrigger/index.js file in your code editor. You'll see code similar to this:

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 = {
        body: responseMessage
    };
};

This function:

  • Logs a message to the console.
  • Reads a name parameter from either the query string or the request body.
  • Constructs a response message.
  • Sets the response body and status code.

Step 4: Run the Function Locally

4

Start the Azure Functions host locally:

func start

The output will show the local URL for your function. Typically, it will be something like http://localhost:7071/api/MyHttpTrigger.

You can test it by opening this URL in your browser, or by using a tool like curl:

curl "http://localhost:7071/api/MyHttpTrigger?name=AzureFunctions"

You should see the personalized response.

Step 5: Deploy to Azure

5

First, log in to your Azure account using the Azure CLI:

az login

Then, deploy your function app:

func azure functionapp publish MyNodeJsFunctionApp --force

Replace MyNodeJsFunctionApp with the desired name for your function app in Azure. The --force flag will overwrite any existing function app with the same name.

Note: If this is your first time deploying, you might need to create a resource group and storage account. The Azure Functions Core Tools will prompt you for this or you can set these up beforehand using az group create and az storage account create.

After deployment, the URL for your function in Azure will be printed in the terminal output. It will be in the format https://your-function-app-name.azurewebsites.net/api/MyHttpTrigger.

Next Steps

Congratulations! You've successfully created and deployed an Azure Function with Node.js.

  • Explore other trigger types like Timer, Queue, and Blob.
  • Learn about binding expressions for seamless integration with other Azure services.
  • Read the official Azure Functions Node.js developer guide for more advanced topics.