|
| 1 | +// Module included in the following assemblies |
| 2 | +// |
| 3 | +// * /serverless/functions/serverless-functions-reference-guide.adoc |
| 4 | + |
| 5 | +[id="serverless-nodejs-context-object-reference_{context}"] |
| 6 | += Node.js context object reference |
| 7 | + |
| 8 | +The `context` object has several properties that can be accessed by the function developer. |
| 9 | + |
| 10 | +[id="serverless-nodejs-context-object-reference-log_{context}"] |
| 11 | +== log |
| 12 | + |
| 13 | +Provides a logging object that can be used to write output to the cluster logs. The log adheres to the link:https://getpino.io/#/docs/api[Pino logging API]. |
| 14 | + |
| 15 | +.Example log |
| 16 | +[source,javascript] |
| 17 | +---- |
| 18 | +function handle(context) { |
| 19 | + context.log.info(“Processing customer”); |
| 20 | +} |
| 21 | +---- |
| 22 | + |
| 23 | +You can access the function by using the `curl` command to invoke it: |
| 24 | + |
| 25 | +.Example command |
| 26 | +[source,terminal] |
| 27 | +---- |
| 28 | +$ curl http://example.com |
| 29 | +---- |
| 30 | + |
| 31 | +.Example output |
| 32 | +[source,terminal] |
| 33 | +---- |
| 34 | +{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"} |
| 35 | +---- |
| 36 | + |
| 37 | +[id="serverless-nodejs-context-object-reference-query_{context}"] |
| 38 | +== query |
| 39 | + |
| 40 | +Returns the query string for the request, if any, as key-value pairs. These attributes are also found on the context object itself. |
| 41 | + |
| 42 | +.Example query |
| 43 | +[source,javascript] |
| 44 | +---- |
| 45 | +function handle(context) { |
| 46 | + // Log the 'name' query parameter |
| 47 | + context.log.info(context.query.name); |
| 48 | + // Query parameters are also attached to the context |
| 49 | + context.log.info(context.name); |
| 50 | +} |
| 51 | +---- |
| 52 | + |
| 53 | +You can access the function by using the `curl` command to invoke it: |
| 54 | + |
| 55 | +.Example command |
| 56 | +[source,terminal] |
| 57 | +---- |
| 58 | +$ curl http://example.com?name=tiger |
| 59 | +---- |
| 60 | + |
| 61 | +.Example output |
| 62 | +[source,terminal] |
| 63 | +---- |
| 64 | +{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"} |
| 65 | +---- |
| 66 | + |
| 67 | +[id="serverless-nodejs-context-object-reference-body_{context}"] |
| 68 | +== body |
| 69 | + |
| 70 | +Returns the request body if any. If the request body contains JSON code, this will be parsed so that the attributes are directly available. |
| 71 | + |
| 72 | +.Example body |
| 73 | +[source,javascript] |
| 74 | +---- |
| 75 | +function handle(context) { |
| 76 | + // log the incoming request body's 'hello' parameter |
| 77 | + context.log.info(context.body.hello); |
| 78 | +} |
| 79 | +---- |
| 80 | + |
| 81 | +You can access the function by using the `curl` command to invoke it: |
| 82 | + |
| 83 | +.Example command |
| 84 | +[source,terminal] |
| 85 | +---- |
| 86 | +$ curl -X POST -d '{"hello": "world"}' -H 'Content-type: application/json' http://example.com |
| 87 | +---- |
| 88 | + |
| 89 | +.Example output |
| 90 | +[source,terminal] |
| 91 | +---- |
| 92 | +{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"} |
| 93 | +---- |
| 94 | + |
| 95 | +[id="serverless-nodejs-context-object-reference-headers_{context}"] |
| 96 | +== headers |
| 97 | + |
| 98 | +Returns the HTTP request headers as an object. |
| 99 | + |
| 100 | +.Example header |
| 101 | +[source,javascript] |
| 102 | +---- |
| 103 | +function handle(context) { |
| 104 | + context.log.info(context.headers["custom-header"]); |
| 105 | +} |
| 106 | +---- |
| 107 | + |
| 108 | +You can access the function by using the `curl` command to invoke it: |
| 109 | + |
| 110 | +.Example command |
| 111 | +[source,terminal] |
| 112 | +---- |
| 113 | +$ curl -H 'x-custom-header: some-value’' http://example.com |
| 114 | +---- |
| 115 | + |
| 116 | +.Example output |
| 117 | +[source,terminal] |
| 118 | +---- |
| 119 | +{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"} |
| 120 | +---- |
| 121 | + |
| 122 | +[id="serverless-nodejs-context-object-reference-http-requests_{context}"] |
| 123 | +== HTTP requests |
| 124 | + |
| 125 | +method:: Returns the HTTP request method as a string. |
| 126 | +httpVersion:: Returns the HTTP version as a string. |
| 127 | +httpVersionMajor:: Returns the HTTP major version number as a string. |
| 128 | +httpVersionMinor:: Returns the HTTP minor version number as a string. |
0 commit comments