12.2. TypeScript 컨텍스트 오브젝트 참조
context
오브젝트에는 함수 개발자가 액세스할 수 있는 여러 속성이 있습니다. 이러한 속성에 액세스하면 들어오는 HTTP 요청에 대한 정보를 제공하고 클러스터 로그에 출력을 쓸 수 있습니다.
12.2.1. log
클러스터 로그에 출력을 작성하는 데 사용할 수 있는 로깅 오브젝트를 제공합니다. 로그는 Pino 로깅 API를 따릅니다.
로그 예
export function handle(context: Context): string { // log the incoming request body's 'hello' parameter if (context.body) { context.log.info((context.body as Record<string, string>).hello); } else { context.log.info('No data received'); } return 'OK'; }
kn func invoke
명령을 사용하여 함수에 액세스할 수 있습니다.
명령 예
$ kn func invoke --target 'http://example.function.com'
출력 예
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"Processing customer"}
로그 수준을 fatal
,error
,warn
,info
,debug
,trace
또는 silent
중 하나로 변경할 수 있습니다. 이렇게 하려면 config
명령을 사용하여 해당 값 중 하나를 환경 변수 FujiNC _LOG_LEVEL
에 할당하여 logLevel
값을 변경합니다.
12.2.2. query
요청에 대한 쿼리 문자열을 키-값 쌍으로 반환합니다. 이러한 속성은 컨텍스트 오브젝트 자체에서도 확인할 수 있습니다.
예제 쿼리
export function handle(context: Context): string { // log the 'name' query parameter if (context.query) { context.log.info((context.query as Record<string, string>).name); } else { context.log.info('No data received'); } return 'OK'; }
kn func invoke
명령을 사용하여 함수에 액세스할 수 있습니다.
명령 예
$ kn func invoke --target 'http://example.function.com' --data '{"name": "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
요청 본문(있는 경우)을 반환합니다. 요청 본문에 JSON 코드가 포함된 경우 속성을 직접 사용할 수 있도록 구문 분석됩니다.
본문의 예
export function handle(context: Context): string { // log the incoming request body's 'hello' parameter if (context.body) { context.log.info((context.body as Record<string, string>).hello); } else { context.log.info('No data received'); } return 'OK'; }
kn func invoke
명령을 사용하여 함수에 액세스할 수 있습니다.
명령 예
$ kn func invoke --target 'http://example.function.com' --data '{"hello": "world"}'
출력 예
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"world"}
12.2.4. headers
HTTP 요청 헤더를 오브젝트로 반환합니다.
헤더 예
export function handle(context: Context): string { // log the incoming request body's 'hello' parameter if (context.body) { context.log.info((context.headers as Record<string, string>)['custom-header']); } else { context.log.info('No data received'); } return 'OK'; }
curl
명령을 사용하여 이를 호출하여 함수에 액세스할 수 있습니다.
명령 예
$ curl -H'x-custom-header: some-value’' http://example.function.com
출력 예
{"level":30,"time":1604511655265,"pid":3430203,"hostname":"localhost.localdomain","reqId":1,"msg":"some-value"}
12.2.5. HTTP 요청
- method
- HTTP 요청 메서드를 문자열로 반환합니다.
- httpVersion
- HTTP 버전을 문자열로 반환합니다.
- httpVersionMajor
- HTTP 주요 버전 번호를 문자열로 반환합니다.
- httpVersionMinor
- HTTP 마이너 버전 번호를 문자열로 반환합니다.