-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
205 lines (171 loc) · 5.98 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
var config = require("./config"),
express = require("express"),
routes = require("./routes"),
routeUtils = require("./utils/routeUtils"),
database = require("./data/database"),
app = express();
configureErrorHandling();
setEnvironmentVariables();
database.connect()
.then(function(message) {
console.log(message);
configureLogging();
configureViewEngine(app);
configurePublicDirectories(app, express);
configureSession(app);
})
.catch(function(error) {
console.error(error);
});
function configureErrorHandling() {
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
function logErrors(err, req, res, next) {
console.error(err.stack);
next(err);
}
function clientErrorHandler(err, req, res, next) {
if (req.xhr) {
res.status(500).send({ error: 'Something blew up!' });
} else {
next(err);
}
}
function errorHandler(err, req, res, next) {
res.status(500);
res.render('error', { error: err });
}
}
function setEnvironmentVariables() {
process.env.DEBUG = false;
}
function configureLogging() {
var winston = require("winston");
require("winston-mongodb").MongoDB;
var transport;
if (process.env.DEBUG === "true") {
transport = new winston.transports.MongoDB({
db: config.test.database.databaseName,
username: config.test.database.userName,
password: config.test.database.password
});
}
else {
transport = new winston.transports.MongoDB({
db: config.database.databaseName,
username: config.database.userName,
password: config.database.password
});
}
winston.handleExceptions(transport);
}
function configureViewEngine(app) {
var handlebars = require("express-handlebars");
var configuredHandlebars = handlebars.create({
helpers: {
activeMenuItem: function(route, activeRoute) {
return route === activeRoute ? "class=\"active\"" : "";
},
actionLink: routeUtils.actionLink,
postLink: routeUtils.postLink,
section: function(name, options){
if(!this.sections) {
this.sections = {};
}
this.sections[name] = options.fn(this);
return null;
},
ifCond: function(v1, operator, v2, options) {
switch (operator) {
case '==':
return (v1 == v2) ? options.fn(this) : options.inverse(this);
case '===':
return (v1 === v2) ? options.fn(this) : options.inverse(this);
case '<':
return (v1 < v2) ? options.fn(this) : options.inverse(this);
case '<=':
return (v1 <= v2) ? options.fn(this) : options.inverse(this);
case '>':
return (v1 > v2) ? options.fn(this) : options.inverse(this);
case '>=':
return (v1 >= v2) ? options.fn(this) : options.inverse(this);
case '&&':
return (v1 && v2) ? options.fn(this) : options.inverse(this);
case '||':
return (v1 || v2) ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
}
}
});
app.engine("handlebars", configuredHandlebars.engine);
app.set("view engine", "handlebars");
}
function configurePublicDirectories(app, express) {
app.use("/upload", express.static(config.assets.uploadFolder));
app.use("/admin/client", express.static(__dirname + "/admin/client"));
app.use("/blog/client", express.static(__dirname + "/blog/client"));
app.use("/robots.txt", express.static(__dirname + "/robots.txt"));
app.use("/keybase.txt", express.static(__dirname + "/keybase.txt"));
}
function configureCookieParser(app) {
app.use(require("cookie-parser")());
}
function configureSession(app) {
var session = require("express-session");
var MongoStore = require('connect-mongo')(session);
var connectionString = process.env.DEBUG === "true" ?
config.test.database.connectionString :
config.database.connectionString;
var sessionStore = new MongoStore({ url: connectionString }, function() {
configureCookieParser(app);
app.use(session({
secret: "node-simple-blog",
resave: false,
saveUninitialized: true,
store: sessionStore
}));
configureBodyParser(app);
configureFlash(app);
configureBusboy(app);
configureGlobalAppLocals(app);
setUpAdmin(app);
setUpBlog(app);
startServer(app);
});
}
function configureBodyParser(app) {
var bodyParser = require("body-parser");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
}
function configureFlash(app) {
app.use(require('connect-flash')());
}
function configureBusboy(app) {
app.use(require("connect-busboy")());
}
function configureGlobalAppLocals(app) {
app.use(function(request, response, next) {
request.app.locals.hideProfilePicture = false;
request.app.locals.routes = routes;
request.app.locals.activeRoute = request.originalUrl;
request.app.locals.blog = config.blog;
request.app.locals.assets = config.assets;
request.app.locals.currentYear = new Date().getFullYear();
next();
});
}
function setUpAdmin(app) {
require("./admin").init(app);
}
function setUpBlog(app) {
require("./blog").init(app);
}
function startServer(app) {
app.listen(config.blog.sitePort, function () {
console.log("Server started listening at " + config.blog.siteRootUrl);
});
}