forked from dangular/storeroom83
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
103 lines (79 loc) · 2.44 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
/**
* Created by: dhayes on 4/4/14.
* Filename: server
*/
var express = require('express'),
http = require('http'),
passport = require('passport'),
path = require('path'),
fs = require('fs'),
mongoStore = require('connect-mongo')(express),
config = require('./lib/configuration');
var app = express();
require('./lib/db/mongo');
// Bootstrap models
var modelsPath = path.join(__dirname, 'lib/models');
fs.readdirSync(modelsPath).forEach(function (file) {
require(modelsPath + '/' + file);
});
// Passport configuration
require('./lib/configuration/pass');
// cookieParser should be above session
app.use(express.cookieParser());
// all environments
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'bower_components')));
app.use(express.favicon());
app.use(express.logger({intermediate: true, format: 'dev'}));
app.use(express.json());
app.use(express.urlencoded());
// development only
app.configure('development', function(){
app.use(express.errorHandler());
});
// cookieParser should be above session
app.use(express.cookieParser());
app.use(express.methodOverride());
// express/mongo session storage
app.use(express.session({
secret: 'dangular',
store: new mongoStore({
url: config.get('mongodb:dbURI'),
collection: 'sessions'
})
}));
//use passport session
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
/**
* Routes
*/
var renderIndex = function(req, res) {
res.render('index');
};
app.get('/', renderIndex);
app.get('/partials/*', function(req, res) {
var requestedView = path.join('./', req.url);
res.render(requestedView);
});
require('./lib/routes')(app);
// redirect all others to the index (HTML5 history)
app.get('*', renderIndex);
app.set('port', config.get("express:port"));
// winston won't create directory, create the logging path if it doesn't exist
if (!fs.existsSync(config.get('logger:path'))) {
fs.mkdirSync(config.get('logger:path'));
}
if (config.get('logger:truncateOnStart')) {
var fullPath = path.join(config.get('logger:path'), config.get('logger:filename'));
if (fs.existsSync(fullPath)) {
fs.truncateSync(fullPath);
}
}
app.listen(app.get('port'), function(){
console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env'));
});
module.exports = app;