-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
95 lines (80 loc) · 2.38 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
var express = require('express'),
config = require('./config/config'),
http = require('http'),
db = require('./lib/db-config'),
passport = require('passport'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
methodOverride = require('method-override'),
compress = require('compression'),
errorHandler = require('errorhandler'),
busboy = require('connect-busboy'),
bodyParser = require('body-parser');
var env = process.env.NODE_ENV || 'development';
/**
* Process level exception catcher
*/
process.on('uncaughtException', function (err) {
console.log("Node NOT Exiting...");
console.log(err);
console.log(err.stack)
});
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Headers', 'Content-Type,Authorization');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
}
var app = express();
db.connectDatabase(function(db){
/**
* Passport.js intiallization
*/
require('./lib/passport-config')(passport);
/**
* Express Configuration
*/
app.use(logger('dev'));
app.use(compress());
app.use(methodOverride());
app.use(cookieParser());
app.use(bodyParser());
app.use(busboy());
app.use(allowCrossDomain);
app.use(passport.initialize());
app.engine('jade', require('jade').__express);
//app.use(express.static(__dirname + '/public'));
if ( env === "development" ){
app.use(
errorHandler({
dumpExceptions: true,
showStack: true
})
)
} else {
app.use(
errorHandler()
)
};
//register routes
require('./routes').routes(app);
//configureControllers(app);
// Setup Express error handler middleware!
app.use(function(err, req, res, next){
console.log(err);
res.send(err.code,{error:err});
});
/**
* Start http server here
*/
var server = http.createServer(app);
server.listen(process.env.PORT ? process.env.PORT : config.get('main:port'));
console.log("Listening on " + config.get('main:port'));
})