Skip to content

Commit ad2f94b

Browse files
authored
feat: Improve aws-node-serve-dynamic-html-via-http-endpoint (serverless#711)
1 parent f8d8245 commit ad2f94b

File tree

4 files changed

+30
-34
lines changed

4 files changed

+30
-34
lines changed

aws-node-serve-dynamic-html-via-http-endpoint/README.md

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
title: 'AWS Serving Dynamic HTML via API Gateway example in NodeJS'
33
description: 'This example illustrates how to hookup an API Gateway endpoint to a Lambda function to render HTML on a GET request.'
44
layout: Doc
5-
framework: v1
5+
framework: v3
66
platform: AWS
77
language: nodeJS
88
priority: 10
99
authorLink: 'https://github.com/slate71'
1010
authorName: 'Lukas Andersen'
1111
authorAvatar: 'https://avatars0.githubusercontent.com/u/2078561?v=4&s=140'
1212
-->
13+
1314
# Serving Dynamic HTML via API Gateway Example
1415

1516
This example illustrates how to hookup an API Gateway endpoint to a Lambda function to render HTML on a `GET` request.
@@ -27,7 +28,7 @@ Instead of returning the default `json` from a request, you can display custom d
2728
const response = {
2829
statusCode: 200,
2930
headers: {
30-
'Content-Type': 'text/html',
31+
"Content-Type": "text/html",
3132
},
3233
body: html,
3334
};
@@ -46,36 +47,21 @@ serverless deploy
4647
The expected result should be similar to:
4748

4849
```bash
49-
Serverless: Creating Stack...
50-
Serverless: Checking Stack create progress...
51-
.....
52-
Serverless: Stack create finished...
53-
Serverless: Packaging service...
54-
Serverless: Uploading CloudFormation file to S3...
55-
Serverless: Uploading service .zip file to S3 (1.01 KB)...
56-
Serverless: Updating Stack...
57-
Serverless: Checking Stack update progress...
58-
...........................
59-
Serverless: Stack update finished...
60-
61-
Service Information
62-
service: serve-dynamic-html-via-http-endpoint
63-
stage: dev
64-
region: us-east-1
65-
api keys:
66-
None
67-
endpoints:
68-
GET - https://nzkl1kas89.execute-api.us-east-1.amazonaws.com/dev/landing-page
50+
Deploying serve-dynamic-html-via-http to stage dev (us-east-1)
51+
52+
✔ Service deployed to stack serve-dynamic-html-via-http-dev (113s)
53+
54+
endpoint: GET - https://XXXXXXXXXXX.execute-api.us-east-1.amazonaws.com/landing-page
6955
functions:
70-
serve-dynamic-html-via-http-endpoint-dev-landingPage: arn:aws:lambda:us-east-1:377024778620:function:serve-dynamic-html-via-http-endpoint-dev-landingPage
56+
landingPage: serve-dynamic-html-via-http-dev-landingPage (466 B)
7157
```
7258

7359
## Usage
7460

7561
You can now send an HTTP request directly to the endpoint using a tool like curl
7662

7763
```bash
78-
curl https://nzkl1kas89.execute-api.us-east-1.amazonaws.com/dev/landing-page?name=Nik%20Graf
64+
curl https://XXXXXXXXXXX.execute-api.us-east-1.amazonaws.com/dev/landing-page?name=Nik%20Graf
7965
```
8066

8167
The expected result should be similar to:

aws-node-serve-dynamic-html-via-http-endpoint/handler.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
'use strict';
1+
"use strict";
22

3-
module.exports.landingPage = (event, context, callback) => {
4-
let dynamicHtml = '<p>Hey Unknown!</p>';
3+
export const landingPage = (event, _context, callback) => {
4+
let dynamicHtml = "<p>Hey Unknown!</p>";
55
// check for GET params and use if available
66
if (event.queryStringParameters && event.queryStringParameters.name) {
77
dynamicHtml = `<p>Hey ${event.queryStringParameters.name}!</p>`;
@@ -21,11 +21,13 @@ module.exports.landingPage = (event, context, callback) => {
2121
const response = {
2222
statusCode: 200,
2323
headers: {
24-
'Content-Type': 'text/html',
24+
"Content-Type": "text/html",
2525
},
2626
body: html,
2727
};
2828

2929
// callback is sending HTML back
3030
callback(null, response);
3131
};
32+
33+
export default landingPage;

aws-node-serve-dynamic-html-via-http-endpoint/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33
"version": "1.0.0",
44
"description": "Hookup an AWS API Gateway endpoint to a Lambda function to render HTML on a `GET` request",
55
"author": "",
6-
"license": "MIT"
6+
"license": "MIT",
7+
"devDependencies": {
8+
"esbuild": "^0.14.53",
9+
"serverless": "^3.22.0",
10+
"serverless-esbuild": "^1.33.0"
11+
}
712
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# Serving HTML through API Gateway for AWS Lambda
2-
service: serve-dynamic-html-via-http-endpoint
2+
service: serve-dynamic-html-via-http
33

4-
frameworkVersion: ">=1.1.0 <2.0.0"
4+
frameworkVersion: "3"
55

66
provider:
77
name: aws
8-
runtime: nodejs12.x
8+
runtime: nodejs16.x
99

1010
functions:
1111
landingPage:
1212
handler: handler.landingPage
1313
events:
14-
- http:
14+
- httpApi:
1515
method: get
16-
path: landing-page
16+
path: /landing-page
17+
18+
plugins:
19+
- serverless-esbuild

0 commit comments

Comments
 (0)