Use Azure Functions to develop Node.js serverless solutions
Azure Functions provides serverless code infrastructure, allowing you to create responsive, on-demand HTTP endpoints. Serverless apps are composed of JavaScript or TypeScript code that runs in response to various events.
Functions provide you with:
Abstraction of web service - you focus on code, not infrastructure.
Integration with Azure services - trigger work into or out of an Azure service with events
Integrate with JavaScript packages - use your favorite npm packages with your serverless code
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 bindings. 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.
Proxy from client app to the API
If you intend to deploy your API with your Static web app, you don't need to proxy your client application's API calls. The proxy is established for you, including local and remote development.
When developing locally with a Static Web App and Azure Functions, the Azure Static Web App CLI provides the local proxy.
Common security settings you need to configure for your Azure Function
The following common settings should be configured to keep your Azure Function secure:
- Configuration settings
- Configuration settings - create Application settings for settings that don't impact security.
- Secrets and keys - for any settings that impact security, create an Azure Key Vault and pull in those settings from your Key Vault.
- FTP state on Platform settings - by default, all are allowed. You need to select FTPS only or disable FTP entirely to improve security.
- API CORS - configure your client domains. Don't use
*
, indicating all domains. - TLS/SSL setting for HTTPS - by default, your API accepts HTTP and HTTPS requests. Enable HTTPS only in the TLS/SSL settings. Because your Function app is hosted on a secure subdomain, you can use it immediately (with
https
) and delay purchasing a domain name, and using a certificate for the domain until you're ready. - Deployment Slots - create a deployment slot, such as
stage
orpreflight
and push to that slot. Swap this stage slot to production when you're ready. Don't get in the habit of manually pushing to production. Your code base should be able to indicate the version or commit that is on a slot.
Prerequisites for developing Azure Functions
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.
import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
export async function status(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`Http function processed request for url "${request.url}"`);
return {
status: 200,
jsonBody: {
env: process.env
}
};
};
app.http('status', {
route: "status",
methods: ['GET'],
authLevel: 'anonymous',
handler: status
});
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.
Integrate with other Azure services
Serverless functions remove much of the server configuration and management so you can focus on just the code you need.
- Low-code functions: With Azure Functions, you can create functions that are triggered by other Azure services or that output to other Azure service using trigger bindings.
- High-code functions: For more control, use the Azure SDKs to coordinate and control other Azure services.
Next steps
Use the following table to learn more about Azure Functions with Node.js
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for