Skip to content

Commit 9e5e0ef

Browse files
authored
Update README.md
1 parent e97c0ee commit 9e5e0ef

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Diff for: README.md

+49
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,55 @@ It also watches your files and restarts the dev server on change. Note: if you a
5151
- Every function needs to be a top-level js/ts/mjs file. You can have subfolders inside the `netlify-lambda` folder, but those are only for supporting files to be imported by your top level function.
5252
- Function signatures follow the [AWS event handler](https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html) syntax but must be named `handler`. [We use Node v8](https://www.netlify.com/blog/2018/04/03/node.js-8.10-now-available-in-netlify-functions/) so `async` functions **are** supported ([beware common mistakes](https://serverless.com/blog/common-node8-mistakes-in-lambda/)!). Read [Netlify Functions docs](https://www.netlify.com/docs/functions/#javascript-lambda-functions) for more info.
5353

54+
<details>
55+
<summary>
56+
<b>Lambda function examples</b>
57+
</summary>
58+
If you are new to writing Lambda functions, this section may help you. Function signatures should conform to one of either two styles. Traditional callback style:
59+
60+
```js
61+
exports.handler = function(event, context, callback) {
62+
// your server-side functionality
63+
callback(null, {
64+
statusCode: 200,
65+
body: JSON.stringify({
66+
message: `Hello world ${Math.floor(Math.random() * 10)}`
67+
})
68+
});
69+
};
70+
```
71+
72+
or you can use async/await:
73+
74+
```js
75+
import fetch from 'node-fetch';
76+
export async function handler(event, context) {
77+
try {
78+
const response = await fetch('https://api.chucknorris.io/jokes/random');
79+
if (!response.ok) {
80+
// NOT res.status >= 200 && res.status < 300
81+
return { statusCode: response.status, body: response.statusText };
82+
}
83+
const data = await response.json();
84+
85+
return {
86+
statusCode: 200,
87+
body: JSON.stringify({ msg: data.value })
88+
};
89+
} catch (err) {
90+
console.log(err); // output to netlify function log
91+
return {
92+
statusCode: 500,
93+
body: JSON.stringify({ msg: err.message }) // Could be a custom message or object i.e. JSON.stringify(err)
94+
};
95+
}
96+
}
97+
```
98+
99+
`async/await` is nicer :) just return an object
100+
101+
</details>
102+
54103
## Using with `create-react-app`, Gatsby, and other development servers
55104

56105
`react-scripts` (the underlying library for `create-react-app`) and other popular development servers often set up catchall serving for you; in other words, if you try to request a route that doesn't exist, the dev server will try to serve you `/index.html`. This is problematic when you are trying to hit a local API endpoint like `netlify-lambda` sets up for you - your browser will attempt to parse the `index.html` file as JSON. This is why you may see this error:

0 commit comments

Comments
 (0)