Skip to content

Commit be2de4a

Browse files
authored
add functions lists and simplify example functions
1 parent 9e5e0ef commit be2de4a

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

README.md

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,26 @@ exports.handler = function(event, context, callback) {
7272
or you can use async/await:
7373

7474
```js
75-
import fetch from 'node-fetch';
7675
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-
8576
return {
8677
statusCode: 200,
87-
body: JSON.stringify({ msg: data.value })
78+
body: JSON.stringify({ message: `Hello world ${Math.floor(Math.random() * 10)}` })
8879
};
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-
}
9680
}
9781
```
9882

99-
`async/await` is nicer :) just return an object
83+
For more Functions examples, check:
84+
85+
- https://functions-playground.netlify.com/ (introductory)
86+
- https://functions.netlify.com/examples/ (our firehose of all functions examples)
87+
- the blogposts at the bottom of this README
10088

10189
</details>
10290

10391
## Using with `create-react-app`, Gatsby, and other development servers
10492

93+
### Why you need to proxy (for beginners)
94+
10595
`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:
10696

10797
`Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0`
@@ -140,7 +130,7 @@ module.exports = {
140130

141131
<details>
142132
<summary>
143-
**Using with `Angular CLI`**
133+
<b>Using with `Angular CLI`</b>
144134
</summary>
145135

146136
CORS issues when trying to use netlify-lambdas locally with angular? you need to set up a proxy.
@@ -221,14 +211,7 @@ You may also want to add `typescript @types/node @types/aws-lambda`.
221211
{
222212
"presets": [
223213
"@babel/preset-typescript",
224-
[
225-
"@babel/preset-env",
226-
{
227-
"targets": {
228-
"node": "6.10.3"
229-
}
230-
}
231-
]
214+
"@babel/preset-env"
232215
],
233216
"plugins": [
234217
"@babel/plugin-proposal-class-properties",
@@ -285,7 +268,7 @@ Minor note: For the `identity` field, since we are not fully emulating Netlify I
285268
You can do a great deal with lambda functions! Here are some examples for inspiration:
286269

287270
- Basic Netlify Functions tutorial: https://flaviocopes.com/netlify-functions/
288-
- Netlify's list of Function examples: https://functions-playground.netlify.com/ ([Even more in the README](https://github.com/netlify/functions))
271+
- Netlify's list of Function examples: https://functions-playground.netlify.com/ ([Even more in the README](https://github.com/netlify/functions) as well as our full list https://functions.netlify.com/examples/)
289272
- Slack Notifications: https://css-tricks.com/forms-auth-and-serverless-functions-on-gatsby-and-netlify/#article-header-id-9
290273
- URL Shortener: https://www.netlify.com/blog/2018/03/19/create-your-own-url-shortener-with-netlifys-forms-and-functions/
291274
- Gatsby + Netlify Identity + Functions: [Turning the Static Dynamic: Gatsby + Netlify Functions + Netlify Identity](https://www.gatsbyjs.org/blog/2018-12-17-turning-the-static-dynamic/)
@@ -309,6 +292,12 @@ If you wish to emulate more Netlify functionality locally, check this repo: http
309292

310293
All of the above are community maintained and not officially supported by Netlify.
311294

295+
## Changelog
296+
297+
- v1.0: https://twitter.com/Netlify/status/1050399820484087815 Webpack 4 and Babel 7
298+
- v1.1: https://twitter.com/swyx/status/1069544181259849729 Typescript support
299+
- v1.2: https://twitter.com/swyx/status/1083446733374337024 Identity emulation (& others)
300+
312301
## License
313302

314303
[MIT](LICENSE)

0 commit comments

Comments
 (0)