-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
101 lines (85 loc) · 2.36 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
'use strict';
// Module dependencies.
var express = require('express'),
path = require('path'),
fs = require('fs'),
methodOverride = require('method-override'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
errorhandler = require('errorhandler'),
multer = require('multer'),
socketio = require('socket.io'),
session = require('express-session'),
passport = require('passport'),
forceDomain = require('forcedomain'),
MongoStore = require('connect-mongo')(session);
var app = module.exports = exports.app = express();
var server = require('http').createServer(app);
var io = socketio(server);
app.io = io;
//todo: io set log level 3
app.locals.siteName = "tinyfeed";
// Connect to database
var db = require('./config/db');
app.use(express.static(__dirname + '/public'));
app.use(multer({ dest: './uploads/'}));
// Bootstrap models
var modelsPath = path.join(__dirname, 'models');
fs.readdirSync(modelsPath).forEach(function (file) {
require(modelsPath + '/' + file);
});
var env = process.env.NODE_ENV || 'development';
var port = process.env.PORT || 3000;
if ('development' == env) {
app.use(morgan('dev'));
app.use(errorhandler({
dumpExceptions: true,
showStack: true
}));
app.set('view options', {
pretty: true
});
}
if ('test' == env) {
port = 9997;
app.use(morgan('test'));
app.set('view options', {
pretty: true
});
app.use(errorhandler({
dumpExceptions: true,
showStack: true
}));
}
if ('production' == env) {
app.use(morgan());
app.use(errorhandler({
dumpExceptions: false,
showStack: false
}));
}
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(methodOverride());
app.use(bodyParser());
app.use(forceDomain({
hostname: 'tinyfeed.me'
}));
app.use(session({
secret: 'ji4(3.0Ul!',
store: new MongoStore({
db: db.mongoose.connection.db
})
}));
app.use(passport.initialize());
app.use(passport.session());
// Bootstrap routes/api
var routesPath = path.join(__dirname, 'routes');
fs.readdirSync(routesPath).forEach(function(file) {
require(routesPath + '/' + file)(app);
});
//require(routesPath + '/index.js')(app);
// Start server
server.listen(port, function () {
console.log('Express server listening on port %d in %s mode', port, app.get('env'));
});