-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
169 lines (150 loc) · 4.09 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* eslint-disable no-undef */
/* eslint-disable no-console */
/* eslint-disable no-unused-vars */
/* eslint-disable prefer-template */
/* eslint-disable no-path-concat */
/* eslint-disable prefer-destructuring */
require('dotenv').config();
const express = require('express');
const exhbs = require('express-handlebars');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const morgan = require('morgan');
const nodemailer = require('nodemailer');
const mg = require('nodemailer-mailgun-transport');
const serveStatic = require('serve-static');
const Email = require('./models/email');
const auth = {
auth: {
api_key: process.env.MAILGUN_API_KEY,
domain: process.env.DOMAIN,
},
};
const app = express();
const nodemailerMailgun = nodemailer.createTransport(mg(auth));
/* ************************
MIDDLEWARE
************************ */
app.use(bodyParser.urlencoded({
extended: true,
}));
app.use(express.static(__dirname + '/public'));
app.use(morgan('combined')); // display each request in the console
// parse application/json
app.use(bodyParser.json());
app.use(serveStatic('public/', {
index: ['index.html', 'email.html', 'thanks.html'],
}));
// ======= API calls =======
app.get('/', (req, res) => {
res.render('index');
});
// SHOW email form
app.get('/email', (req, res) => {
res.render('email');
});
app.get('/unsub-thanks', (req, res) => {
res.render('unsub-thanks.html');
});
// FIXME: error handling if user doesnt want to email
// also cancel and go back to home
// CREATE a new email
app.post('/email/new', (req, res) => {
console.log('Request: ', req.body);
const data = {
from: req.body.from,
subject: req.body.subject,
text: req.body.content,
};
nodemailerMailgun.sendMail({
from: req.body.from,
to: '[email protected]',
subject: req.body.subject,
template: {
name: 'email.hbs',
engine: 'handlebars',
context: data,
},
}).then((info) => {
console.log('Response: ', info);
res.redirect('/');
}).catch((err) => {
console.log('Error sending email: ', err.message);
});
});
// JOIN email list
app.post('/subscribe', (req, res) => {
const data = {
from: req.body.from,
subject: "You've Got a New Subscriber to Your Website!",
text: 'Please subscribe me to your list',
};
nodemailerMailgun.sendMail({
from: req.body.from,
to: '[email protected]',
subject: data.subject,
template: {
name: 'thanks.hbs',
engine: 'handlebars',
context: data,
},
}).then((info) => {
console.log('Response: ', info);
const email = new Email();
email.name = req.body.from;
email.save()
.then((savedEmail) => {
res.redirect('/thanks.html');
}).catch((error) => {
console.log('Error saving email', error.message);
// eslint-disable-next-line no-alert
alert('There was an error saving your email address. Please try again.');
res.redirect('/');
});
}).catch((err) => {
console.log('Error: ', err);
alert('Please enter a valid email address');
res.redirect('/');
});
});
app.get('/subscribe', (req, res) => {
res.redirect('/');
})
// UNJOIN email from list
app.post('/unsubscribe', (req, res) => {
const data = {
from: req.body.from,
subject: 'UNSUBSCRIBE',
text: 'Please unsubscribe me from your list',
};
nodemailerMailgun.sendMail({
from: req.body.from,
to: '[email protected]',
subject: data.subject,
template: {
name: 'email.hbs',
engine: 'handlebars',
context: data,
},
}).then((info) => {
// Delete
Email.findByIdAndRemove(req.params.id, req.body)
.then((removedEmail) => {
res.redirect('/unsub-thanks.html');
}).catch((err) => {
console.log(`Error: ${err.message}`);
});
});
});
// DATABASE
mongoose.connect(
process.env.MONGODB_URI, {
useNewUrlParser: true,
},
() => {
console.log('Connected to Calibrate Kombucha Database');
},
);
// SEVER
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}`));