You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -45,14 +54,27 @@ In v4, the [`@azure/functions`](https://www.npmjs.com/package/@azure/functions)
45
54
46
55
In v4 of the programming model, you can structure your code however you want. The only files that you need at the root of your app are *host.json* and *package.json*.
47
56
48
-
Otherwise, you define the file structure by setting the `main` field in your *package.json* file. You can set the `main` field to a single file or multiple files by using a [glob pattern](https://wikipedia.org/wiki/Glob_(programming)). Common values for the `main` field might be:
57
+
Otherwise, you define the file structure by setting the `main` field in your *package.json* file. You can set the `main` field to a single file or multiple files by using a [glob pattern](https://wikipedia.org/wiki/Glob_(programming)). The following table shows example values for the `main` field:
58
+
59
+
:::zone pivot="programming-language-javascript"
60
+
61
+
| Example | Description |
62
+
| --- | --- |
63
+
| **`src/index.js`** | Register functions from a single root file. |
64
+
| **`src/functions/*.js`** | Register each function from its own file. |
65
+
| **`src/{index.js,functions/*.js}`** | A combination where you register each function from its own file, but you still have a root file for general app-level code. |
66
+
67
+
:::zone-end
49
68
50
-
- TypeScript:
51
-
- `dist/src/index.js`
52
-
- `dist/src/functions/*.js`
53
-
- JavaScript:
54
-
- `src/index.js`
55
-
- `src/functions/*.js`
69
+
:::zone pivot="programming-language-typescript"
70
+
71
+
| Example | Description |
72
+
| --- | --- |
73
+
| **`dist/src/index.js`** | Register functions from a single root file. |
74
+
| **`dist/src/functions/*.js`** | Register each function from its own file. |
75
+
| **`dist/src/{index.js,functions/*.js}`** | A combination where you register each function from its own file, but you still have a root file for general app-level code. |
76
+
77
+
:::zone-end
56
78
57
79
> [!TIP]
58
80
> Make sure you define a `main` field in your *package.json* file.
@@ -68,39 +90,74 @@ The trigger input, instead of the invocation context, is now the first argument
68
90
69
91
You no longer have to create and maintain those separate *function.json* configuration files. You can now fully define your functions directly in your TypeScript or JavaScript files. In addition, many properties now have defaults so that you don't have to specify them every time.
> Move the configuration from your *function.json* file to your code. The type of the trigger corresponds to a method on the `app` object in the new model. For example, if you use an `httpTrigger` type in *function.json*, call `app.http()` in your code to register the function. If you use `timerTrigger`, call `app.timer()`.
132
192
@@ -144,22 +204,50 @@ The primary input is also called the *trigger* and is the only required input or
144
204
145
205
Version 4 supports only one way of getting the trigger input, as the first argument:
Version 3 supports several ways of setting the primary output:
@@ -207,6 +311,28 @@ return {
207
311
> [!TIP]
208
312
> Make sure you always return the output in your function handler, instead of setting it with the `context` object.
209
313
314
+
### Context logging
315
+
316
+
In v4, logging methods were moved to the root `context` object as shown in the following example. For more information about logging, see the [Node.js developer guide](./functions-reference-node.md#logging).
317
+
318
+
# [v4](#tab/v4)
319
+
320
+
```javascript
321
+
context.log('This is an info log');
322
+
context.error('This is an error');
323
+
context.warn('This is an error');
324
+
```
325
+
326
+
# [v3](#tab/v3)
327
+
328
+
```javascript
329
+
context.log('This is an info log');
330
+
context.log.error('This is an error');
331
+
context.log.warn('This is an error');
332
+
```
333
+
334
+
---
335
+
210
336
### Create a test context
211
337
212
338
Version 3 doesn't support creating an invocation context outside the Azure Functions runtime, so authoring unit tests can be difficult. Version 4 allows you to create an instance of the invocation context, although the information during tests isn't detailed unless you add it yourself.
@@ -238,7 +364,7 @@ The types use the [`undici`](https://undici.nodejs.org/) package in Node.js. Thi
238
364
239
365
- *Body*. You can access the body by using a method specific to the type that you want to receive:
240
366
241
-
```javascript
367
+
```javascript
242
368
constbody=awaitrequest.text();
243
369
constbody=awaitrequest.json();
244
370
constbody=awaitrequest.formData();
@@ -301,10 +427,18 @@ The types use the [`undici`](https://undici.nodejs.org/) package in Node.js. Thi
301
427
302
428
- *Body*:
303
429
430
+
Use the `body` property to return most types like a `string` or `Buffer`:
431
+
304
432
```javascript
305
433
return { body:"Hello, world!" };
306
434
```
307
435
436
+
Use the `jsonBody` property for the easiest way to return a JSON response:
437
+
438
+
```javascript
439
+
return { jsonBody: { hello:"world" } };
440
+
```
441
+
308
442
- *Header*. You can set the header in two ways, depending on whether you're using the `HttpResponse` class or the `HttpResponseInit` interface:
309
443
310
444
```javascript
@@ -331,12 +465,12 @@ The types use the [`undici`](https://undici.nodejs.org/) package in Node.js. Thi
331
465
return { statusCode:200 };
332
466
```
333
467
334
-
- *Body*. You can set a body in several ways:
468
+
- *Body*. You can set a body in several ways and it's the same regardless of the body type (`string`, `Buffer`, JSON object, etc.):
335
469
336
470
```javascript
337
471
context.res.send("Hello, world!");
338
472
context.res.end("Hello, world!");
339
-
context.res= { body:"Hello, world!" }
473
+
context.res= { body:"Hello, world!" };
340
474
return { body:"Hello, world!" };
341
475
```
342
476
@@ -356,8 +490,18 @@ The types use the [`undici`](https://undici.nodejs.org/) package in Node.js. Thi
356
490
357
491
---
358
492
493
+
:::zone pivot="programming-language-javascript"
494
+
495
+
> [!TIP]
496
+
> Update any logic by using the HTTP request or response types to match the new methods.
497
+
498
+
:::zone-end
499
+
:::zone pivot="programming-language-typescript"
500
+
359
501
> [!TIP]
360
-
> Update any logic by using the HTTP request or response types to match the new methods. If you're using TypeScript, you'll get build errors if you use old methods.
502
+
> Update any logic by using the HTTP request or response types to match the new methods. You should get TypeScript build errors to help you identify if you're using old methods.
Copy file name to clipboardExpand all lines: articles/azure-functions/functions-reference-node.md
+19-1Lines changed: 19 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -270,7 +270,25 @@ export default httpTrigger;
270
270
271
271
::: zone pivot="nodejs-model-v4"
272
272
273
-
The programming model loads your functions based on the `main` field in your `package.json`. This field can be set to a single file like `src/index.js` or a [glob pattern](https://wikipedia.org/wiki/Glob_(programming)) specifying multiple files like `src/functions/*.js`.
273
+
The programming model loads your functions based on the `main` field in your `package.json`. You can set the `main` field to a single file or multiple files by using a [glob pattern](https://wikipedia.org/wiki/Glob_(programming)). The following table shows example values for the `main` field:
274
+
275
+
# [JavaScript](#tab/javascript)
276
+
277
+
| Example | Description |
278
+
| --- | --- |
279
+
|**`src/index.js`**| Register functions from a single root file. |
280
+
|**`src/functions/*.js`**| Register each function from its own file. |
281
+
|**`src/{index.js,functions/*.js}`**| A combination where you register each function from its own file, but you still have a root file for general app-level code. |
282
+
283
+
# [TypeScript](#tab/typescript)
284
+
285
+
| Example | Description |
286
+
| --- | --- |
287
+
|**`dist/src/index.js`**| Register functions from a single root file. |
288
+
|**`dist/src/functions/*.js`**| Register each function from its own file. |
289
+
|**`dist/src/{index.js,functions/*.js}`**| A combination where you register each function from its own file, but you still have a root file for general app-level code. |
290
+
291
+
---
274
292
275
293
In order to register a function, you must import the `app` object from the `@azure/functions` npm module and call the method specific to your trigger type. The first argument when registering a function is the function name. The second argument is an `options` object specifying configuration for your trigger, your handler, and any other inputs or outputs. In some cases where trigger configuration isn't necessary, you can pass the handler directly as the second argument instead of an `options` object.
0 commit comments