-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (58 loc) · 1.85 KB
/
app.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
const express = require('express');
const request = require('request');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
// Bodyparser Middleware
app.use(bodyParser.urlencoded({ extended: true }));
// Static folder
app.use(express.static(path.join(__dirname, 'public')));
// allow cross origin
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Signup Route
app.get('/subscribe-news-letter', (req, res) => {
const { firstName, email } = req.query;
// Make sure fields are filled
if (!firstName || !email) {
res.status(422).send({status: false, message: 'Please enter the required field data'});
return;
}
// Construct req data
const data = {
members: [
{
email_address: email,
status: 'subscribed',
merge_fields: {
FNAME: firstName
}
}
]
};
const postData = JSON.stringify(data);
const options = {
url: `https://us20.api.mailchimp.com/3.0/lists/${process.env.LIST_ID}`,
method: 'POST',
headers: {
Authorization: `auth ${process.env.API_KEY}`
},
body: postData
};
request(options, (err, response) => {
if (err) {
res.status(500).send({status: false, message: 'Unable to subscribe you to the news letter, please try again'});
} else {
if (response.statusCode === 200) {
res.send({status: true, message: 'You have successfully subscribed to Pills Of Code news letter, thank you!'});
} else {
res.status(500).send({status: false, message: 'Unable to subscribe you to the news letter, please try again'});
}
}
});
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, console.log(`Server started on ${PORT}`));