Este conteúdo não está disponível no idioma selecionado.
Chapter 12. Functions development reference guide
OpenShift Serverless Functions provides templates that can be used to create basic functions. A template initiates the function project boilerplate and prepares it for use with the kn func
tool. Each function template is tailored for a specific runtime and follows its conventions. With a template, you can initiate your function project automatically.
Templates for the following runtimes are available:
12.1. Node.js context object reference Copiar o linkLink copiado para a área de transferência!
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.
12.1.1. log Copiar o linkLink copiado para a área de transferência!
Provides a logging object that can be used to write output to the cluster logs. The log adheres to the Pino logging API.
Example log
function handle(context) { context.log.info(“Processing customer”); }
function handle(context) {
context.log.info(“Processing customer”);
}
You can access the function by using the kn func invoke
command:
Example command
kn func invoke --target 'http://example.function.com'
$ kn func invoke --target 'http://example.function.com'
Example output
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}
{"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.
12.1.2. query Copiar o linkLink copiado para a área de transferência!
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
You can access the function by using the kn func invoke
command:
Example command
kn func invoke --target 'http://example.com?name=tiger'
$ kn func invoke --target 'http://example.com?name=tiger'
Example output
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
12.1.3. body Copiar o linkLink copiado para a área de transferência!
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
function handle(context) { // log the incoming request body's 'hello' parameter context.log.info(context.body.hello); }
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
kn func invoke -d '{"Hello": "world"}'
$ kn func invoke -d '{"Hello": "world"}'
Example output
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
12.1.4. headers Copiar o linkLink copiado para a área de transferência!
Returns the HTTP request headers as an object.
Example header
function handle(context) { context.log.info(context.headers["custom-header"]); }
function handle(context) {
context.log.info(context.headers["custom-header"]);
}
You can access the function by using the kn func invoke
command:
Example command
kn func invoke --target 'http://example.function.com'
$ kn func invoke --target 'http://example.function.com'
Example output
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}
12.1.5. HTTP requests Copiar o linkLink copiado para a área de transferência!
- 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.
12.2. TypeScript context object reference Copiar o linkLink copiado para a área de transferência!
The context
object has several properties that can be accessed by the function developer. Accessing these properties can provide information about incoming HTTP requests and write output to the cluster logs.
12.2.1. log Copiar o linkLink copiado para a área de transferência!
Provides a logging object that can be used to write output to the cluster logs. The log adheres to the Pino logging API.
Example log
You can access the function by using the kn func invoke
command:
Example command
kn func invoke --target 'http://example.function.com'
$ kn func invoke --target 'http://example.function.com'
Example output
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}
{"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.
12.2.2. query Copiar o linkLink copiado para a área de transferência!
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
You can access the function by using the kn func invoke
command:
Example command
kn func invoke --target 'http://example.function.com' --data '{"name": "tiger"}'
$ kn func invoke --target 'http://example.function.com' --data '{"name": "tiger"}'
Example output
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"} {"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"tiger"}
12.2.3. body Copiar o linkLink copiado para a área de transferência!
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
You can access the function by using the kn func invoke
command:
Example command
kn func invoke --target 'http://example.function.com' --data '{"hello": "world"}'
$ kn func invoke --target 'http://example.function.com' --data '{"hello": "world"}'
Example output
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
12.2.4. headers Copiar o linkLink copiado para a área de transferência!
Returns the HTTP request headers as an object.
Example header
You can access the function by using the curl
command to invoke it:
Example command
curl -H'x-custom-header: some-value’' http://example.function.com
$ curl -H'x-custom-header: some-value’' http://example.function.com
Example output
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}
12.2.5. HTTP requests Copiar o linkLink copiado para a área de transferência!
- 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.