Lambda Static HTML Page

JS
S
JavaScript

Simple setup with cache enabled for serving static HTML pages

1'use strict';
2
3const Promise = require('bluebird');
4const fs = Promise.promisifyAll(require('fs'));
5
6// Cached global var
7var html;
8
9const loadHtml = async () => {
10  if (!html) {
11    return await fs.readFileAsync('static/index.html', 'utf-8');
12  }
13  return html;
14}
15
16module.exports.handler = async (event, context) => {
17  let html = await loadHtml();
18  console.log('got html', html);
19  return {
20    statusCode: 200,
21    body: html,
22    headers: {
23      'Content-Type': 'text/html; charset=UTF-8'
24    }
25  };
26};
27
28
29
30// Serverless YAML
31/*
32service: myapp
33
34provider:
35  name: aws
36  runtime: nodejs8.10
37
38functions:
39  hello:
40    handler: functions/get-index.handler
41    events:
42      - http:
43          path: /
44          method: get
45*/

Created on 1/29/2019