-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_order.js
52 lines (46 loc) · 1.26 KB
/
process_order.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
var http = require('http');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var formater = require('./orderFormater');
// keep these empty when committing, shouldn't exactly
// be public knowledge
const SERVICE_EMAIL = '';
const SERVICE_PASSWORD = '';
const SERVICE_TARGET_EMAIL = '';
const MAIL_SERVICE = 'gmail';
const jsonParser = bodyParser.json();
const transporter = nodemailer.createTransport({
service: MAIL_SERVICE,
auth: {
user: SERVICE_EMAIL,
pass: SERVICE_PASSWORD
}
});
const requestHandler = (req, res) => {
if(req.method === 'POST') {
jsonParser(req, res, (error) => {
const formattedOrder = formater.formatOrder(req.body);
const mailOptions = {
from: SERVICE_EMAIL,
to: SERVICE_TARGET_EMAIL,
subject: `New order from ${req.body.customerName}`,
text: formattedOrder
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
res.statusCode = 500;
res.end();
} else {
res.statusCode = 200;
res.end();
}
});
});
}
}
const server = http.createServer(requestHandler);
server.listen(8080, (err) => {
if (err) {
return console.log("An error occured:\n", err);
}
});