forked from DanWahlin/CodeWithDanDockerServices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
173 lines (136 loc) · 5.25 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
'use strict';
//3rd Party Modules
var express = require('express'),
exphbs = require('express-handlebars'),
hbsHelpers = require('handlebars-helpers'),
hbsLayouts = require('handlebars-layouts'),
compression = require('compression'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
session = require('cookie-session'),
csurf = require('csurf'),
favicon = require('serve-favicon'),
merge = require('merge'),
//fs = require('fs'),
//Local Modules
customExpressHbsHelpers = require('./lib/hbsHelpers/expressHbsHelpers'),
db = require('./lib/database'),
redisClient = require('./lib/redisClient'),
productTypeRepository = require('./lib/productTypeRepository'),
//routes = require('./routes/router.js'),
router = require('express-convention-routes'),
port = process.env.PORT || 8080,
app = express(),
config = require('./lib/configLoader');
//*************************************************
// Handlebars template registration
//*************************************************
var customHelpers = merge(customExpressHbsHelpers, hbsHelpers());
var hbs = exphbs.create({
extname: '.hbs',
defaultLayout: 'master',
helpers: customHelpers
});
app.engine('hbs', hbs.engine);
app.set('view engine', 'hbs');
//Add custom handlebars template helper functionality
hbsLayouts.register(hbs.handlebars, {});
//*************************************************
// Middleware and other settings
//*************************************************
app.use(favicon(__dirname + '/public/img/favicon.ico'));
app.use(express.static(__dirname + '/public'));
//app.use(compression()); //Compression done via nginx
//Logging
//var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'});
//app.use(morgan('dev', {stream: accessLogStream}));
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({
keys: ['some*key']
}));
app.use(csurf());
//************************************
// Custom middleware injection
//************************************
//Handle product types being passed with every response
app.use(productTypeRepository.injectProductTypes);
//Pass database config settings
//Simple work around to wait for MongoDB to start
//NO....NOT FOR PRODUCTION
setTimeout(() => {
db.init(config.databaseConfig);
}, 5000);
//Pass redis config settings
redisClient.connect();
//Handle each request and ensure proper locals are set that are needed by app
app.use(function(req, res, next) {
res.locals._csrf = req.csrfToken();
if (req.query.searchtext) {
res.locals.searchtext = req.query.searchtext;
}
res.locals.encodedUrl = encodeURIComponent(req.protocol + '://' + req.get('host') + req.originalUrl);
next();
});
app.use(function(err, req, res, next) {
console.error(err.stack);
res.send(500, { message: err.message });
});
process.on('uncaughtException', function(err) {
if (err) console.log(err, err.stack);
});
//*********************************************************
// Ensure DB gets closed when SIGINT called
//*********************************************************
if (process.platform === "win32") {
require("readline").createInterface({
input: process.stdin,
output: process.stdout
}).on("SIGINT", function () {
console.log('SIGINT: Closing MongoDB connection');
db.close();
redisClient.close();
});
}
process.on('SIGINT', function() {
console.log('SIGINT: Closing MongoDB connection');
db.close();
redisClient.close();
});
//*********************************************************
// Convention based route loading
//*********************************************************
//routes.load(app, './controllers');
router.load(app, {
//Defaults to "./controllers" but showing for example
routesDirectory: './controllers',
//Root directory where your server is running
rootDirectory: __dirname,
//Do you want the created routes to be shown in the console?
logRoutes: true
});
//Handle any routes that are unhandled and return 404
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
res.render('errors/404', err);
});
app.listen(port, function (err) {
console.log('[%s] Listening on http://localhost:%d', process.env.NODE_ENV, port);
});
//*********************************************************
// Quick and dirty way to detect event loop blocking
//*********************************************************
var lastLoop = Date.now();
function monitorEventLoop() {
var time = Date.now();
if (time - lastLoop > 1000) console.error('Event loop blocked ' + (time - lastLoop));
lastLoop = time;
setTimeout(monitorEventLoop, 200);
}
if (process.env.NODE_ENV === 'development') {
monitorEventLoop();
}