-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
70 lines (57 loc) · 1.92 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
//jshint esversion:6
// Deployed: https://ali-hamxa-maillist.herokuapp.com/
// Mailchimp docs: https://mailchimp.com/developer/marketing/
// Required Modules
const express = require("express");
const https = require("https");
// Create server and use modules
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.static("public"));
// Routes for get and post requests and responses
app.get("/", (req, res) => {
res.sendFile(__dirname + "/public/signup.html");
});
app.post("/", (req, res) => {
// Parse parameters from POST request
const fName = req.body.fName;
const lName = req.body.lName;
const email = req.body.email;
// Prepare url, options and data for Mailchimp request and response
const url = "https://{server}.api.mailchimp.com/3.0/lists/{list_id}";
const options = {
method:"POST",
auth:"<username>:<api_key>"
}
// Mailchimp memebers data
const data = {
members:[
{
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: fName,
LNAME: lName
}
}
]
}
// Convert data into packed JSON string
const jsonData = JSON.stringify(data);
// Call Mailchimp Api and write data
// if sccuessfull, display success else failure
const request = https.request(url, options, function(response){
if (response.statusCode === 200)
res.sendFile(__dirname + "/public/success.html");
else
res.sendFile(__dirname + "/public/failure.html");
});
request.write(jsonData);
request.end();
});
// Redirect to '/', if something went wrong
app.post("/failure", function(req, res) {
res.redirect("/");
});
// Listener for server for both Heroku and localhost
app.listen(process.env.PORT || 3000, console.log("Server started at port: 3000..."));