/
server.js
159 lines (132 loc) 路 3.85 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
/**
* Required External Modules
*/
const express = require("express");
const path = require("path");
const cookieSession = require("cookie-session");
const createError = require("http-errors");
const bodyParser = require("body-parser");
const csp = require("helmet-csp");
// Adding business logic to the server.
const ProjectService = require("./services/ProjectService");
const BlogService = require("./services/BlogService");
const CreativeService = require("./services/CreativeService");
// Create instances of the above classes.
const projectService = new ProjectService("./data/project.json");
const blogService = new BlogService("./data/blog.json");
const creativeService = new CreativeService("./data/creative.json");
const routes = require("./routes");
/**
* App Variables
*/
const app = express();
const port = process.env.PORT || "3000";
// Required if running through reverse proxy like NGINX
app.set("trust proxy", 1);
app.enable("trust proxy");
/**
* Middleware
*/
// Solution taken from: https://stackoverflow.com/questions/8605720/how-to-force-ssl-https-in-express-js
function requireHTTPS(req, res, next) {
// The 'x-forwarded-proto' check is for Heroku
if (!req.secure && req.get('x-forwarded-proto') !== 'https' && process.env.NODE_ENV !== "development") {
return res.redirect('https://' + req.get('host') + req.url);
}
next();
}
app.use(requireHTTPS);
// Solution taken from: https://mindthecode.com/blog/how-to-redirect-www-to-non-www-in-your-express-app
function redirectWwwTraffic(req, res, next) {
if (req.headers.host.slice(0, 4) === "www.") {
var newHost = req.headers.host.slice(4);
return res.redirect(301, req.protocol + "://" + newHost + req.originalUrl);
}
next();
};
app.use(redirectWwwTraffic);
// Request the lifecycle, to fetch cookies that are sent with
// the headers that come from the client and parse them
// and also then set them on the request object.
app.use(
cookieSession({
name: "session",
// Just example keys.
keys: ["F56FsQQwE3r5", "htryhfgDSFG4"],
})
);
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(bodyParser.json());
/**
* Routes Definitions
*/
// Utilizing the EJS template
app.set("view engine", "ejs");
// Expects the template to be in the "views" folder
app.set("views", path.join(__dirname, "./views"));
app.locals.siteName = "Brennan K. Brown";
// Before routing handlers are defined, there needs to be
// "app.use" and the middleware called "express.static"
app.use(express.static(path.join(__dirname, "./src")));
app.use(
"/",
// Will pass down the service instances to the routes.
routes({
projectService: projectService,
blogService: blogService,
creativeService: creativeService,
})
);
/**
* Error Handling
*/
app.use(async (request, response, next) => {
try {
return next();
} catch (err) {
return next(err);
}
});
app.use((request, response, next) => {
try {
return next();
} catch (err) {
return next(createError(404, "File not found"));
}
});
app.use((err, request, response, next) => {
response.locals.message = err.message;
// Will log the error information and trace stack
// to the developer, but not the user.
console.error(err);
const status = err.status || 500;
response.locals.status = status;
response.status(status);
response.render("error");
});
/**
* Content Security Policy
*/
app.use(
csp({
directives: {
defaultSrc: ["'self'"],
styleSrc: [
"'self'",
"maxcdn.bootstrapcdn.com",
"fonts.googleapis.com",
"api.segment.io",
],
},
})
);
/**
* Server Activation
*/
app.listen(port, () => {
console.log(`Express Server is Listening on port ${port}!`);
});