-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
92 lines (70 loc) · 2.68 KB
/
app.js
File metadata and controls
92 lines (70 loc) · 2.68 KB
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
// https://mailchimp.com/developer/marketing/docs/fundamentals/
// https://mailchimp.com/developer/marketing/api/ecommerce-orders/
// https://mailchimp.com/developer/marketing/api/lists/
// https://mailchimp.com/developer/marketing/guides/access-user-data-oauth-2/?_ga=2.230154139.609880322.1709910708-1755440482.1709910707
const express = require("express");
const bodyParser = require("body-parser");
const https = require("https");
require('dotenv').config();
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
const apiKey = process.env.MAILCHIMP_API_KEY;
const audienceId = process.env.MAILCHIMP_AUDIENCE_ID;
app.get("/", function(req, res) {
res.sendFile(__dirname + "/signup.html");
});
// Simplified object destructuring to directly extract fName, lName, and email
app.post("/", function(req, res) {
const { fName, lName, email } = req.body;
// will need to send JSON object.
//firstly, create javascript object. With key, value pairs that mailchimp is expecting.
const data = {
members: [ //array of objects
{
email_address: email, // the email from the post request (the form)
status: "subscribed",
merge_fields: { //from admin.mailchimp.com/lists/settings/merge-tags
FNAME: fName,
LNAME: lName
}
}
]
};
const jsonData = JSON.stringify(data); //convert the javascript object to a JSON string, that mailchimp is expecting.
const url = "https://us21.api.mailchimp.com/3.0/lists/9df456b62d";
// options for my http post request
const options = {
method: "POST",
auth: `shani:${process.env.MAILCHIMP_API_KEY}`
};
// Post Request to mailchimp server (url, options above, callback function from mailchimp server)
const mailchimpRequest = https.request(url, options, function(response) {
let responseData = "";
response.on("data", function(chunk) {
responseData += chunk;
});
response.on("end", function() {
const responseDataObject = JSON.parse(responseData);
if (response.statusCode === 200) {
res.sendFile(__dirname + "/success.html");
} else {
console.error("Mailchimp API Error:", responseDataObject);
res.sendFile(__dirname + "/failure.html");
}
});
});
mailchimpRequest.on("error", function(error) {
console.error("Mailchimp API Request Error:", error);
res.sendFile(__dirname + "/error.html");
});
mailchimpRequest.write(jsonData);
mailchimpRequest.end();
});
// post request to catch the failure route
app.post("/failure", function(req, res) {
res.redirect("/");
});
app.listen(3000, function() {
console.log("Server is running on port 3000");
});