Skip to content

Instantly share code, notes, and snippets.

@budparr
Forked from DavidWells/dynamic-html-lambda.js
Created January 26, 2019 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save budparr/d77b0653c1b1d2d8eb6271c5cea66acc to your computer and use it in GitHub Desktop.
Save budparr/d77b0653c1b1d2d8eb6271c5cea66acc to your computer and use it in GitHub Desktop.
Respond with dynamic HTML from a lambda function
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>`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment