-
Notifications
You must be signed in to change notification settings - Fork 4
/
form-handler.js
44 lines (38 loc) · 900 Bytes
/
form-handler.js
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
40
41
42
43
44
import { parse } from 'querystring'
exports.handler = (event, context, callback) => {
let body = {}
console.log(event)
try {
body = JSON.parse(event.body)
} catch (e) {
body = parse(event.body)
}
// Bail if email is missing
if (!body.email) {
return callback(null, {
statusCode: 400,
body: JSON.stringify({
error: 'missing email'
})
})
}
// Do my email subscription logic
if (event.headers['content-type'] === 'application/x-www-form-urlencoded') {
// Do redirect for non JS enabled browsers
return callback(null, {
statusCode: 302,
headers: {
Location: '/thanks.html',
'Cache-Control': 'no-cache',
},
body: JSON.stringify({})
})
}
// Return data to AJAX request
return callback(null, {
statusCode: 200,
body: JSON.stringify({
emailAdded: true
})
})
}