dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.74k stars 167 forks source link

Please move binary mode docs to the standard README #88

Open tyler-brandt opened 5 years ago

tyler-brandt commented 5 years ago

I've been using serverless-http for a while (it's great and I love it <3) but only recently needed to enable binary mode for some application/octet-stream responses. This seems like a pretty commonly needed configuration option and would be nice to see in the regular README (or at least a reference to the binary mode config so people know it's in the advanced configuration docs).

I hope that this change would save time and headache when new users are searching for the documentation on this awesome package.

Thanks for being great! 👍

ceefour commented 5 years ago

👍 , see https://github.com/dougmoscrop/serverless-http/issues/52

Especially now that serverless framework 1.42.x supports this: https://github.com/serverless/serverless/pull/6063

Here's what I do:

serverless.yml :

  apiGateway:
    # restApiId: afot4qlq25 # Doesn't work: Template error: instance of Fn::GetAtt references undefined resource ApiGatewayRestApi
    minimumCompressionSize: 1024
    binaryMediaTypes: # Optional binary media types the API might return
      - 'application/*'
      - 'image/*'
      - '*/*'

lambda.js :

"use strict";
const serverless = require("serverless-http");
const app = require("./dist/app");

const handler = serverless(app, {binary: ['application/*', 'image/*']});
module.exports.handler = async (event, context) => {
  // FIXME: Ugly workaround for https://github.com/awslabs/aws-serverless-express/issues/86 and https://github.com/claudiajs/claudia/issues/170 and https://github.com/dougmoscrop/serverless-http/issues/68
  event.path = event.path.replace(/^\/profile-[^/]+/, "");
  return await handler(event, context);
};

in express:

  res.type(result.contentType)
    .header("Content-Disposition", `attachment; filename="${result.filename}"`);
  return res.send(result.buffer);