-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
74 lines (65 loc) · 2.21 KB
/
api.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { join } from 'path'
import express from 'express'
import legit from 'legit'
import { sendEmail } from './email'
import { t, setLocale } from './translations'
const app = express()
const appStart = (conf) => {
if (typeof conf !== 'object') {
throw 'Configuration is not an object'
}
app.use(require('compression')())
app.use(require('body-parser').json())
app.use(require('cors')())
app.get('/favicon.ico', (req, res) => {
res.sendFile(join(__dirname, 'static', 'favicon.ico'))
})
app.get('/', (req, res) => {
res.sendFile(join(__dirname, 'static', 'index.html'))
})
app.post('/contactus', (req, res) => {
const email = typeof req.body.email === 'string' ? req.body.email : false
const msg = typeof req.body.msg === 'string' ? req.body.msg : false
const name = typeof req.body.name === 'string' ? req.body.name : false
const locale = typeof req.body.locale === 'string' ? req.body.locale : 'en'
const validKey = req.body.key ? req.body.key === conf.clientKey : false
if (validKey) {
if (locale !== 'en') {
setLocale(locale)
}
if (email && msg && name) {
legit(email).then((response) => {
if (response.isValid) {
const subject = `Message from: ${name}`
sendEmail(email, subject, msg, conf, (err) => {
res.setHeader('Content-Type', 'application/json')
if (!err.error) {
res.end(JSON.stringify({ status: 'sent' })) // don't translate this
} else {
res.end(JSON.stringify({ status: t('send_error', { error: err.error }) }))
}
})
} else {
res.end(JSON.stringify({ status: t('email_error') }))
}
}).catch((err) => {
res.end(JSON.stringify({ status: t('email_check_error', { error: err.message }) }))
})
} else {
res.end(JSON.stringify({ status: t('error_required') }))
}
} else {
res.end(JSON.stringify({ status: t('unauthorized') }))
}
})
app.get('/ping', (req, res) => {
res.send('OK')
})
return app
}
/*
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err.message}`)
})
*/
export default appStart