Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created January 26, 2019 01:37
Show Gist options
  • Select an option

  • Save DavidWells/28c59d7fe7e01b1465aa0540b5ba6ac0 to your computer and use it in GitHub Desktop.

Select an option

Save DavidWells/28c59d7fe7e01b1465aa0540b5ba6ac0 to your computer and use it in GitHub Desktop.

Revisions

  1. DavidWells created this gist Jan 26, 2019.
    39 changes: 39 additions & 0 deletions dynamic-html-lambda.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    module.exports = (event, context, callback) => {
    let name
    if (event.pathParameters && event.pathParameters.name) {
    name = event.pathParameters.name
    }

    /* generate the hello paragraph */
    const helloParagraph = greetPerson(name)

    // callback is sending HTML back
    return callback(null, {
    statusCode: 200,
    headers: {
    'Content-Type': 'text/html',
    },
    body: generateHtmlPage(helloParagraph),
    })
    }

    /* Utility function for rendering HTML */
    function generateHtmlPage(content) {
    /* for security always escape output html */
    // const safeValues = escapeHtml(content)
    return `
    <html>
    <style>
    h1 { color: #73757d }
    </style>
    <body>
    ${content}
    </body>
    </html>`
    }

    /* Utility function for rendering hello message HTML */
    function greetPerson(name) {
    const userName = name || 'Unknown Person!'
    return `<p>Hey ${userName}!</p>`
    }