-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (53 loc) · 1.51 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
import express from "express";
import mailchimp from "@mailchimp/mailchimp_marketing";
import dotenv from "dotenv";
import path from "path";
const app = express();
const port = process.env.PORT || 3000;
const dirname = path.resolve(path.dirname(""));
//server config
dotenv.config();
//mailchimp
const MCapiKey = process.env.apiKeyMC; //set on server
const MCServer = "us21";
const MCListId = "49efc1691a";
mailchimp.setConfig({
apiKey: MCapiKey,
server: MCServer,
});
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
//routes
app.get("/", (req, res) => {
res.sendFile(path.join(dirname, "signup.html"));
});
app.post("/", async (req, res) => {
var FName = req.body.FName;
var LName = req.body.LName;
var email = req.body.email;
const contact = {
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: FName,
LNAME: LName,
},
};
const addListMember = async (MCListId, contact) => {
const response = await mailchimp.lists.addListMember(MCListId, contact);
console.log(`Successfully added contact.. The contact's id is ${response.id}.`);
return response.id;
};
const sendResponse = (res, fileName) => {
res.sendFile(path.join(dirname, fileName));
};
addListMember(MCListId, contact)
.then(memberId => sendResponse(res, 'success.html'))
.catch(error => {
console.error(error);
sendResponse(res, 'fail.html');
});
});
app.listen(port, () => {
console.log(`Running on port ${port}`);
});