-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
73 lines (58 loc) · 1.79 KB
/
app.js
File metadata and controls
73 lines (58 loc) · 1.79 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
70
71
72
73
const { subscribe } = require("diagnostics_channel");
const express = require("express")
const app = express();
const https = require("https");
app.use(express.urlencoded({ extended: true }))
app.use(express.static("public"));
require("dotenv").config();
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
})
app.post("/",function(req, res){
var email= req.body.Email
var fname= req.body.Fname
var lname=req.body.Lname
const url= "https://us4.api.mailchimp.com/3.0/lists/ac860c934c"
const data =JSON.stringify({
members :[{
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: fname,
LNAME: lname
}
}]
})
const options ={
method: "POST",
auth: "omkar:" + process.env.apiKey
}
const request = https.request(url, options, function (response){
var rawData= "";
response.on ("data", function(chunk){
rawData += chunk;
})
response.on("end", function(end){
try{
const fullyParsedData= JSON.parse(rawData);
if (response.statusCode=== 200){
res.sendFile(__dirname + "/success.html");
}else{
res.sendFile(__dirname +"/failure.html");
}
console.log(response.statusCode);
}catch(err){
console.log(err)
res.sendFile(__dirname +"/failure.html");
}
})
})
request.write(data);
request.end();
})
app.post("/failure", function(req,res){
res.redirect("/");
})
app.listen(process.env.PORT ||3000, function (req, res) {
console.log("Serve is listening at port 3000");
})