UNPKG

aliyun-function-compat

Version:

aliyun function computing compat

52 lines (35 loc) 1.17 kB
# Aliyun-function-compat 阿里云函数计算适配器 阿里云的函数计算提供两种入口:普通函数和http函数 普通函数形如`event, context, callback` http函数则是`req, res, context` 本工具包将两种函数转换为`event, context`模式,并且使用`async/await`来返回结果 ## 使用方法 安装依赖 ```shell npm i aliyun-function-compat ``` 编写代码 ```javascript const compat = require('aliyun-function-compat') exports.handler = compat(async (event, context) => { await some() // 在http模式中,event是req对象,包含queries header等参数,具体看官方文档 // 本工具接收POST和PUT方法发送的数据,如果是json格式的会对其格式化 // 否则返回buffer // 通过body属性获取 if(events.method === 'POST') { console.log(events.body.id) } // 可以直接返回错误信息 throw new Error('some erorr message') // 指定错误码 const err = new Error('error with code') err.code = -1234 throw err // 更简单的返回方式 throw 'error message' // 返回处理结果 return { result:123 } }) ```