-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·95 lines (68 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const express = require("express");
const dotenv = require("dotenv");
dotenv.config();
// change accordingly
// const key = "";
// const api = "";
const bodyParser = require("body-parser");
const request = require("request");
const https = require('https');
const { response } = require("express");
const app = express();
app.use(bodyParser.urlencoded({extended : true}));
app.use(express.static("public"));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.get("/" , function(req , res){
res.sendFile(__dirname + "/signup.html");
})
app.post("/", function(req,res){
const email = req.body.variableforbackend; // email from frontend
console.log(email)
// created object aligned with the api
const data = {
members: [
{
email_address:email,
status: "subscribed"
}
]
};
// converted to json string
const jsondata= JSON.stringify(data);
const url = "https://us11.api.mailchimp.com/3.0/lists/"+key+"";
// the post request we will send to the url
const options = {
method: "POST",
auth: `abhishekmorla:${api}`
}
// for sending request
const requests = https.request(url , options , function(response){
// if(response.statusCode === 401)
// {
// return res.status(200).sendFile(__dirname + "/success.html");
// }
response.on("data" , function(data){
const respo = JSON.parse(data);
console.log(respo);
if(respo.new_members.length === 0)
{
const err = respo.errors[0].error;
const print = err.split(',')[0];
res.status(404).render(__dirname + "/failure.html", {print:print});
}
else{
res.status(200).sendFile(__dirname + "/success.html");
}
})
})
requests.write(jsondata)
requests.end();
})
app.post("/failure",function(req , res){
res.redirect("/");
})
// dynamically port , procfile
app.listen(process.env.PORT || 1337, function(){
console.log("Started eh?");
})