forked from netlify/create-react-app-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync-dadjoke.js
More file actions
39 lines (35 loc) · 1.03 KB
/
async-dadjoke.js
File metadata and controls
39 lines (35 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// example of async handler using async-await
// https://github.com/netlify/netlify-lambda/issues/43#issuecomment-444618311
import axios from "axios"
const API = "https://icanhazdadjoke.com/"
export async function handler(_event, _context) {
try {
const { data } = await axios.get(API, { headers: { Accept: "application/json" } })
return {
statusCode: 200,
body: data.joke,
}
} catch (err) {
return {
statusCode: 422,
body: String(err),
}
}
}
// export async function handler(event, context) {
// try {
// const { data } = await axios.get("https://icanhazdadjoke.com", {
// headers: { Accept: "application/json" },
// })
// return {
// statusCode: 200,
// body: JSON.stringify({ msg: data.joke }),
// }
// } catch (err) {
// console.log(err) // output to netlify function log
// return {
// statusCode: 500,
// body: JSON.stringify({ msg: err.message }), // Could be a custom message or object i.e. JSON.stringify(err)
// }
// }
// }