本文介绍Custom Container事件函数的入参、返回结果及代码示例。
背景介绍
在Custom Container Runtime中,函数计算系统会将Common Headers、调用的请求体(Body)、POST方法以及/invoke、/initialize等路径转发给容器中的HTTP Server。您可以选择实现类似官方支持的Runtime(例如Golang Runtime)的context、event这样的函数签名。您也可以直接使用入参请求头(Headers)和请求体 (Body)来编写函数的业务逻辑。更多信息,请参见事件函数。
函数入参
- event:POST请求体(Body)。
- context:
- 通过x-fc-access-key-id、x-fc-access-key-secret和x-fc-security-token请求头获取服务角色(Service Role)中的临时访问凭证访问云服务。
- 通过x-fc-request-id获取当前请求ID。
- 所有请求头信息请参见Common Headers。
函数返回结构
函数结果通过HTTP响应结构体返回。
代码示例
在以下Node.js Express示例中,POST方法和/initialize路径会在函数实例初始化时被函数计算调用,POST方法和/invoke路径为函数计算被调用时的Handler,通过req.headers
以及req.body
获取context、event并将函数返回结果通过HTTP Response结构体输出。
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// HTTP function invocation
const app = express();
app.get('/*', (req, res) => {
res.send('Hello FunctionCompute, http function\n');
});
// Event function invocation
app.post('/invoke', (req, res) => {
res.send('Hello FunctionCompute, event function\n');
});
var server = app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
server.timeout = 0; // never timeout
server.keepAliveTimeout = 0; // keepalive, never timeout
在文档使用中是否遇到以下问题
更多建议
匿名提交