Use Azure Functions to develop Node.js serverless code

Azure Functions provides serverless code infrastructure, allowing you to create responsive, on-demand HTTP endpoints. Serverless code is composed of JavaScript or TypeScript code that runs in response to various events.

Functions run on top of a web service, as code or a Docker container, which is abstracted away so you can focus on the code for your endpoint. Functions also allow you to trigger another function so that a function work stream can replace existing hosted backend server functionality and remove the need to manage that server.

What is a Function resource?

An Azure Function resource is a logical unit for all related functions in a single Azure geographic location. The resource can contain a single function or many functions, which can be independent of each other or related with input or output triggers. You can select from many common functions or create your own.

You can select from many common functions or create your own..

The function resource settings include typical serverless configurations including environment variables, authentication, logging, and CORS.

Durable, stateful functions

Durable Functions retain state, or manage long-running functions in Azure. Create your first durable function in JavaScript.

Static web apps include functions

When developing a static front-end client application (such as Angular, React, or Vue), which also need serverless APIs, use Static Web apps with functions to bundle both together.

A simple JavaScript function for HTTP requests

A function is an exported asynchronous function with request and context information. The following partial screenshot from the Azure portal shows the function code.

Partial screenshot of Azure Function in Azure portal.

Develop functions locally with Visual Studio Code and extensions

Create your first function using Visual Studio Code. Visual Studio Code, simplifies many of the details with the Azure Functions extension.

This extension helps you create JavaScript and TypeScript functions with common templates.

A JavaScript example of an HTTP function for Azure is:

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

A TypeScript example of an HTTP function for Azure is:

import { AzureFunction, Context, HttpRequest } from "@azure/functions"

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    context.log('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
    };

};

export default httpTrigger;

Configuring the function

Functions are configured with the function.json file. This configuration allows you to configure how the function is triggered ("direction": in) and what the function returns ("direction": out). It also allows you to set environment variables, and other necessary information for the function to work. Learn more about the trigger and binding.

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

Develop functions remotely using the Azure portal

When you create an Azure function using the Azure portal, you can configure the function, write the code inside a pre-populated template, and test the function.

The portal creates JavaScript functions only, not TypeScript. If you want to develop with TypeScript, either download the function or create the function locally in Visual Studio Code with the Function extension.

Next steps

The Azure Functions developer guide for JavaScript) in a good starting point.

Use the Microsoft Learn Module to learn how to enable automatic updates in a web app using Azure functions and SignalR Service.