Skip to content

Commit abe3bd5

Browse files
authored
Merge pull request #226577 from lilyjma/v4ModelQuickStarts
Node V4 model quick starts
2 parents bd3979b + b23c2bc commit abe3bd5

7 files changed

+343
-55
lines changed

articles/azure-functions/create-first-function-cli-node.md

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
---
22
title: Create a JavaScript function from the command line - Azure Functions
33
description: Learn how to create a JavaScript function from the command line, then publish the local Node.js project to serverless hosting in Azure Functions.
4-
ms.date: 11/18/2021
4+
ms.date: 03/08/2023
55
ms.topic: quickstart
66
ms.devlang: javascript
77
ms.custom: devx-track-azurecli, devx-track-azurepowershell, mode-api
8+
zone_pivot_groups: functions-nodejs-model
89
---
910

1011
# Quickstart: Create a JavaScript function in Azure from the command line
1112

12-
[!INCLUDE [functions-language-selector-quickstart-cli](../../includes/functions-language-selector-quickstart-cli.md)]
13-
1413
In this article, you use command-line tools to create a JavaScript function that responds to HTTP requests. After testing the code locally, you deploy it to the serverless environment of Azure Functions.
1514

16-
Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.
15+
>[!NOTE]
16+
>The v4 programming model for authoring Functions in Node.js is currently in Preview. Compared to the current v3 model, the v4 model is designed to have a more idiomatic and intuitive experience for JavaScript and TypeScript developers. To learn more, see the [Developer Reference Guide](functions-reference-node.md).
17+
18+
Use the selector at the top to choose the programming model of your choice for completing this quickstart. Note that completion will incur a small cost of a few USD cents or less in your Azure account.
1719

1820
There is also a [Visual Studio Code-based version](create-first-function-vs-code-node.md) of this article.
1921

@@ -23,31 +25,55 @@ Before you begin, you must have the following:
2325

2426
+ An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio).
2527

