aliyun-function-compat

aliyun function computing compat

Usage no npm install needed!

<script type="module">
  import aliyunFunctionCompat from 'https://cdn.skypack.dev/aliyun-function-compat';
</script>

README

Aliyun-function-compat

阿里云函数计算适配器

阿里云的函数计算提供两种入口:普通函数和http函数

普通函数形如event, context, callback

http函数则是req, res, context

本工具包将两种函数转换为event, context模式,并且使用async/await来返回结果

使用方法

安装依赖

npm i aliyun-function-compat

编写代码

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 }
})