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.
Type definitions for Azure Functions
This package contains type definitions for using TypeScript with Azure Functions.
These typings are for common objects that will be passed to your Azure Functions function code. Azure Functions supports TypeScript development, but does not support directly running TypeScript code without transpilation.
Read more on configuring entry points in your Azure Functions function app.
Install
Because this package only contains TypeScript type definitions, it should be saved under devDependencies
.
npm install @azure/functions --save-dev
Usage
import { AzureFunction, Context, HttpRequest } from "@azure/functions";
const index: AzureFunction = async function (context: Context, req: HttpRequest) {
context.log('JavaScript HTTP trigger function processed a request.');
if (req.query.name || (req.body && req.body.name)) {
context.res = {
status: "200",
body: "Hello " + (req.query.name || req.body.name)
};
} else {
context.res = {
status: 400,
body: "Please pass a name on the query string or in the request body"
};
}
}
export { index };
Versions
Versioning of @azure/functions is tied to the version of the Azure Functions Node.js Worker the types were generated from. You can find the Azure Functions Node.js Worker version of a given Function Runtime Version here. It is recommended that you take the latest version available.
Getting Started with Azure Functions
If you are getting started with Azure Functions, you can follow this tutorial to create and deploy your first JavaScript function. We recommend that you use Visual Studio Code and the Azure Functions extension.
The Azure Functions developer guide and the JavaScript-specific developer guide are good resources to gain an understanding of more Azure Functions concepts.