-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (52 loc) · 1.37 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
const express = require('express');
const request = require('request');
const bodyParser = require("body-parser");
require('dotenv').config();
var port = process.env.PORT || 8080;
const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/assets', express.static('assets'));
app.get('/', (req, res) => {
res.sendFile(__dirname + "/views/signup.html");
});
app.post('/', (req, res) => {
var name = req.body.fName;
var email = req.body.Email;
var data = {
members: [{
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: name
}
}]
};
var jsonData = JSON.stringify(data);
var url = "https://us4.api.mailchimp.com/3.0/lists/" + process.env.UNIQUE_ID;
var mailchimp_req = {
url: url,
method: "POST",
headers: {
"Authorization": "ishandeveloper " + process.env.API_KEY
},
body: jsonData
};
request(mailchimp_req, (e, response, body) => {
if (response.statusCode == 200) {
res.sendFile(__dirname+"/views/success.html");
}
else {
res.sendFile(__dirname+"/views/failure.html");
}
});
});
app.post(
'/failure',(req,res)=>{
res.redirect('/');
}
);
app.listen(port, () => {
console.log("Server Up At " + port)
});