-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
207 lines (190 loc) · 5.77 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
const path = require(`path`);
require(`dotenv`).config();
if (!process.env.DB_NAME || !process.env.DB_PASS || !process.env.DB_HOST) {
throw Error(`Environment variables missing!`);
}
const express = require(`express`);
const exphbs = require(`express-handlebars`);
const Sequelize = require(`sequelize`);
const app = express();
app.engine(
`.hbs`,
exphbs.engine({
extname: `.hbs`,
helpers: {
navLink: (url, options) => {
return (
`<li class="nav-item">` +
`<a href="${url}" class="nav-link ${url == app.locals.activeRoute ? " active" : ``}">` +
`${options.fn(this)}</a></li>`
);
},
},
})
);
app.set(`view engine`, `.hbs`);
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, `/public`)));
const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_NAME, process.env.DB_PASS, {
host: process.env.DB_HOST,
dialect: `postgres`,
port: 5432,
dialectOptions: {
ssl: { rejectUnauthorized: false },
},
query: { raw: true },
});
let Person = sequelize.define(`Person`, {
firstName: Sequelize.STRING,
lastName: Sequelize.STRING,
email: Sequelize.STRING,
phone: Sequelize.STRING,
department: Sequelize.STRING,
});
const HTTP_PORT = process.env.PORT || 8080;
const errIDLessThanOne = new Error(`ID must be greater than zero.`);
const errIDNotFound = new Error(`A person with this ID does not exist.`);
function onHTTPStart() {
console.log(`Express HTTP server listening on: ${HTTP_PORT}`);
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
app.use((req, res, next) => {
let route = req.path.substring(1);
app.locals.activeRoute = `/${isNaN(route.split(`/`)[1]) ? route.replace(/\/(?!.*)/, ``) : route.replace(/\/(.*)/, ``)}`;
next();
});
app.get("/", (req, res) => {
res.render(`index.hbs`, {
title: `Home | Persons`,
});
});
app.get(`/persons`, (req, res) => {
let sort = req.query.sort || [`id`, `DESC`];
Person.findAll({ order: [sort] })
.then((data) => {
res.render(`persons`, {
data: data,
showReturnToTop: data.length > 10,
title: `Your Persons | Persons`,
});
})
.catch((error) => {
console.log(`ERROR: ${error}`);
res.status(400).render(`error`, {
errorMessage: `Sort parameter must be an existing column. Please try again.`,
title: `Error | Persons`
});
});
});
app.post(`/add`, (req, res) => {
if (
/^[A-z'-\s]+$/.test(req.body.firstName) &&
/^[A-z'-\s]+$/.test(req.body.lastName) &&
/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(req.body.email) &&
/^\+?\d{0,3}\s?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/.test(req.body.phone) &&
/^[A-z\d\s&'-]+$/.test(req.body.department)
) {
Person.create({
firstName: capitalizeFirstLetter(req.body.firstName),
lastName: capitalizeFirstLetter(req.body.lastName),
email: req.body.email,
phone: req.body.phone,
department: req.body.department,
}).then((newPerson) => {
console.log(`Successfully added person #${newPerson.id}: ${newPerson.firstName} ${newPerson.lastName}`);
res.redirect(`/persons`);
});
} else {
console.log(`ERROR: Received invalid values for adding person`);
res.status(403).render(`error`, {
errorMessage: `Received invalid values for adding person. Please try again.`,
title: `Error | Persons`
});
}
});
app.get(`/update`, async (req, res) => {
try {
// Must use a query because express replaces id parameter
// with the string 'style.css' for some reason
if (req.query.id < 1) {
throw errIDLessThanOne;
}
let person = await Person.findByPk(req.query.id);
if (!person) {
throw errIDNotFound;
}
res.render(`update`, {
data: person,
title: `Update | Persons`,
});
} catch (error) {
console.log(`ERROR: ${error}`);
res.status(400).render(`error`, { errorMessage: error,
title: `Error | Persons` });
}
});
app.post(`/update`, async (req, res) => {
try {
if (req.body.id < 1) {
throw errIDLessThanOne;
}
let person = await Person.findByPk(req.body.id);
if (!person) {
throw errIDNotFound;
}
if (/^[A-z'-\s]+$/.test(req.body.firstName)) {
await Person.update({ firstName: capitalizeFirstLetter(req.body.firstName) }, { where: { id: req.body.id } });
}
if (/^[A-z'-\s]+$/.test(req.body.lastName)) {
await Person.update({ lastName: capitalizeFirstLetter(req.body.lastName) }, { where: { id: req.body.id } });
}
if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(req.body.email)) {
await Person.update({ email: req.body.email }, { where: { id: req.body.id } });
}
if (/^\+?\d{1,3}?\s?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/.test(req.body.phone)) {
await Person.update({ phone: req.body.phone }, { where: { id: req.body.id } });
}
if (/^[A-z\d\s&'-]+$/.test(req.body.department)) {
await Person.update({ department: req.body.department }, { where: { id: req.body.id } });
}
console.log(`Successfully updated person #${req.body.id}`);
res.redirect(`/persons`);
} catch (error) {
console.log(`ERROR: ${error}`);
res.status(400).render(`error`, { errorMessage: error,
title: `Error | Persons` });
}
});
app.get(`/delete`, async (req, res) => {
try {
// Must use a query because express replaces id parameter
// with the string 'style.css' for some reason
if (req.query.id < 1) {
throw errIDLessThanOne;
}
let person = await Person.findByPk(req.query.id);
if (!person) {
throw errIDNotFound;
}
await Person.destroy({ where: { id: req.query.id } });
console.log(`Successfully deleted person ${req.query.id}`);
res.redirect(`/persons`);
} catch (error) {
console.log(`ERROR: ${error}`);
res.status(400).render(`error`, {
errorMessage: err,
title: `Error | Persons`
});
}
});
app.use((req, res) => {
res.status(404).render(`error`, {
errorMessage: `Page not found :/`,
title: `Error | Persons`
});
});
sequelize.sync().then(() => {
app.listen(HTTP_PORT, onHTTPStart);
});