-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
140 lines (112 loc) · 3.82 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
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var hbs = require('express-handlebars');
var dbFile = require("./node_simple.js");
var sanitizer = require ("sanitizer");
var expressValidator = require('express-validator');
var session = require('express-session');
var passport = require('passport');
var flash = require('connect-flash');
var compression = require('compression');
var MongoStore = require('connect-mongo') (session); //for storing sessions in database
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io')(server);
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var routes = require('./routes/index');
var userRoutes = require('./routes/user')(io);
var adminRoutes = require('./routes/admin');
// Templating engine, we are using handlebars
// This will allow us to create html pages dynamically before serving them.
// use the minifed css if the app running on production, otherwise use reg css
var layout = (process.env.NODE_ENV == "production") ? 'layout_mini' : 'layout';
app.engine(
'.hbs',
hbs({
extname: '.hbs',
defaultLayout: layout,
layoutsDir: __dirname + '/views/layouts/', // Set directory for base layout
partialsDir: __dirname + '/views/partials',
})
); // Set directory for partials
app.set('views', path.join(__dirname, 'views')); // Our view path
app.set('view engine', 'hbs');
// Middleware initialization, make sure everything is initialized in proper order
app.use(compression());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(expressValidator());
app.use(cookieParser());
// session is stored in memory, change to storage in mongo later
app.use(session({
secret: 'pickBetterLater',
resave: false,
saveUninitialized: false,
store: new MongoStore({url : dbFile.uri}),
cookie: {maxAge: 120 * 60 * 1000} // in milliseconds - 120 mins, expires after this amount of time
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport'); // simply need to load it
/*LOADS ALL STATIC FILES FROM THE DIRECTORY __dirname*/
//app.use(minify({cache: __dirname + '/cache'}));
app.use(express.static(__dirname));
// Needed to style the header based on the whether the user is signed in or not
app.use(function(req, res, next) {
res.locals.login = req.isAuthenticated(); // global variable
res.locals.session = req.session;
res.locals.user = req.user;
res.locals.messages = req.session.messages;
next();
});
// Allows us to customize express routing
// in a separate file.
app.use('/admin', adminRoutes);
app.use('/user', userRoutes);
app.use('/', routes);
/* Handle error page */
app.use(function(req, res, next){
res.status(404);
// respond with html page
if (req.accepts('html')) {
res.render('error', { url: req.url });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
app.use(function(error, req, res, next) {
res.status(500);
url = req.url;
res.render('error', {error: error, url: url});
});
/****************** Server Setup ********************/
dbFile.setupDB(function (success, mssg) {
if (success) { // db establiseh
server.listen(port, function(){ // now accept connections
console.log('listening on port 8080');
});
}
else {
console.log(mssg);
}
});
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/*********************** Setup end *******************/
module.exports = app;