mirror of
https://github.com/openshift/openshift-docs.git
synced 2026-02-06 15:46:57 +01:00
129 lines
3.3 KiB
Plaintext
129 lines
3.3 KiB
Plaintext
|
|
// Module included in the following assemblies
|
|||
|
|
//
|
|||
|
|
// * /serverless/functions/serverless-functions-reference-guide.adoc
|
|||
|
|
|
|||
|
|
[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.
|
|||
|
|
|
|||
|
|
[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 `curl` command to invoke it:
|
|||
|
|
|
|||
|
|
.Example command
|
|||
|
|
[source,terminal]
|
|||
|
|
----
|
|||
|
|
$ curl http://example.com
|
|||
|
|
----
|
|||
|
|
|
|||
|
|
.Example output
|
|||
|
|
[source,terminal]
|
|||
|
|
----
|
|||
|
|
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}
|
|||
|
|
----
|
|||
|
|
|
|||
|
|
[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 `curl` command to invoke it:
|
|||
|
|
|
|||
|
|
.Example command
|
|||
|
|
[source,terminal]
|
|||
|
|
----
|
|||
|
|
$ curl 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]
|
|||
|
|
----
|
|||
|
|
$ curl -X POST -d '{"hello": "world"}' -H 'Content-type: application/json' http://example.com
|
|||
|
|
----
|
|||
|
|
|
|||
|
|
.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 `curl` command to invoke it:
|
|||
|
|
|
|||
|
|
.Example command
|
|||
|
|
[source,terminal]
|
|||
|
|
----
|
|||
|
|
$ curl -H 'x-custom-header: some-value’' http://example.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.
|