1
0
mirror of https://github.com/openshift/openshift-docs.git synced 2026-02-05 21:46:22 +01:00
Files
openshift-docs/modules/serverless-nodejs-context-object-reference.adoc
2023-10-30 10:13:25 -04:00

132 lines
3.7 KiB
Plaintext

// Module included in the following assemblies:
//
// * serverless/functions/serverless-functions-reference-guide.adoc
:_mod-docs-content-type: REFERENCE
[id="serverless-nodejs-context-object-reference_{context}"]
= Node.js context object reference
The `context` object has several properties that can be accessed by the function developer. Accessing these properties can provide information about HTTP requests and write output to the cluster logs.
[id="serverless-nodejs-context-object-reference-log_{context}"]
== log
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].
.Example log
[source,javascript]
----
function handle(context) {
context.log.info(“Processing customer”);
}
----
You can access the function by using the `kn func invoke` command:
.Example command
[source,terminal]
----
$ kn func invoke --target 'http://example.function.com'
----
.Example output
[source,terminal]
----
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}
----
You can change the log level to one of `fatal`, `error`, `warn`, `info`, `debug`, `trace`, or `silent`. To do that, change the value of `logLevel` by assigning one of these values to the environment variable `FUNC_LOG_LEVEL` using the `config` command.
[id="serverless-nodejs-context-object-reference-query_{context}"]
== query
Returns the query string for the request, if any, as key-value pairs. These attributes are also found on the context object itself.
.Example query
[source,javascript]
----
function handle(context) {
// Log the 'name' query parameter
context.log.info(context.query.name);
// Query parameters are also attached to the context
context.log.info(context.name);
}
----
You can access the function by using the `kn func invoke` command:
.Example command
[source,terminal]
----
$ kn func invoke --target 'http://example.com?name=tiger'
----
.Example output
[source,terminal]
----
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
----
[id="serverless-nodejs-context-object-reference-body_{context}"]
== body
Returns the request body if any. If the request body contains JSON code, this will be parsed so that the attributes are directly available.
.Example body
[source,javascript]
----
function handle(context) {
// log the incoming request body's 'hello' parameter
context.log.info(context.body.hello);
}
----
You can access the function by using the `curl` command to invoke it:
.Example command
[source,terminal]
----
$ kn func invoke -d '{"Hello": "world"}'
----
.Example output
[source,terminal]
----
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
----
[id="serverless-nodejs-context-object-reference-headers_{context}"]
== headers
Returns the HTTP request headers as an object.
.Example header
[source,javascript]
----
function handle(context) {
context.log.info(context.headers["custom-header"]);
}
----
You can access the function by using the `kn func invoke` command:
.Example command
[source,terminal]
----
$ kn func invoke --target 'http://example.function.com'
----
.Example output
[source,terminal]
----
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}
----
[id="serverless-nodejs-context-object-reference-http-requests_{context}"]
== HTTP requests
method:: Returns the HTTP request method as a string.
httpVersion:: Returns the HTTP version as a string.
httpVersionMajor:: Returns the HTTP major version number as a string.
httpVersionMinor:: Returns the HTTP minor version number as a string.