-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
98 lines (82 loc) · 2.47 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
'use strict';
// Modules
var express = require('express');
var favicon = require('serve-favicon');
var logger = require('morgan');
var methodOverride = require('method-override');
var session = require('express-session');
var bodyParser = require('body-parser');
var multer = require('multer');
var errorHandler = require('errorhandler');
var cookieParser = require('cookie-parser');
var http = require('http');
var passport = require('passport');
var path = require('path');
var fs = require('fs');
var mongoStore = require('connect-mongo')(session);
var config = require('./app/config/config');
// App
var app = express();
// Database
var db = require('./app/config/database').db;
// Bootstrap
var modelsPath = path.join(__dirname, 'app/models');
fs.readdirSync(modelsPath).forEach(function(file) {
require(modelsPath + '/' + file);
});
// Passport
var pass = require('./app/config/pass');
// Config
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
if ('development' === app.get('env')) {
app.set('views', __dirname + '/client/views');
} else if ('production' === app.get('env')) {
app.set('views', __dirname + '/public/views');
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
}
// Logger
app.use(logger('dev'));
// Override
app.use(methodOverride());
// Cookie Parser
app.use(cookieParser());
// Mongo
app.use(session({
resave: true,
saveUninitialized: true,
secret: 'secrets for secret people',
store: new mongoStore({
url: config.db,
collection: 'sessions'
})
}));
// Body Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(multer());
// Passport
app.use(passport.initialize());
app.use(passport.session());
// Statics
if ('development' === app.get('env')) {
app.use(express.static(path.join(__dirname, '.tmp')));
app.use(express.static(path.join(__dirname, 'client')));
app.use(errorHandler());
} else if ('production' === app.get('env')) {
app.use(express.static(path.join(__dirname, 'public')));
}
// Build DB
if ('development' === app.get('env')) {
require('./app/database/dev')(db);
} else if ('production' == app.get('env')) {
require('./app/database/live')(db);
}
// Routes
require('./app/config/routes')(app);
// Start Server
app.listen(config.port, function() {
console.log('Server started on port %d in %s enviroment', config.port, app.get('env'));
});