File tree Expand file tree Collapse file tree 3 files changed +69
-0
lines changed
aws-node-serve-dynamic-html Expand file tree Collapse file tree 3 files changed +69
-0
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments