-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (71 loc) · 3.16 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
71
72
73
74
75
76
77
78
//Requiring mailchimp's module
//For this we need to install the npm module @mailchimp/mailchimp_marketing. To do that we write:
//npm install @mailchimp/mailchimp_marketing
const mailchimp = require("@mailchimp/mailchimp_marketing");
//Requiring express and body parser and initializing the constant "app"
const express = require("express");
const bodyParser = require("body-parser");
const { post } = require("request");
const app = express();
//Using bod-parser
app.use(bodyParser.urlencoded({ extended: true }));
//The public folder which holds the CSS
app.use(express.static("public"));
//Listening on port 3000 and if it goes well then logging a message saying that the server is running
app.listen(process.env.PORT || 3000, () => {
console.log("Server is running at port 3000");
});
//Sending the signup.html file to the browser as soon as a request is made on localhost:3000
app.get("/", (req, res) => {
res.sendFile(__dirname + "/signup.html");
});
// //Redirect failure page to home route
// app, post("/failure", (req, res) => {
// res.redirect('/');
// });
//Setting up MailChimp
mailchimp.setConfig({
//*****************************ENTER YOUR API KEY HERE******************************
apiKey: "insert api key here",
//*****************************ENTER YOUR API KEY PREFIX HERE i.e.THE SERVER******************************
server: "us17"
});
//As soon as the sign in button is pressed execute this
app.post("/", (req, res) => {
//*****************************CHANGE THIS ACCORDING TO THE VALUES YOU HAVE ENTERED IN THE INPUT ATTRIBUTE IN HTML******************************
const firstName = req.body.firstName;
const lastName = req.body.lastName;
const email = req.body.email;
//*****************************ENTER YOU LIST ID HERE******************************
const listId = "b1b406c32b";
//Creating an object with the users data
const subscribingUser = {
firstName: firstName,
lastName: lastName,
email: email
};
//Uploading the data to the server
const run = async () => {
const response = await mailchimp.lists.addListMember(listId, {
email_address: subscribingUser.email,
status: "subscribed",
merge_fields: {
FNAME: subscribingUser.firstName,
LNAME: subscribingUser.lastName
}
});
//If all goes well logging the contact's id
res.sendFile(__dirname + "/success.html")
console.log(
`Successfully added contact as an audience member. The contact's id is ${
response.id
}.`
);
}
//Running the function and catching the errors (if any)
// ************************THIS IS THE CODE THAT NEEDS TO BE ADDED FOR THE NEXT LESSON*************************
// So the catch statement is executed when there is an error so if anything goes wrong the code in the catch code is executed. In the catch block we're sending back the failure page. This means if anything goes wrong send the faliure page
run().catch(e => res.sendFile(__dirname + "/failure.html"));
});
// api key = ec60751672f9750393f14eafa6a8be9d-us17
// listID = b1b406c32b