UNPKG

3.88 kBMarkdownView Raw
1# Azure Functions Node.js Programming Model
2
3|Branch|Status|Support level|Node.js Versions|
4|---|---|---|---|
5|v4.x (default)|[![Build Status](https://img.shields.io/azure-devops/build/azfunc/Azure%2520Functions/145/v4.x)](https://azfunc.visualstudio.com/Azure%20Functions/_build/latest?definitionId=145&branchName=v4.x) [![Test Status](https://img.shields.io/azure-devops/tests/azfunc/Azure%2520Functions/146/v4.x?compact_message)](https://azfunc.visualstudio.com/Azure%20Functions/_build/latest?definitionId=146&branchName=v4.x)|GA (Recommended)|20 (Preview), 18|
6|v3.x|[![Build Status](https://img.shields.io/azure-devops/build/azfunc/Azure%2520Functions/145/v3.x)](https://azfunc.visualstudio.com/Azure%20Functions/_build/latest?definitionId=145&branchName=v3.x) [![Test Status](https://img.shields.io/azure-devops/tests/azfunc/Azure%2520Functions/146/v3.x?compact_message)](https://azfunc.visualstudio.com/Azure%20Functions/_build/latest?definitionId=146&branchName=v3.x)|GA|20 (Preview), 18, 16, 14|
7
8> _**Version 4 is Generally Available! 🎉✨ Read our [blog post](https://aka.ms/AzFuncNodeV4) and let us know what you think by reacting or commenting on our [GA discussion thread](https://aka.ms/AzFuncNodeV4Discussion)**_
9
10## Install
11
12```bash
13npm install @azure/functions
14```
15
16## Documentation
17
18- [Azure Functions JavaScript Developer Guide](https://learn.microsoft.com/azure/azure-functions/functions-reference-node?pivots=nodejs-model-v4)
19- [Upgrade guide from v3 to v4](https://learn.microsoft.com/azure/azure-functions/functions-node-upgrade-v4)
20- [Create your first TypeScript function](https://docs.microsoft.com/azure/azure-functions/create-first-function-vs-code-typescript?pivots=nodejs-model-v4)
21- [Create your first JavaScript function](https://docs.microsoft.com/azure/azure-functions/create-first-function-vs-code-node?pivots=nodejs-model-v4)
22
23## Considerations
24
25- The Node.js "programming model" shouldn't be confused with the Azure Functions "runtime".
26 - _**Programming model**_: Defines how you author your code and is specific to JavaScript and TypeScript.
27 - _**Runtime**_: Defines underlying behavior of Azure Functions and is shared across all languages.
28- The programming model version is strictly tied to the version of the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions) npm package, and is versioned independently of the [runtime](https://learn.microsoft.com/azure/azure-functions/functions-versions?pivots=programming-language-javascript). Both the runtime and the programming model use "4" as their latest major version, but that is purely a coincidence.
29- 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.
30
31## Usage
32
33### TypeScript
34
35```typescript
36import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
37
38export async function httpTrigger1(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
39 context.log(`Http function processed request for url "${request.url}"`);
40
41 const name = request.query.get('name') || await request.text() || 'world';
42
43 return { body: `Hello, ${name}!` };
44};
45
46app.http('httpTrigger1', {
47 methods: ['GET', 'POST'],
48 authLevel: 'anonymous',
49 handler: httpTrigger1
50});
51```
52
53### JavaScript
54
55```javascript
56const { app } = require('@azure/functions');
57
58app.http('httpTrigger1', {
59 methods: ['GET', 'POST'],
60 authLevel: 'anonymous',
61 handler: async (request, context) => {
62 context.log(`Http function processed request for url "${request.url}"`);
63
64 const name = request.query.get('name') || await request.text() || 'world';
65
66 return { body: `Hello, ${name}!` };
67 }
68});
69```