-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
151 lines (134 loc) · 4.04 KB
/
Copy pathapp.js
File metadata and controls
151 lines (134 loc) · 4.04 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
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
// Load environment
require("./config/environment.js");
// Shared
const ConfirmedError = require("shared/error");
const Logger = require("shared/logger");
// Constants
const NODE_ENV = process.env.NODE_ENV;
const DOMAIN = process.env.DOMAIN;
// Express and body parsers
const express = require("express");
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Email for /signin alerts
const { Email } = require("shared/utilities");
// Support logs everything including successful requests
const expressWinston = require("express-winston");
expressWinston.requestWhitelist = ["url", "session", "ip", "method", "httpVersion", "originalUrl", "query"];
app.use(expressWinston.logger({
winstonInstance: Logger,
ignoreRoute: function (request, response) {
const reqUrl = request.url;
if (reqUrl.startsWith("/css/")
|| reqUrl.startsWith("/images/")
|| reqUrl.startsWith("/js/")
|| reqUrl.startsWith("/favicon.ico")
|| reqUrl.startsWith("/health")) {
return true;
}
return false;
}
}));
// Log unhandled rejections
process.on("unhandledRejection", error => {
Logger.error(`unhandledRejection:
${error.stack}`);
});
// Basic Security
app.use(require("helmet")());
// View Engine
app.engine(".hbs", require("express-handlebars").engine({
defaultLayout: "main",
extname: ".hbs",
partialsDir: __dirname + "/views/partials/"
}));
app.set("view engine", ".hbs");
app.use(express.static("public"));
app.locals.DOMAIN = DOMAIN;
// Sessions/Flash
app.use(require("./config/session.js"));
if (NODE_ENV === "production") {
app.set("trust proxy", 1);
}
// Controllers
app.use("/", require("./controllers/support-controller.js"));
app.use("/", require("./controllers/notification-controller.js"));
app.use("/", require("./controllers/reviews-controller.js"));
app.use("/", require("./controllers/signin-controller.js"));
app.use("/", require("./controllers/signup-controller.js"));
app.get("/error-test", (request, response, next) => {
next(new ConfirmedError(500, 999, "Test alerts", "Details here"));
});
app.get("/health", function(request, response, next) {
return response.status(200).json({
message: "OK from Support"
});
});
app.get("/", (request, response, next) => {
if (request.session && request.session.userEmail) {
response.redirect("/support");
}
else {
response.redirect("/signin");
}
});
// Log Errors
app.use(expressWinston.errorLogger({
winstonInstance: Logger
}));
// Handle Errors
app.use((error, request, response, next) => {
// Email Alert on /signin requests
// This is here instead of the controller route so we can also catch validation errors from middleware
if (request.path.toLowerCase().startsWith("/signin")) {
Email.sendAdminAlert("Sign In Error",
`Path: ${request.path}
IP: ${request.ip}
Time: ${new Date()}
Error: ${error}
Email: ${request.body ? request.body.email : "none"}`);
}
if (response.headersSent) {
Logger.error("RESPONSE ALREADY SENT");
return;
}
return response.format({
json: () => {
response.status(error.statusCode).json({
code: error.confirmedCode,
message: error.message
});
},
html: () => {
if (error.statusCode == 401) {
request.flashRedirect("error", error.message, "/signin");
}
else if (error.statusCode >= 200 && error.statusCode < 500) {
if (error.confirmedCode == 1) {
response.redirect("/resend-confirm-code");
}
request.flashRedirect("error", error.message, "/support");
}
else {
request.flashRender("error", error.message, "notification");
}
}
});
});
// Handle 404 Not Found
app.use((request, response, next) => {
Logger.info("404 NOT FOUND - " + request.originalUrl);
return response.format({
json: () => {
response.status(404).json({
code: 404,
message: "Not Found"
});
},
html: () => {
request.flashRender("error", "The page you are looking for does not exist.", "notification", 404);
}
});
});
module.exports = app;