Home

Awesome

npm version npm Build Status Coverage Status License GitHub stars

serverless-aws-static-file-handler

Host the front-end of your web applications on Serverless framework on AWS Lambda right alongside the API.

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 CloudFront in front of your Serverless endpoints service static content.

<!-- TOC --> <!-- /TOC -->

Usage / Quick Start

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 examples/basic/ directory as well as a serverless-offline-specific example in examples/serverless-offline/.

Prerequisites / Usage Requirements

Requires Node.js latest, LTS, or v10 (tested).

Requires Serverless Framework v1.x. If you are new to the Serverless Framework, check out the Serverless Framework Getting Started Guide.

Install

Install with yarn (yarn add serverless-aws-static-file-handler) or npm (npm install serverless-aws-static-file-handler --save-prod)

Features

Contributing 🤝

This is a community project. We invite your participation through issues and pull requests! You can peruse the contributing guidelines.

Show your support

Give a ⭐️ if this project helped you!

Release Process (Deploying to NPM)

We use semantic-release to consistently release semver-compatible versions. This project deploys to multiple npm distribution tags. Each of the below branches correspond to the following npm distribution tags:

branchnpm distribution tag
mainlatest
betabeta

To trigger a release use a Conventional Commit following Angular Commit Message Conventions on one of the above branches.

License 📝

Copyright © 2017 scott@willeke.com.

This project is MIT licensed.