-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (47 loc) · 1.15 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require('dotenv').config()
const express = require('express')
const crypto = require('crypto')
const chart = require('./lib/chart')
const mail = require('./lib/mail')
const template = require('./lib/template')
const db = require('./lib/db')
const app = express()
app.use(
express.json({
verify: (req, res, buf) => {
req.rawBody = buf
},
})
)
app.use(express.static('public'))
app.post('/progress', async (req, res) => {
const expectedSig = req.header('Typeform-Signature')
const hash = crypto
.createHmac('sha256', process.env.TYPEFORM_SECRET)
.update(req.rawBody)
.digest('base64')
const actualSig = `sha256=${hash}`
if (actualSig !== expectedSig) {
res.status(403).send('invalid')
return
}
const answers = req.body.form_response.answers
await db.store(answers)
const chartAsset = await chart(answers)
await mail(
{ answers, chart: chartAsset },
{
bcc: process.env.EMAIL,
template,
}
)
res.send('ok')
})
app.get('/', (req, res) => {
res.sendFile('index.html', {
root: './templates',
})
})
app.listen(3000, () => {
console.log(`Example app listening on port 3000!`)
})