-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
75 lines (57 loc) · 1.55 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
68
69
70
71
72
73
74
75
import express from 'express';
import webpack from 'webpack';
import path from 'path';
import config from '../webpack.config';
import open from 'open';
import bodyParser from 'body-parser';
import nodemailer from 'nodemailer';
import nodemailerTransport from 'nodemailer-mailgun-transport';
import ENV from 'dotenv';
ENV.config();
const auth ={
auth: {
api_key: process.env.REACT_APP_MAILGUN_API_KEY,
domain: process.env.REACT_APP_MAILGUN_DOMAIN
}
}
const nodemailerMailgun = nodemailer.createTransport(nodemailerTransport(auth));
const port = 3000;
const app = express();
const compiler = webpack(config);
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
app.get('*', function(req, res) {
res.sendFile(path.join( __dirname, '../public/index.html'));
});
app.post('/inbox', (req,res) => {
req.setTimeout(0);
nodemailerMailgun.sendMail({
from: req.body.email,
to: process.env.REACT_APP_DOMAIN_EMAIL,
subject: req.body.subject,
html: `<p>${req.body.message}</p>`,
text: 'Mailgun rocks, pow pow!'
}, function (err, info) {
if (err) {
console.log('Error: ' + err);
res.json(err);
}
else {
console.log('Response: ' + info);
res.json(info);
}
});
})
app.listen(port, function(err) {
if (err) {
console.log(err.response);
} else {
console.log("Server is running");
open(`http://localhost:${port}`);
}
});