-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (72 loc) · 2.08 KB
/
index.js
File metadata and controls
86 lines (72 loc) · 2.08 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
74
75
76
77
78
79
80
81
82
83
84
85
86
// importing express
const express = require("express");
// importing https
const https = require("https");
// importing body-parser
const bodyParser = require("body-parser");
require("dotenv").config();
// Creating app using express
const app = express();
// Creating port
const port = 3000;
// using the bodyParser in urlEncoder mode
app.use(bodyParser.urlencoded({ extended: true }));
// get function for "/" route
app.get("/", (req, res) => {
// sending the index.html file back as response
res.sendFile(`${__dirname}/index.html`);
});
// post request function for "/" route
app.post("/", (req, res) => {
// creating constants to get data entered by the user
const email = req.body.email;
const fName = req.body.firstName;
const lName = req.body.lastName;
// creating an object to send data via API
const data = {
members: [
{
email_address: email,
status: "subscribed",
merge_fields: {
FNAME: fName,
LNAME: lName,
},
},
],
};
// converting the object to packed json
const jsonData = JSON.stringify(data);
//api url
const url = "https://us17.api.mailchimp.com/3.0/lists/d47d16f599";
// options object to make api call
const options = {
method: "POST",
auth: `manobal:${process.env.API}`,
};
// making an api call
const request = https.request(url, options, (response) => {
// if the response is successful go to success page else go to failure page
if (response.statusCode === 200) {
res.sendFile(`${__dirname}/Success.html`);
} else {
res.sendFile(`${__dirname}/Failure.html`);
}
response.on("data", (data) => {
console.log(JSON.parse(data));
});
});
// send the data from user to API
request.write(jsonData);
request.end();
});
// post request function for "/failure" route
app.post("/failure", (req, res) => {
// using try again button to redirect to home page
res.redirect("/");
});
// Listening to the port
app.listen(port, () => {
// Console logging the server starting message
console.log(`Server is online at port : ${port}`);
});