tcb-ff
使用指南
tcb-ff
由 @cloudbase/functions-framework
提供,用于启动云函数服务。
@cloudbase/functions-framework
是云开发提供的函数框架,用于云端运行云函数,以及本地开发和调试云函数。
Q: 什么情况下需要安装
tcb-ff
?A: 云函数本地开发和调试时,需要安装
tcb-ff
。
安装 tcb-ff
安装到全局
# 全局安装 tcb-ff
npm install -g @cloudbase/functions-framework
# 运行 tcb-ff
tcb-ff
安装到项目下
# 本地安装 tcb-ff
npm install @cloudbase/functions-framework
# 运行 tcb-ff
npx tcb-ff
使用 tcb-ff
启动云函数服务
如下为一个常用的示例,启动云函数同时监听文件(基于 nodemon
)变化,文件变化后会重新启动,该命令开启 CORS 了能力,允许浏览器在 *.example.com
和 www.another.com
域名上访问服务。
tcb-ff -w --enableCors=true --allowedOrigins=*.example.com,www.another.com
在函数代码根路径下执行 tcb-ff
即可启动云函数服务
tcb-ff
默认情况下,tcb-ff
会监听 3000
端口,如需指定端口,可通过 --port
参数指定
tcb-ff --port=30000
tcb-ff -p=30000
tcb-ff
支持监听本地文件变更,如需启用热重启模式,可通过 -w
参数启用
tcb-ff -w
如果通过浏览器访问云函数服务,可能会遇到跨域问题,可以通过 --enableCors
参数启用 CORS
tcb-ff --enableCors=true
--enableCors
默认启用 Origin
为 localhost | 127.0.0.1
的跨域访问,如需配置其他域名,可通过 --allowedOrigins
参数配置
tcb-ff --enableCors=true --allowedOrigins=*.example.com,www.another.com
tcb-ff
支持开启 Basic Auth
进行身份认证,可以通过 --allowedAccounts
参数指定允许访问的账号
tcb-ff --allowedAccounts=abc:123,efg:456
tcb-ff
参数说明
参数说明
命令行参数 | 环境变量 | 默认值 | 示例 | 说明 |
---|---|---|---|---|
-w, --watch | / | false | -w | 是否启用热重启模式,如启用,将会在函数代码文件变更时自动重启服务 |
--port | PORT | 3000 | --port=3000 | 监听的端口 |
--extendedContextKey | EXTENDED_CONTEXT_KEY | '' | --extendedContextKey=X-Functions-Extended-Context | 指定获取扩展 context.extendedContext 的头部字段 |
--enableCors | ENABLE_CORS | false | --enableCors=true | 是否启用 CORS,启用后可配置放通的域名,默认放通 localhost 和 127.0.0.1 |
--allowedOrigins | ALLOWED_ORIGINS | localhost,127.0.0.1 | --allowedOrigins="example.com,*.abc.com" | 需放通 CORS 的域名,默认放通 localhost 和 127.0.0.1 。支持通配符,如 *.example.com 可匹配 www.example.com 和 auth.example.com 。该配置项仅在启用enableCors 时生效。 |
--allowedAccounts | ALLOWED_ACCOUNTS | 无 | --allowedAccounts=abc:123,efg:456 | 允许访问的账号,配置后开启 Basic Auth |
--gracefulShutdown | GRACEFUL_SHUTDOWN | NODE_ENV 不为 production 时开启 | --gracefulShutdown=false | 是否启用服务的优雅停止 |
--timeout | TIMEOUT | 0 | --timeout=5000 | Node.js Server 超时时间,见 https://nodejs.org/api/http.html#servertimeout |
--keepAliveTimeout | KEEP_ALIVE_TIMEOUT | 65000 | --keepAliveTimeout=10000 | Node.js Server keep-alive 超时时间,见 https://nodejs.org/api/http.html#server-keepalivetimeout |
--captureStack | CAPTURE_STACK | NODE_ENV 不为 production 时开启 | --captureStack=true | 请求出错时,是否返回错误堆栈 |
--concurrencyLimit | CONCURRENCY_LIMIT | 1000 | --concurrencyLimit=100 | 并发请求数限制,0 为不限制 |
--noExit | NO_EXIT | false | --noExit=true | 是否禁用 process.exit() 避免进程意外退出 |
--logHeaderBody | LOG_HEADER_BODY | false | --logHeaderBody=true | 是否在请求日志中记录请求头、请求体、响应头、响应体,如记录,将会限制 body 大小为 2KB |
--logEventContext | LOG_EVENT_CONTEXT | true | --logEventContext=false | 是否在请求日志中记录入参 event 和 context |
--logDirname | LOG_DIRNAME | ./logs | --logDirname=/var/logs | 日志文件目录 |
--maxLogFileSize | MAX_LOG_FILE_SIZE | 1024 * 1024 * 128 (128m) | --maxLogFileSize=2097152 | 单个日志文件最大字节数 |
--maxLogFiles | MAX_LOG_FILES | 4 | --maxLogFiles=5 | 最多同时存在的日志文件数量 |
--interceptOutput | INTERCEPT_OUTPUT | false | --interceptOutput=true | 是否拦截console.log 等标准输出,若拦截,会将标准输出的内容记录到日志文件中 |
可以通过 tcb-ff -h
查看全部参数
$ tcb-ff -h
[@cloudbase/functions-framework@1.6.0][pid:0]
Usage: tcb-ff [options]
Options:
-w, --watch Watch the source file for changes and restart the service. Default is false.
Example: -w
--port, -p Set the port to listen on. Default is 3000.
Example: --port=3000
Environment Variable: PORT
--target Set the target function name. Default is 'main'.
Example: --target=hello
Environment Variable: FUNCTION_TARGET
--source Set the target function filename. Default is 'index.js' (extension differs based on module type).
Example: --source=main.mjs
Environment Variable: FUNCTION_SOURCE
--loadAllFunctions Whether to load all functions in the 'functionsRoot' directory. Default: false.
Example: --loadAllFunctions=true
Environment Variable: LOAD_ALL_FUNCTIONS
--functionsRoot The root directory of the functions. only effective when 'loadAllFunctions' is true.
Example: --functionsRoot=path/to/functionsRoot
Environment Variable: FUNCTIONS_ROOT
--signatureType Set the function format. Currently only 'event' is supported.
Example: --signatureType=event
Environment Variable: SIGNATURE_TYPE
--gracefulShutdown Enable graceful shutdown of the service. Default true when 'NODE_ENV=production' otherwise false.
Example: --gracefulShutdown=false
Environment Variable: GRACEFUL_SHUTDOWN
--timeout Set the Node.js Server timeout. Default is 0.
Example: --timeout=5000
Environment Variable: TIMEOUT
--keepAliveTimeout Set the Node.js Server keep-alive timeout. Default is 65000.
Example: --keepAliveTimeout=10000
Environment Variable: KEEP_ALIVE_TIMEOUT
--captureStack Return error stack when a request fails. Enabled when 'NODE_ENV=production'.
Example: --captureStack=true
Environment Variable: CAPTURE_STACK
--concurrencyLimit Limit the number of concurrent requests. 0 means no limit. Default is 1000.
Example: --concurrencyLimit=100
Environment Variable: CONCURRENCY_LIMIT
--noExit Disable 'process.exit()' to prevent accidental process exit. Default is false.
Example: --noExit=true
Environment Variable: NO_EXIT
--interceptOutput If true, intercept console、process.stdout、process.stderr output. Default: false.
Example: --interceptOutput=true
Environment Variable: INTERCEPT_OUTPUT
--logHeaderBody Log request and response headers and bodies in the request log.
Default is false.
Example: --logHeaderBody=false
Environment Variable: LOG_HEADER_BODY
--logEventContext Log original event and context in the access log. Default is true.
Example: --logEventContext=false
Environment Variable: LOG_EVENT_CONTEXT
--extendedContextKey The key of headers to parse `context.extendedContext`.
`''` means that the feature is turned off.
Default is .
Example: --extendedContextKey=X-Functions-Extended-Context
Environment Variable: EXTENDED_CONTEXT_KEY
--extendedContext The value to parse `context.extendedContext`.
`''` means that the feature is turned off.
This parameter will be ignore when 'extendedContextKey' is specified.
Default is null.
Example: --extendedContext='{"a":1,"b":2}'
Environment Variable: EXTENDED_CONTEXT
--functionsConfigFile Set the multi-functions definition config file. Default is 'cloudbase-functions.json'.
Example: --functionsConfigFile=functions.json
Environment Variable: FUNCTIONS_CONFIG_FILE
--enableCors If true, enable CORS for configured origins. Default is false.
Example: --enableCors=true
Environment Variable: ENABLE_CORS
--allowedOrigins Set the allowed origins for CORS. Default allows localhost,127.0.0.1.
Origins should be domain names without protocol or port.
Wildcard origins are supported. e.g. ['*.example.com'].
Multiple origins should be separated by comma.
Example: --allowedOrigins=*.example.com,www.another.com
Environment Variable: ALLOWED_ORIGINS
--allowedAccounts Set the accounts allowed to access.
If specified, The 'username:password' of request will be verified.
Parameter Format: username1:password1,username1:password1,....
Example: --allowedAccounts=abc:123,efg:456
Access Example:
curl http://username:password@127.0.0.1:3000/
curl -H "Authorization: Basic base64encode(username:password)" http://127.0.0.1:3000/
Environment Variable: ALLOWED_ACCOUNTS
--dry-run Do not start the service, only validate that the code can be loaded. Default is false.
Example: --dry-run