|
1 |
| -This project is based on [Create React App](https://github.com/facebookincubator/create-react-app). |
| 1 | +This project is based on [Create React App](https://github.com/facebookincubator/create-react-app). (For more information about Create react App, check their full [documentation](https://github.com/facebookincubator/create-react-app#create-react-app).) |
2 | 2 |
|
3 |
| -You can follow the docs there to see the full docs on Create React App. |
4 |
| - |
5 |
| -The main addition is a new folder: `src/lambda`. Each js file in there will automatically be deployed and routed as an AWS lambda function by Netlify's continuous deployments. |
| 3 | +The main addition is a new folder: `src/lambda`. Each JavaScript file in there will automatically be prepared for Lambda function deployment. |
6 | 4 |
|
7 | 5 | As an example, we've included a small `src/lambda/hello.js` function, which will be deployed to `/.netlify/functions/hello`.
|
8 | 6 |
|
9 |
| -Each function must export a `handler( event, context, callback )` (eaxtly as if the Lambda had been configured with AWS's API Gateway). |
| 7 | +## Babel/webpack compilation |
10 | 8 |
|
11 |
| -The event object looks like: |
| 9 | +All functions are compiled with webpack using the Babel Loader, so you can use modern JavaScript, import npm modules, etc., without any extra setup. |
12 | 10 |
|
13 |
| -```js |
14 |
| -{ |
15 |
| - "path": "/.netlify/functions/hello", |
16 |
| - "httpMethod": "GET" |
17 |
| - "headers": {...} |
18 |
| - "queryStringParameters": {...} |
19 |
| - "body": "Request Body" |
20 |
| - "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode" |
21 |
| -} |
22 |
| -``` |
| 11 | +## Local Development |
23 | 12 |
|
24 |
| -our handler should use the callback to return either an error (as the first parameter) or a response object: |
| 13 | +Before developing, clone the repository and run `yarn` from the root of the repo to install all dependencies. |
25 | 14 |
|
26 |
| -```js |
27 |
| -{ |
28 |
| - "isBase64Encoded": true|false, |
29 |
| - "statusCode": httpStatusCode, |
30 |
| - "headers": { "headerName": "headerValue", ... }, |
31 |
| - "body": "..." |
32 |
| -} |
33 |
| -``` |
| 15 | +### Run the functions dev server |
34 | 16 |
|
35 |
| -Here’s a simple example function `hello.js`: |
| 17 | +From inside the project folder, run: |
36 | 18 |
|
37 |
| -```js |
38 |
| -exports.handler = function(event, context, callback) { |
39 |
| - callback(null, { |
40 |
| - statusCode: 200, |
41 |
| - body: "Hello, World" |
42 |
| - }); |
43 |
| -} |
| 19 | +``` |
| 20 | +yarn start:lambda |
44 | 21 | ```
|
45 | 22 |
|
46 |
| -## Babel/Webpack compilation |
47 |
| - |
48 |
| -All functions are compiled with webpack using the Babel Loader, so you can use modern JS, import npm modules, etc, without any extra setup. |
| 23 | +This will open a local server running at `http://localhost:9000` serving your Lambda functions, updating as you make changes in the `src/lambda` folder. |
49 | 24 |
|
50 |
| -## Local Development |
| 25 | +You can then access your functions directly at `http://localhost:9000/{function_name}`, but to access them with the app, you'll need to start the app dev server. |
51 | 26 |
|
52 |
| -Before developing, clone the repository and run `yarn` from the root of the repo. |
| 27 | +### Run the app dev server |
53 | 28 |
|
54 |
| -Then open one terminal tab and run: |
| 29 | +While the functions server is still running, open a new terminal tab and run: |
55 | 30 |
|
56 | 31 | ```
|
57 | 32 | yarn start
|
58 | 33 | ```
|
59 | 34 |
|
60 |
| -This will start the normal create-react-app dev server and open your app at `http://localhost:3000` |
61 |
| - |
62 |
| -Then open a separate terminal tab and run: |
63 |
| - |
64 |
| -``` |
65 |
| -yarn start:lambda |
66 |
| -``` |
67 |
| - |
68 |
| -This will open a local server running at `http://localhost:9000` serving your lambda functions. |
| 35 | +This will start the normal create-react-app dev server and open your app at `http://localhost:3000`. |
69 | 36 |
|
70 |
| -Requests to `http://localhost:3000/.netlify/functions/*` will automatically be proxied to the lambda dev server. |
| 37 | +Local in-app requests to the relative path `/.netlify/functions/*` will automatically be proxied to the local functions dev server. |
0 commit comments