Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
69 lines (65 loc) · 2.78 KB
/
Copy pathapp.js
File metadata and controls
69 lines (65 loc) · 2.78 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
//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 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 8888 and if it goes well then logging a message saying that the server is running
app.listen(process.env.PORT||8888,function () {
console.log("Server is running at port 8888");
});
//Sending the signup.html file to the browser as soon as a request is made on localhost:8888
app.get("/", function (req, res) {
res.sendFile(__dirname + "/signup.html");
});
//Setting up MailChimp
mailchimp.setConfig({
//*****************************ENTER YOUR API KEY HERE******************************
apiKey: "930f02ba1fd3ef976a6efc03b75959ba-us7",
//*****************************ENTER YOUR API KEY PREFIX HERE i.e.THE SERVER******************************
server: "us7"
});
//As soon as the sign in button is pressed execute this
app.post("/", function (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 = "3773f53d3e";
//Creating an object with the users data
const subscribingUser = {
firstName: firstName,
lastName: lastName,
email: email
};
//Uploading the data to the server
async function run() {
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")
};
//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"));
}
);
app.post("/failure", function(req, res){
res.redirect("/");
});
// 75bc9b25d044d5f93b6e45d4f62ffa03-us7
// 3773f53d3e