28+
::: zone pivot="nodejs-model-v3"
2629
+ The [Azure Functions Core Tools](./functions-run-local.md#v2) version 4.x.
30+
::: zone-end
31+
32+
::: zone pivot="nodejs-model-v4"
33+
+ The [Azure Functions Core Tools](./functions-run-local.md#v2) version v4.0.5085 or above
34+
::: zone-end
2735

2836
+ One of the following tools for creating Azure resources:
2937

3038
+ [Azure CLI](/cli/azure/install-azure-cli) version 2.4 or later.
3139

3240
+ The Azure [Az PowerShell module](/powershell/azure/install-az-ps) version 5.9.0 or later.
3341

42+
::: zone pivot="nodejs-model-v3"
3443
+ [Node.js](https://nodejs.org/) version 18 or 16.
44+
::: zone-end
45+
46+
::: zone pivot="nodejs-model-v4"
47+
+ [Node.js](https://nodejs.org/) version 18 or above.
48+
::: zone-end
3549

3650
### Prerequisite check
3751

3852
Verify your prerequisites, which depend on whether you are using Azure CLI or Azure PowerShell for creating Azure resources:
3953

4054
# [Azure CLI](#tab/azure-cli)
4155

56+
::: zone pivot="nodejs-model-v3"
4257
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools are version 4.x.
58+
::: zone-end
59+
60+
::: zone pivot="nodejs-model-v4"
61+
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools are version v4.0.4915 or above.
62+
::: zone-end
4363

4464
+ Run `az --version` to check that the Azure CLI version is 2.4 or later.
4565

4666
+ Run `az login` to sign in to Azure and verify an active subscription.
4767

4868
# [Azure PowerShell](#tab/azure-powershell)
4969

70+
::: zone pivot="nodejs-model-v3"
5071
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools are version 4.x.
72+
::: zone-end
73+
74+
::: zone pivot="nodejs-model-v4"
75+
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools are version v4.0.4915 or above.
76+
::: zone-end
5177

5278
+ Run `(Get-Module -ListAvailable Az).Version` and verify version 5.0 or later.
5379

@@ -59,6 +85,7 @@ Verify your prerequisites, which depend on whether you are using Azure CLI or Az
5985

6086
In Azure Functions, a function project is a container for one or more individual functions that each responds to a specific trigger. All functions in a project share the same local and hosting configurations. In this section, you create a function project that contains a single function.
6187

88+
::: zone pivot="nodejs-model-v3"
6289
1. Run the `func init` command, as follows, to create a functions project in a folder named *LocalFunctionProj* with the specified runtime:
6390

6491
```console
@@ -81,6 +108,8 @@ In Azure Functions, a function project is a container for one or more individual
81108

82109
`func new` creates a subfolder matching the function name that contains a code file appropriate to the project's chosen language and a configuration file named *function.json*.
83110

111+
You may find the [Azure Functions Core Tools reference](functions-core-tools-reference.md) helpful.
112+
84113
### (Optional) Examine the file contents
85114

86115
If desired, you can skip to [Run the function locally](#run-the-function-locally) and examine the file contents later.
@@ -101,6 +130,52 @@ For an HTTP trigger, the function receives request data in the variable `req` as
101130

102131
Each binding requires a direction, a type, and a unique name. The HTTP trigger has an input binding of type [`httpTrigger`](functions-bindings-http-webhook-trigger.md) and output binding of type [`http`](functions-bindings-http-webhook-output.md).
103132

133+
::: zone-end
134+
135+
::: zone pivot="nodejs-model-v4"
136+
1. Run the `func init` command, as follows, to create a functions project in a folder named *LocalFunctionProj*:
137+
138+
```console
139+
func init LocalFunctionProj --model V4
140+
```
141+
You are then prompted to choose a worker runtime and a language - choose Node for the first and JavaScript for the second.
142+
143+
2. Navigate into the project folder:
144+
145+
```console
146+
cd LocalFunctionProj
147+
```
148+
149+
This folder contains various files for the project, including configurations files named *local.settings.json* and *host.json*. Because *local.settings.json* can contain secrets downloaded from Azure, the file is excluded from source control by default in the *.gitignore* file.
150+
151+
3. Add a function to your project by using the following command:
152+
153+
```console
154+
func new
155+
```
156+
157+
Choose the template for "HTTP trigger". You can keep the default name (*httpTrigger*) or give it a new name (*HttpExample*). Your function name must be unique, or you'll be asked to confirm if your intention is to replace an existing function.
158+
159+
You can find the function you added in the *src/functions* directory.
160+
161+
4. Add Azure Storage connection information in *local.settings.json*.
162+
```json
163+
{
164+
"Values": {
165+
"AzureWebJobsStorage": "<Azure Storage connection information>",
166+
"FUNCTIONS_WORKER_RUNTIME": "node",
167+
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
168+
}
169+
}
170+
```
171+
172+
5. (Optional) If you want to learn more about a particular function, say HTTP trigger, you can run the following command:
173+
174+
```console
175+
func help httptrigger
176+
```
177+
::: zone-end
178+
104179
[!INCLUDE [functions-run-function-test-local-cli](../../includes/functions-run-function-test-local-cli.md)]
105180

106181
[!INCLUDE [functions-create-azure-resources-cli](../../includes/functions-create-azure-resources-cli.md)]
@@ -129,6 +204,28 @@ Each binding requires a direction, a type, and a unique name. The HTTP trigger h
129204

130205
This command creates a function app running in your specified language runtime under the [Azure Functions Consumption Plan](consumption-plan.md), which is free for the amount of usage you incur here. The command also provisions an associated Azure Application Insights instance in the same resource group, with which you can monitor your function app and view logs. For more information, see [Monitor Azure Functions](functions-monitoring.md). The instance incurs no costs until you activate it.
131206

207+
::: zone pivot="nodejs-model-v4"
208+
## Update app settings
209+
210+
To enable your V4 programming model app to run in Azure, you need to add a new application setting named `AzureWebJobsFeatureFlags` with a value of `EnableWorkerIndexing`. This setting is already in your local.settings.json file.
211+
212+
Run the following command to add this setting to your new function app in Azure. Replace `<FUNCTION_APP_NAME>` and `<RESOURCE_GROUP_NAME>` with the name of your function app and resource group, respectively.
213+
214+
# [Azure CLI](#tab/azure-cli)
215+
216+
```azurecli
217+
az functionapp config appsettings set --name <FUNCTION_APP_NAME> --resource-group <RESOURCE_GROUP_NAME> --settings AzureWebJobsFeatureFlags=EnableWorkerIndexing
218+
```
219+
220+
# [Azure PowerShell](#tab/azure-powershell)
221+
222+
```azurepowershell
223+
Update-AzFunctionAppSetting -Name <FUNCTION_APP_NAME> -ResourceGroupName <RESOURCE_GROUP_NAME> -AppSetting @{"AzureWebJobsFeatureFlags" = "EnableWorkerIndexing"}
224+
```
225+
226+
---
227+
::: zone-end
228+
132229
[!INCLUDE [functions-publish-project-cli](../../includes/functions-publish-project-cli.md)]
133230

134231
[!INCLUDE [functions-run-remote-azure-cli](../../includes/functions-run-remote-azure-cli.md)]

0 commit comments

Comments
 (0)