-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (48 loc) · 1.63 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
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const app = express();
const nodemailer = require('nodemailer');
const config = require('./config');
// Parsers
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Angular DIST output folder
app.use(express.static(path.join(__dirname, 'dist/Portfolio-Rebuild')));
// Send all other requests to the Angular app
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/Portfolio-Rebuild/index.html'));
});
//Set Port
const port = process.env.PORT || '8080';
app.set('port', port);
app.post('/email', function(req, res) {
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: config.email,
pass: config.pass
}
});
var body = req.body;
var params = {
from: body.email,
to: config.email,
subject: 'New submission from ' + body.name,
text: "Sent from: " + body.email + "\n\nMessage:\n" + body.message
};
transporter.sendMail(params, (mailError, mailReponse) => {
var arrResponse = '';
if (mailError) {
arrResponse = { 'status': 'Master, I have failed you...', 'error': mailError };
} else {
arrResponse = { 'status': 'Check your mailbox', 'data': mailReponse.accepted };
}
res.status(200).send(arrResponse.status);
});
});
const server = http.createServer(app);
server.listen(port, () => console.log(`Running on localhost:${port}`));