-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
213 lines (187 loc) · 5.6 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const express = require("express");
const keys = require("./config/keys");
const stripe = require("stripe")(keys.stripeSecretKey);
const bodyParser = require("body-parser");
const path = require("path");
const nodemailer = require("nodemailer");
const request = require("request");
const passport = require("passport");
const session = require("express-session");
// Creating routing var for routes file
const routes = require("./routes/index");
const about = require("./routes/about");
const store = require("./routes/store");
const contact = require("./routes/contact");
const contact_send = require("./routes/contact_send");
const contact_error = require("./routes/contact_error");
const success = require("./routes/success");
const terms = require("./routes/terms");
const policy = require("./routes/policy");
const error = require("./routes/error");
// Global var
var views = false;
// Passport config
//require('./config/passport')(passport);
// Initalize app
const app = express();
// View engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
// Body Parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Set static folder
app.use(express.static(`${__dirname}/public`));
// Express session middelware
app.use(
session({
secret: "secret",
resave: true,
saveUninitialized: true
})
);
// Creating routing path for ejs files
app.use("/", routes);
app.use("/about", about);
app.use("/store", store);
app.use("/contact", contact);
app.use("/success", success);
app.use("/contact_send", contact_send);
app.use("/contact_error", contact_error);
app.use("/terms", terms);
app.use("/policy", policy);
app.use("/error", error);
// Charge route
app.post("/charge", (req, res) => {
const amount = 1999;
stripe.customers
.create({
email: req.body.stripeEmail,
source: req.body.stripeToken
})
.then(customer =>
stripe.charges.create({
amount,
description: "iPhone 10/10x BioCase",
currency: "cad",
customer: customer.id
})
)
.then(charge => res.render("success"));
});
app.get("/contact_send", (req, res) => {
if (views) {
console.log("true");
res.redirect("/contact_send");
} else {
console.log("false");
res.redirect("/error");
}
});
// Error handling for charge route
app.get("/charge", (req, res) => {
res.redirect("/error");
});
// Error handling for send route
app.get("/send", (req, res) => {
res.redirect("/error");
});
// Contact route
app.post("/send", (req, res, next) => {
const output = `
<p>You have a new message:</p>
<h3>Information details</h3>
<ul>
<li>Name ${req.body.name}</li>
<li>Subject ${req.body.subject}</li>
<li>Email ${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
var captchaSolved = false;
var view = false;
if (
req.body["g-recaptcha-response"] === undefined ||
req.body["g-recaptcha-response"] === "" ||
req.body["g-recaptcha-response"] === null
) {
//return res.json({"responseCode" : 1, "responseDesc" : "Please select captcha"});
return res.redirect("/contact_error"), (view = true);
}
var secretKey = keys.captchaSecret;
var verificationUrl =
"https://www.google.com/recaptcha/api/siteverify?secret=" +
secretKey +
"&response=" +
req.body["g-recaptcha-response"] +
"&remoteip=" +
req.connection.remoteAddress;
request(verificationUrl, (error, response, body) => {
body = JSON.parse(body);
if (body.success !== undefined && !body.success) {
return (captchaSolved = false), (view = true);
//return res.json({"responseCode" : 1, "responseDesc" : "Failed captcha verification"});
}
view = true;
captchaSolved = true;
//res.json({"responseCode" : 0, "responseDesc" : "Success"});
});
// Error in captcha
app.use("*", (req, res) => {
res.status(404).send("404");
});
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let account = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
//service: 'gmail',
//port: 587,
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: keys.emailAddress, // generated ethereal user
pass: keys.emailPassword // generated ethereal password
},
tls: {
rejectUnauthorized: false
}
});
// setup email data with unicode symbols
let mailOptions = {
from: "${req.body.name}", // sender address
to: keys.emailAddress, // list of receivers
subject: "New message from contact form", // Subject line
html: output // html body
};
// send mail with defined transport object
let info = await transporter.sendMail(mailOptions, (err, info) => {
if (err) {
if (!captchaSolved && view) {
console.log(err);
next();
} else {
res.redirect("/error");
}
} else {
if (captchaSolved && view) {
console.log(info);
views = true;
res.redirect("/contact_send");
} else {
res.redirect("/error");
}
}
});
}
views = false;
main().catch(console.error);
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`server started on port ${port}`);
});