serverless http不支持gzip的解决方法

const { gzipSync } = require("zlib");// 引入node本身的自带库
exports.handler = async (event) => {
    const json = { message: 'Hello' };
    const body = JSON.stringify(json);
    const gzip = gzipSync(body);
    const base64 = gzip.toString('base64');
    
    return{
        isBase64Encoded: true,  // 这里要设置base64为true.
        statusCode: 200,
        headers: {
            'Content-Type': 'application/json',
            'Content-Encoding': 'gzip', // 这里要加上gzip
        },
        body: base64,
    };
};

参考:

https://medium.com/@vanbergstephane/gzip-aws-lambda-http-api-gateway-6b9c6431c564

你可能感兴趣的:(AWS)