-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (52 loc) · 2.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
//express with Node js && API:
require('dotenv').config()
const express = require("express");
const app = express();
const bodyParser = require("body-parser")
var md5 = require('md5');
//mailchimp module
const mailchimp = require("@mailchimp/mailchimp_marketing");
const { response } = require("express");
//body parser
app.use(bodyParser.urlencoded({extended : true}));
//public folder storing static info:css, images
app.use(express.static("public"));
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html")
});
//setting up mailchimp
mailchimp.setConfig({
apiKey: process.env.API_KEY,
server: process.env.SERVER
});
app.post("/", function(req, res){
const fname = req.body.fname;
const lname = req.body.lname;
const email = req.body.email;
const subscribingUser={
lname:lname,
fname:fname,
email:email
}
async function run(){
const response= await mailchimp.lists.addListMember(process.env.LIST_ID, {
email_address: subscribingUser.email,
status:"subscribed",
merge_fields: {
FNAME: subscribingUser.fname,
LNAME: subscribingUser.lname}
});
//if all goes well move to :
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)
// 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.listen(process.env.PORT || 3000, function(){
console.log("server is running on port 3000");
});