-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (59 loc) · 1.4 KB
/
server.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
const express = require('express');
const nodemailer = require('nodemailer');
const bodyParser = require('body-parser');
const settings = require('./settings.json');
const transport = nodemailer.createTransport(settings.nodemailer);
const app = express();
function escape(s) {
return s
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.get('/', (req, res) => res.sendFile('./public/index.html'));
app.post('/contact', (req, res) => {
const {
name,
email,
phone,
message,
} = req.body;
if(!name || !email || !message) {
res.sendStatus(400);
} else {
const html = `
<b>Name:</b> ${escape(name)}<br/>
<b>Email:</b> ${escape(email)}<br/>
${!!phone ?
`<b>Phone:</b> ${escape(phone)}<br/>`
: ''}
<b>Message:</b> ${escape(message)}<br/>
`;
const mailOptions = {
to: settings.email,
subject: settings.subject,
from: settings.from,
html,
};
transport.sendMail(mailOptions, (err) => {
if(err) {
console.error("Failed to send email:", err);
res.sendStatus(500);
} else {
res.sendStatus(200);
}
});
}
});
app.listen(3000, (err) => {
if(err) {
console.err("Server error:", err);
} else {
console.info("Listening on port 3000");
}
});