Skip to content

Commit 7d33a68

Browse files
Merge pull request #263477 from ejizba/ej/hooks
Add Node.js Azure Functions hook docs
2 parents af1c8ba + 584118d commit 7d33a68

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

articles/azure-functions/functions-reference-node.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,86 @@ The response can be set in several ways:
14221422
14231423
::: zone-end
14241424
1425+
## Hooks
1426+
1427+
::: zone pivot="nodejs-model-v3"
1428+
1429+
Hooks aren't supported in the v3 model. [Upgrade to the v4 model](./functions-node-upgrade-v4.md) to use hooks.
1430+
1431+
::: zone-end
1432+
::: zone pivot="nodejs-model-v4"
1433+
1434+
Use a hook to execute code at different points in the Azure Functions lifecycle. Hooks are executed in the order they're registered and can be registered from any file in your app. There are currently two scopes of hooks, "app" level and "invocation" level.
1435+
1436+
### Invocation hooks
1437+
1438+
Invocation hooks are executed once per invocation of your function, either before in a `preInvocation` hook or after in a `postInvocation` hook. By default your hook executes for all trigger types, but you can also filter by type. The following example shows how to register an invocation hook and filter by trigger type:
1439+
1440+
# [JavaScript](#tab/javascript)
1441+
1442+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/invocationHooks1.js" :::
1443+
1444+
# [TypeScript](#tab/typescript)
1445+
1446+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/invocationHooks1.ts" :::
1447+
1448+
---
1449+
1450+
The first argument to the hook handler is a context object specific to that hook type.
1451+
1452+
The `PreInvocationContext` object has the following properties:
1453+
1454+
| Property | Description |
1455+
| --- | --- |
1456+
| **`inputs`** | The arguments passed to the invocation. |
1457+
| **`functionHandler`** | The function handler for the invocation. Changes to this value affect the function itself. |
1458+
| **`invocationContext`** | The [invocation context](#invocation-context) object passed to the function. |
1459+
| **`hookData`** | The recommended place to store and share data between hooks in the same scope. You should use a unique property name so that it doesn't conflict with other hooks' data. |
1460+
1461+
The `PostInvocationContext` object has the following properties:
1462+
1463+
| Property | Description |
1464+
| --- | --- |
1465+
| **`inputs`** | The arguments passed to the invocation. |
1466+
| **`result`** | The result of the function. Changes to this value affect the overall result of the function. |
1467+
| **`error`** | The error thrown by the function, or null/undefined if there's no error. Changes to this value affect the overall result of the function. |
1468+
| **`invocationContext`** | The [invocation context](#invocation-context) object passed to the function. |
1469+
| **`hookData`** | The recommended place to store and share data between hooks in the same scope. You should use a unique property name so that it doesn't conflict with other hooks' data. |
1470+
1471+
### App hooks
1472+
1473+
App hooks are executed once per instance of your app, either during startup in an `appStart` hook or during termination in an `appTerminate` hook. App terminate hooks have a limited time to execute and don't execute in all scenarios.
1474+
1475+
The Azure Functions runtime currently [doesn't support](https://github.com/Azure/azure-functions-host/issues/8222) context logging outside of an invocation. Use the Application Insights [npm package](https://www.npmjs.com/package/applicationinsights) to log data during app level hooks.
1476+
1477+
The following example registers app hooks:
1478+
1479+
# [JavaScript](#tab/javascript)
1480+
1481+
:::code language="javascript" source="~/azure-functions-nodejs-v4/js/src/appHooks1.js" :::
1482+
1483+
# [TypeScript](#tab/typescript)
1484+
1485+
:::code language="typescript" source="~/azure-functions-nodejs-v4/ts/src/appHooks1.ts" :::
1486+
1487+
---
1488+
1489+
The first argument to the hook handler is a context object specific to that hook type.
1490+
1491+
The `AppStartContext` object has the following properties:
1492+
1493+
| Property | Description |
1494+
| --- | --- |
1495+
| **`hookData`** | The recommended place to store and share data between hooks in the same scope. You should use a unique property name so that it doesn't conflict with other hooks' data. |
1496+
1497+
The `AppTerminateContext` object has the following properties:
1498+
1499+
| Property | Description |
1500+
| --- | --- |
1501+
| **`hookData`** | The recommended place to store and share data between hooks in the same scope. You should use a unique property name so that it doesn't conflict with other hooks' data. |
1502+
1503+
::: zone-end
1504+
14251505
## Scaling and concurrency
14261506
14271507
By default, Azure Functions automatically monitors the load on your application and creates more host instances for Node.js as needed. Azure Functions uses built-in (not user configurable) thresholds for different trigger types to decide when to add instances, such as the age of messages and queue size for QueueTrigger. For more information, see [How the Consumption and Premium plans work](event-driven-scaling.md).

0 commit comments

Comments
 (0)