What is @azure/functions?
@azure/functions is an npm package that provides tools and libraries for building and deploying serverless functions on the Azure platform. It allows developers to create event-driven applications that can respond to various triggers such as HTTP requests, timers, and messages from other Azure services.
What are @azure/functions's main functionalities?
HTTP Trigger
This feature allows you to create a function that responds to HTTP requests. The code sample demonstrates a simple HTTP-triggered function that returns a greeting message.
module.exports = async function (context, req) {
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,
body: responseMessage
};
};
Timer Trigger
This feature allows you to create a function that runs on a schedule. The code sample demonstrates a timer-triggered function that logs the current timestamp.
module.exports = async function (context, myTimer) {
var timeStamp = new Date().toISOString();
if (myTimer.isPastDue) {
context.log('Timer function is running late!');
}
context.log('Timer trigger function ran!', timeStamp);
};
Queue Trigger
This feature allows you to create a function that responds to messages in an Azure Storage Queue. The code sample demonstrates a queue-triggered function that logs the message content.
module.exports = async function (context, myQueueItem) {
context.log('Queue trigger function processed work item', myQueueItem);
};
Other packages similar to @azure/functions
aws-lambda
aws-lambda is an npm package for building serverless functions on AWS Lambda. It provides similar functionalities to @azure/functions, such as handling HTTP requests, scheduled tasks, and processing messages from queues. However, it is designed specifically for the AWS ecosystem.
serverless
serverless is a framework-agnostic npm package that allows you to build and deploy serverless applications across multiple cloud providers, including AWS, Azure, and Google Cloud. It provides a unified experience for managing serverless functions, but requires additional configuration compared to provider-specific packages like @azure/functions.
Azure Functions Node.js Programming Model
Branch | Status | Support level | Node.js Versions |
---|
v4.x (default) | | GA | 20, 18 |
v3.x | | GA | 20, 18 |
Install
npm install @azure/functions
Documentation
Considerations
- The Node.js "programming model" shouldn't be confused with the Azure Functions "runtime".
- Programming model: Defines how you author your code and is specific to JavaScript and TypeScript.
- Runtime: Defines underlying behavior of Azure Functions and is shared across all languages.
- The programming model version is strictly tied to the version of the
@azure/functions
npm package, and is versioned independently of the runtime. Both the runtime and the programming model use "4" as their latest major version, but that is purely a coincidence. - You can't mix the v3 and v4 programming models in the same function app. As soon as you register one v4 function in your app, any v3 functions registered in function.json files are ignored.
Usage
TypeScript
import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
export async function httpTrigger1(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`Http function processed request for url "${request.url}"`);
const name = request.query.get('name') || await request.text() || 'world';
return { body: `Hello, ${name}!` };
};
app.http('httpTrigger1', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: httpTrigger1
});
JavaScript
const { app } = require('@azure/functions');
app.http('httpTrigger1', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: async (request, context) => {
context.log(`Http function processed request for url "${request.url}"`);
const name = request.query.get('name') || await request.text() || 'world';
return { body: `Hello, ${name}!` };
}
});