Skip to content

Commit 86cf1c3

Browse files
committed
add simple dynamic html example
1 parent 47d68a0 commit 86cf1c3

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

aws-node-serve-dynamic-html/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Serving Dynamic HTML via API Gateway Example
2+
3+
These examples illustrate how to hookup an API Gateway endpoint to a Lambda function to render HTML on a `GET` request.
4+
5+
Instead of returning the default `json` from requests to an endpoint, you can display custom dynamic HTML by setting the `Content-Type` header.
6+
7+
```js
8+
const response = {
9+
statusCode: 200,
10+
headers: {
11+
'Content-Type': 'text/html',
12+
},
13+
body: html,
14+
};
15+
// callback will send HTML back
16+
callback(null, response);
17+
```
18+
19+
## Use-cases
20+
21+
- landing pages for marketing activities
22+
- dynamic single use webpages
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
// Your function handler
3+
module.exports.serveHtml = (event, context, callback) => {
4+
let dynamicHtml;
5+
/* check for GET params and use if available */
6+
if (event.queryStringParameters && event.queryStringParameters.name) {
7+
// yourendpoint.com/{stage}/landing-page?name=bob
8+
dynamicHtml = `<p>Hey ${event.queryStringParameters.name}</p>`;
9+
} else {
10+
dynamicHtml = '';
11+
}
12+
13+
const html = `
14+
<html>
15+
<style>
16+
h1 { color: blue; }
17+
</style>
18+
<body>
19+
<h1>Landing Page</h1>
20+
${dynamicHtml}
21+
</body>
22+
</html>`;
23+
24+
const response = {
25+
statusCode: 200,
26+
headers: {
27+
'Content-Type': 'text/html',
28+
},
29+
body: html,
30+
};
31+
// callback will send HTML back
32+
callback(null, response);
33+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Serving HTML through API Gateway for AWS Lambda
2+
service: serve-html
3+
4+
provider:
5+
name: aws
6+
runtime: nodejs4.3
7+
8+
functions:
9+
staticHtml:
10+
handler: handler.serveHtml
11+
events:
12+
- http:
13+
method: get
14+
path: landing-page

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy