-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
93 lines (78 loc) · 2.66 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
const fs = require('fs'),
express = require('express'),
request = require('request'),
{ createBundleRenderer }= require('vue-server-renderer'),
csrf = require('csurf'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser');
// Mail chimp variables
const mailchimpInstance = process.env.MC_INSTANCE || 'us17',
mailchimpApiKey = process.env.MC_API_KEY || '';
listUniqueId = process.env.MC_UNIQUE_ID || '',
port = process.env.PORT || 8080;
const bundleRenderer = createBundleRenderer(
// Load the SSR bundle with require.
require('./dist/vue-ssr-bundle.json'),
{
// Yes, I know, readFileSync is bad practice. It's just shorter to read here.
template: fs.readFileSync('./index.html', 'utf-8')
}
);
// Create the express app.
const app = express();
app.set('trust proxy', true);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// Prevent CORS
app.use(csrf({ cookie: true }));
// Serve static assets from ./dist on the /dist route.
app.use('/dist', express.static('dist'));
app.use(function (req, res, next) {
var token = req.csrfToken();
res.cookie('XSRF-TOKEN', token);
res.locals.csrfToken = token;
next();
});
// Render all other routes with the bundleRenderer.
app.get('*', (req, res) => {
bundleRenderer
// Renders directly to the response stream.
// The argument is passed as "context" to main.server.js in the SSR bundle.
.renderToStream({ url: req.path, _csrf: req.csrfToken() })
.pipe(res);
});
app.post('/subscribe', (req, res) => {
const email = req.body.email;
if(email){
var options = {
url: 'https://' + mailchimpInstance + '.api.mailchimp.com/3.0/lists/' + listUniqueId + '/members/',
headers: {
'Content-Type': 'application/json;charset=utf-8',
'Authorization': 'Basic ' + new Buffer('any:' + mailchimpApiKey ).toString('base64')
},
body: JSON.stringify({
'email_address': email,
'status': 'subscribed'
})
};
// Mailchimp authentication
request
.post(options, (err, response, body) => {
if(err) {
console.log(err);
}
const rBody = JSON.parse(response.body);
if (rBody.status === 'subscribed' || (rBody.status === 400 && rBody.title === "Member Exists")) {
res.sendStatus(200);
} else {
console.log(rBody);
res.sendStatus(400);
}
});
} else {
res.sendStatus(422);
}
});
// Bind the app to this port.
app.listen(port);