-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (37 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
const express = require('express');
const cors = require('cors');
const sendNotFoundResponse = require('./lib/sendNotFoundResponse');
const isValidEmail = require('./lib/isValidEmail');
const sendEmail = require('./lib/sendEmail');
const app = express();
app.use(cors());
app.use(express.json());
app.get('/*', (req, res) => sendNotFoundResponse(res));
app.post('/feedback/create', async (req, res) => {
const { feedback, sender_email: senderEmail } = req.body;
if (!feedback) {
return res.status(400).json({
error_description: 'No feedback provided.'
});
}
const senderEmailIsValid = isValidEmail(senderEmail);
await sendEmail({
feedback,
senderEmail: senderEmailIsValid ? senderEmail : null
});
if (senderEmail && !senderEmailIsValid) {
return res.json({
success: true,
warning_description:
'Note: the sender e-mail address was excluded from your feedback since it was invalid.'
});
}
res.json({
success: true
});
});
app.post('/*', (req, res) => sendNotFoundResponse(res));
const PORT = 8080 || process.env.PORT;
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});