在创建函数时,您需要指定一个事件处理函数(event handler function),以供函数计算服务执行代码时调用。Node.js 事件处理函数签名为:
- [backcolor=transparent]exports[backcolor=transparent].[backcolor=transparent]myHandler [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]function[backcolor=transparent]([backcolor=transparent]event[backcolor=transparent],[backcolor=transparent] context[backcolor=transparent],[backcolor=transparent] callback[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]...
- [backcolor=transparent] [backcolor=transparent]// Use callback() to return information to the caller.
- [backcolor=transparent]}
myHandler 是函数计算调用的函数的名称。您需要导出此项,使其对函数计算可见。 假设您将此代码保存为 helloworld.js。那么 helloworld.myHandler 是事件处理函数。更多信息请参阅
创建函数。
- event:事件数据,类型为 Buffer 对象。
- context:当前函数的运行时信息,类型为 Context 对象。
- callback:通知函数计算系统函数执行完毕,并将信息返回给调用方。
函数示例:
- [backcolor=transparent]exports[backcolor=transparent].[backcolor=transparent]myHandler [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]function[backcolor=transparent]([backcolor=transparent]event[backcolor=transparent],[backcolor=transparent] context[backcolor=transparent],[backcolor=transparent] callback[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]// 假设调用函数时,传入的event是一个json字符串
- [backcolor=transparent] [backcolor=transparent]var[backcolor=transparent] eventObj [backcolor=transparent]=[backcolor=transparent] JSON[backcolor=transparent].[backcolor=transparent]parse[backcolor=transparent]([backcolor=transparent]event[backcolor=transparent].[backcolor=transparent]toString[backcolor=transparent]());
- [backcolor=transparent] console[backcolor=transparent].[backcolor=transparent]log[backcolor=transparent]([backcolor=transparent]"value1 = "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] eventObj[backcolor=transparent].[backcolor=transparent]key1[backcolor=transparent]);
- [backcolor=transparent] console[backcolor=transparent].[backcolor=transparent]log[backcolor=transparent]([backcolor=transparent]"value2 = "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] eventObj[backcolor=transparent].[backcolor=transparent]key2[backcolor=transparent]);[backcolor=transparent]
- [backcolor=transparent] [backcolor=transparent]// callback第一个参数是Error类型,null表示函数成功执行
- [backcolor=transparent] callback[backcolor=transparent]([backcolor=transparent]null[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"some success message"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// or
- [backcolor=transparent] [backcolor=transparent]// callback("some error type");
- [backcolor=transparent]}
此示例有一个 Node.js 函数,该函数同时也是事件处理函数。在该函数中,首先传入的event被转化成一个JSON object。console.log() 语句会将一些传入的事件数据记录到阿里云日志服务中。当调用callback时,函数退出,并将结果“some success message”返回。
event的格式说明
当一个函数被调用时,函数计算服务将
InvokeFunction的Body透传给事件处理函数,不对Event的格式做任何要求,您可以根据自己的业务需求选择不同的格式,如JSON,XML,Image,Video等。事件处理函数以何种方式解析和使用Event是完全由开发者控制的。
如果您使用了OSS触发器,那这个Event是JSON格式的,下面是一个示例:
- [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]"events"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent][
- [backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]"eventName"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"ObjectCreated:PutObject"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"eventSource"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"acs:oss"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"eventTime"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"2017-04-21T12:46:37.000Z"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"eventVersion"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"1.0"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"oss"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]"bucket"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]"arn"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"acs:oss:cn-shanghai:123456789:bucketname"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"name"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"testbucket"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"ownerIdentity"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"123456789"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"virtualBucket"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]""
- [backcolor=transparent] [backcolor=transparent]},
- [backcolor=transparent] [backcolor=transparent]"object"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]"deltaSize"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]122539[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"eTag"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"688A7BF4F233DC9C88A80BF985AB7329"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"key"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"image/a.jpg"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"size"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]122539
- [backcolor=transparent] [backcolor=transparent]},
- [backcolor=transparent] [backcolor=transparent]"ossSchemaVersion"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"1.0"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"ruleId"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"9adac8e253828f4f7c0466d941fa3db81161e853"
- [backcolor=transparent] [backcolor=transparent]},
- [backcolor=transparent] [backcolor=transparent]"region"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"cn-shanghai"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]"requestParameters"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]"sourceIPAddress"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"140.205.128.221"
- [backcolor=transparent] [backcolor=transparent]},
- [backcolor=transparent] [backcolor=transparent]"responseElements"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]"requestId"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"58F9FF2D3DF792092E12044C"
- [backcolor=transparent] [backcolor=transparent]},
- [backcolor=transparent] [backcolor=transparent]"userIdentity"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]"principalId"[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]"123456789"
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]]
- [backcolor=transparent]}
callback参数使用说明
您通过callback参数将函数执行结果返回给调用方。callback函数签名为:
- [backcolor=transparent]callback[backcolor=transparent]([backcolor=transparent]Error[backcolor=transparent] error[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]Object[backcolor=transparent] result[backcolor=transparent])
当callback调用后,函数计算系统会终止函数。其中,
- error:可选参数,通知系统函数执行失败并将错误信息返回给调用方。
- result:可选参数,将函数成功执行的结果返回给调用方。当用户传入“error”参数时,该参数将被忽略。
callback使用示例
- [backcolor=transparent]callback[backcolor=transparent]();[backcolor=transparent] [backcolor=transparent]// 表明函数执行成功,无返回信息。
- [backcolor=transparent]callback[backcolor=transparent]([backcolor=transparent]null[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 表明函数执行成功,无返回信息。
- [backcolor=transparent]callback[backcolor=transparent]([backcolor=transparent]null[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]null[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 表明函数执行成功,无返回信息。
- [backcolor=transparent]callback[backcolor=transparent]([backcolor=transparent]null[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"success"[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 表明函数执行成功,返回“success”信息给调用方。
- [backcolor=transparent]callback[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Error[backcolor=transparent]([backcolor=transparent]"error"[backcolor=transparent]));[backcolor=transparent] [backcolor=transparent]// 表明函数执行失败,返回“error”信息给调用方。
- [backcolor=transparent]callback[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Error[backcolor=transparent]([backcolor=transparent]"error"[backcolor=transparent]),[backcolor=transparent] [backcolor=transparent]"ignored message"[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]// 表明函数执行失败,第二个参数被忽略。
- [backcolor=transparent]callback[backcolor=transparent]([backcolor=transparent]"error"[backcolor=transparent]);[backcolor=transparent] [backcolor=transparent]// 表明函数执行失败。
注意:
- 您必须主动调用callback函数,通知函数计算系统函数执行完毕;否则函数的执行不会退出,最后会导致超时错误。
- 当您使用callback error参数返回错误信息时,函数计算系统会自动将 error 信息记录到与该函数关联的 SLS 日志库中。如果error信息大于256KB,则系统只会记录前256KB的数据,并在错误消息后显示 “Truncated by FunctionCompute”。关于如何设置函数日志,请参阅相关文档。
- 如果函数的调用类型是同步调用,错误消息或者结果数据会填充到http body中,并返回给客户端。如果是异步调用,则错误信息或者结果数据不会被返回。
[backcolor=transparent] [backcolor=transparent]