-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
223 lines (206 loc) · 6.31 KB
/
server.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
214
215
216
217
218
219
220
221
222
223
"use strict";
require("dotenv").config({ path: "./dev.env" }); //if the dev.env is absent from the directory the application will assume it's running in production
require("./config/config");
const express = require("express");
const bodyParser = require("body-parser");
const { mongoose } = require("./db/mongoose");
const { ObjectID } = require("mongodb");
const { User } = require("./models/user");
const geocode = require("./geocode/geocode");
const time = require("./timezone/timezone");
const { sendCron, updateCron } = require("./app");
const { getRawWeather } = require("./weather/weather");
const { validateRecaptcha } = require("./captcha/captcha");
const hbs = require("hbs");
const userData = require("./user/user");
const port = process.env.port;
updateCron; //Check for DST at 03:00 UTC daily
sendCron; // Send emails
const app = express();
// app.use((req, res, next) => {
// if (!req.secure && process.env.NODE_ENV === "development") {
// var secureUrl = "https://" + req.headers["host"];
// console.log(secureUrl);
// res.writeHead(301, { Location: secureUrl });
// res.end();
// }
// next();
// });
app.set("view engine", "hbs");
hbs.registerPartials(__dirname + "/views/partials/");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
const httpHeaders = {
"x-powered-by": "myserver",
"Content-Security-Policy":
"script-src 'self' 'unsafe-inline' https://www.googletagmanager.com https://www.google.com https://www.gstatic.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;",
"X-Frame-Options": "deny",
"X-XXS-Protection": "1; mode=block"
};
app.get("/", (req, res) => {
res
.status(200)
.set(httpHeaders)
.render("index.hbs", {
method: "post",
action: ""
});
});
app.post("/", async (req, res) => {
let user = {}; //Declare user object here in order to use it in catch block
try {
//Validate form captcha
if (process.env.NODE_ENV != "test") {
const captcha = await validateRecaptcha(req.body);
if (captcha !== 200) {
throw new Error("Invalid captcha");
}
}
const data = await userData(
req.body.location,
req.body.country,
req.body.time,
req.body.timestamp
);
//Create a new user
user = new User({
email: req.body.email,
address: data.address,
location: data.location,
country: req.body.country,
latitude: data.latitude,
longitude: data.longitude,
userTime: req.body.time,
utcTime: data.utcTime,
utcTime_dst: "0",
timezone: data.timezone,
units: data.units
});
await user.save(); //Save user to database
res
.status(200)
.set(httpHeaders)
.render("success.hbs", {
message: `Success! You will receive daily emails with weather forecast for ${
user.address
} at ${user.email}`,
id: user._id,
timestamp: req.body.timestamp
});
} catch (err) {
if (err.code == 11000) {
res.status(400).render("error.hbs", {
message: `Looks like ${
user.email
} has already been used. Please try a different address.`
});
} else {
res.status(400).render("error.hbs", { message: `${err}, ${err.code}` });
}
}
});
//The method below is for the user to confirm that the location is valid before saving to db. This is not implemented so as to save on API calls to Google
app.post("/location/:location&:country", (req, res) => {
let location = req.params.location;
let country = req.params.country;
geocode.getCoords(location, country).then(result => {
res.status(200).send(result), err => res.status(404).send(err);
});
});
app.get("/update/:id", (req, res) => {
const id = req.params.id;
if (!ObjectID.isValid(id)) {
res.status(404).render("error.hbs", {
message: `Invalid user ID.`
});
}
User.findById({ _id: id })
.then(fields => {
res
.status(200)
.set(httpHeaders)
.render("update.hbs", {
method: "post",
action: "/update",
param: id,
email: fields.email,
location: fields.location,
country: fields.country,
time: fields.time,
units: fields.units
});
})
.catch(err => res.status(400).render("error.hbs", { message: err }));
});
app.post("/update/:id", async (req, res) => {
const id = req.params.id;
if (!ObjectID.isValid(id)) {
res.status(404).render("error.hbs", {
message: `Invalid user ID.`
});
}
const data = await userData(
req.body.location,
req.body.country,
req.body.time,
req.body.timestamp
);
User.findByIdAndUpdate(
{ _id: id },
{
email: req.body.email,
latitude: data.latitude,
longitude: data.longitude,
address: data.address,
location: data.location,
userTime: req.body.time,
utcTime: data.utcTime,
units: data.units,
timezone: data.timezone,
utcTime_dst: ""
},
{ new: true }
)
.then(user => {
if (!user) {
return res.status(404).render("error.hbs", {
message: `Invalid user ID.`
});
}
res
.status(200)
.set(httpHeaders)
.render("success.hbs", {
message: `Information updated. You will receive daily emails with weather forecast for ${
user.location
} at ${user.email}`,
id: user._id,
timestamp: req.body.timestamp
});
})
.catch(err =>
res.status(400).render("error.hbs", {
message: `${err}`
})
);
});
app.get("/delete/:id", (req, res) => {
const id = req.params.id;
if (!ObjectID.isValid(id)) {
res.status(404).send("Invalid ID");
}
User.findByIdAndDelete({ _id: id })
.then(user => {
res.status(200).render("delete.hbs", {
message: `We are sad to see you go. Your email ${
user.email
} has been unsubscribed.`
});
})
.catch(err => res.status(400).send(err));
});
app.listen(port, () => {
console.log(`server running on port ${port}`);
});
//module.exports.app = app;