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 Framework
Branch | Status | Support level | Node.js Versions |
---|
v4.x | | Preview | 18 (preview) |
v3.x (default) | | GA (Recommended) | 18 (preview), 16, 14 |
Install
npm install @azure/functions
Usage
UPDATE: See here for important information on v4 of the Azure Functions Node.js Framework: https://aka.ms/AzFuncNodeV4
Prior to version 3.5.0, this package only contained TypeScript type definitions. Starting with version 3.5.0 it also contains the underlying Azure Functions Framework for Node.js. This framework package is included by default in v4.x of the Azure Functions runtime, meaning you do not need to include the package in your app. However, there may be cases where you want a specific version of the package, so you can override the default shipped in Azure with the below steps.
TypeScript:
For a full tutorial, see how to create your first TypeScript function.
- Specify a main entrypoint in your package.json
"main": "dist/src/index.js"
- Add the following code to your entrypoint file (e.g.
src/index.ts
):
import * as func from '@azure/functions';
func.setup();
IMPORTANT NOTE: If you only want this package for the TypeScript type definitions, you may list this package in the "devDependencies" section of your package.json. If you are overriding the default shipped in Azure as described above, the package must be listed in the production "dependencies" section of your package.json.
For more documentation, see the TypeScript developer guide.
JavaScript
For a full tutorial, see how to create your first JavaScript function.
- Specify a main entrypoint in your package.json
"main": "src/index.js"
- Add the following code to your entrypoint file:
const func = require('@azure/functions');
func.setup();
For more documentation, see the JavaScript developer guide.
Contributing
- Clone the repository locally and open in VS Code
- Run "Extensions: Show Recommended Extensions" from the command palette and install all extensions listed under "Workspace Recommendations"
- Run
npm install
- Run
npm run build
- Run
npm link
- Create or open a local function app to test with
- In the local function app:
- Make sure you are calling
func.setup()
somewhere in your app, as described above in the "Usage" section - Run
npm link @azure/functions
. This will point your app to the local repository for the framework package - Add the following settings to your "local.settings.json" file or configure them directly as environment variables
languageWorkers__node__arguments
: --inspect
💡 Tip: Set logging__logLevel__Worker
to debug
if you want to view worker-specific logs in the output of func start
- Start the app (i.e. run
func start
or press F5)
- Back in the framework repository, press F5 and select the process for your running function app
- Before you submit a PR, run
npm run lint
and npm test
and fix any issues. If you want to debug the tests, switch your launch profile in VS Code to "Launch Unit Tests" and press F5.
Code of Conduct
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Contributing to type definitions
The type definitions are located in the types
folder. Please make sure to update the tests in ./test/types/index.test.ts
as well.