|
| 1 | +> ⚠️You may not need `netlify-lambda`. [Netlify Dev](https://github.com/netlify/netlify-dev-plugin) works with `create-react-app` out of the box, give it a try! Only use `netlify-lambda` if you need a build step for your functions. |
| 2 | +
|
1 | 3 | This project is based on [Create React App v2](https://github.com/facebookincubator/create-react-app) and [netlify-lambda v1](https://github.com/netlify/netlify-lambda). (For more information about Create react App, check their full [documentation](https://github.com/facebookincubator/create-react-app#create-react-app).)
|
2 | 4 |
|
3 |
| -The main addition is a new folder: `src/lambda`. Each JavaScript file in there will automatically be prepared for Lambda function deployment. |
| 5 | +The main addition to base Create-React-App is a new folder: `src/lambda`. Each JavaScript file in there will automatically be prepared for Lambda function deployment. |
4 | 6 |
|
5 | 7 | As an example, we've included a small `src/lambda/hello.js` function, which will be deployed to `/.netlify/functions/hello`. We've also included an async lambda example using async/await syntax in `async-chuck-norris.js`.
|
6 | 8 |
|
7 | 9 | [](https://app.netlify.com/start/deploy?repository=https://github.com/netlify/create-react-app-lambda)
|
8 | 10 |
|
9 | 11 | ## Video
|
10 | 12 |
|
11 |
| -Learn how to set this up yourself (and why everything is the way it is) from scratch in a video: https://www.youtube.com/watch?v=3ldSM98nCHI |
| 13 | +Learn how to set this up yourself (and why everything is the way it is) from scratch in a video: https://www.youtube.com/watch?v=3ldSM98nCHI |
12 | 14 |
|
13 | 15 | ## Babel/webpack compilation
|
14 | 16 |
|
@@ -67,31 +69,27 @@ You can use Typescript in both your React code (with `react-scripts` v2.1+) and
|
67 | 69 | 3. use types in your event handler:
|
68 | 70 |
|
69 | 71 | ```ts
|
70 |
| -import { Handler, Context, Callback, APIGatewayEvent } from 'aws-lambda'; |
| 72 | +import { Handler, Context, Callback, APIGatewayEvent } from "aws-lambda" |
71 | 73 |
|
72 | 74 | interface HelloResponse {
|
73 |
| - statusCode: number; |
74 |
| - body: string; |
| 75 | + statusCode: number |
| 76 | + body: string |
75 | 77 | }
|
76 | 78 |
|
77 |
| -const handler: Handler = ( |
78 |
| - event: APIGatewayEvent, |
79 |
| - context: Context, |
80 |
| - callback: Callback |
81 |
| -) => { |
82 |
| - const params = event.queryStringParameters; |
| 79 | +const handler: Handler = (event: APIGatewayEvent, context: Context, callback: Callback) => { |
| 80 | + const params = event.queryStringParameters |
83 | 81 | const response: HelloResponse = {
|
84 | 82 | statusCode: 200,
|
85 | 83 | body: JSON.stringify({
|
86 | 84 | msg: `Hello world ${Math.floor(Math.random() * 10)}`,
|
87 | 85 | params
|
88 | 86 | })
|
89 |
| - }; |
| 87 | + } |
90 | 88 |
|
91 |
| - callback(undefined, response); |
92 |
| -}; |
| 89 | + callback(undefined, response) |
| 90 | +} |
93 | 91 |
|
94 |
| -export { handler }; |
| 92 | +export { handler } |
95 | 93 | ```
|
96 | 94 |
|
97 | 95 | rerun and see it work!
|
|
0 commit comments