Serverless Aws Static File Handler

homepage icon https://github.com/activescott/serverless-aws-static-file-handler
Follow @activescott

Tracked

NPM Downloads Last Month
398
Issues
0
Stars
3
Forks
1
Watchers
3
Watch Star Fork Issue Download License NPM Build Status Coverage Status Contributors

Repo README Contents:

serverless-aws-static-file-handler

npm version Build Status Coverage Status code style: prettier license

An easy way to host the front-end of your web applications on Serverless framework on AWS Lambda along with their APIs written in Serverless.

It is a fast and easy way to get started and makes it trivial to deploy your web applications. If you need better response time in the future and get concerned about AWS costs of using Lambda to static content, you put Cloud Front in front of your Serverless endpoints service static content.

Usage

Import & initialize:

const StaticFileHandler = require('serverless-aws-static-file-handler')

# configure where to serve files from:
const clientFilesPath = path.join(__dirname, "./data-files/")
const fileHandler = new StaticFileHandler(clientFilesPath)

Define a handler in your code as follows:

module.exports.html = async (event, context) => {
  event.path = "index.html" // forcing a specific page for this handler, ignore requested path. This would serve ./data-files/index.html
  return fileHandler.get(event, context)
}

In your serverless.yml file, reference the handler function from above to provide routes to your static files:

functions:
  html:
    handler: handler.html
    events:
      - http:
          path: /
          method: get

  # Note Binary files work too! See configuration information below
  png:
    handler: handler.png
    events:
      - http:
          path: png
          method: get

  # The following example uses a path placeholder to serve all files directly in the /binary/ directory:
  binary:
    handler: handler.binary
    events:
      - http:
        path: /binary/{pathvar+}
        method: get

To serve binary content make sure that you setup the plugin in your serverless.yml like so:

plugins:
  - serverless-aws-static-file-handler/plugins/BinaryMediaTypes

custom:
  apiGateway:
    binaryMediaTypes:
      - "image/png"
      - "image/jpeg"

Some additional real-world examples are demonstrated in the demo.

Installation

npm: npm install serverless-aws-static-file-handler --save-prod