serverless-http icon indicating copy to clipboard operation
serverless-http copied to clipboard

Please move binary mode docs to the standard README

Open tyler-brandt opened this issue 6 years ago • 1 comments

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! 👍

tyler-brandt avatar Apr 26 '19 20:04 tyler-brandt

👍 , 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);

ceefour avatar May 17 '19 04:05 ceefour