This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (84 loc) · 3.02 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
/*jslint nomen: true*/
/*global process, require, angular, competitions, bets, $scope, __dirname, console, process*/
/*jslint white: false*/
/*jshint multistr: true*/
// Dependences
var Express = require("express"),
fs = require('fs'),
path = require('path'),
http = require('http'),
https = require('https'),
marked = require('marked'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
errorHandler = require('errorhandler'),
passport = require('passport'),
expressSession = require('express-session'),
mongoose = require('mongoose'),
MongoStore = require('connect-mongo/es5')(expressSession),
// Configuration
port = process.env.PORT || 5000,
port_ssl = process.env.PORT_SSL || 5001,
certificate = {
key: process.env.SSL_KEY ? new Buffer(process.env.SSL_KEY, 'base64').toString('utf8') : fs.readFileSync('tests/fixtures/key.pem'),
cert: process.env.SSL_CRT ? new Buffer(process.env.SSL_CRT, 'base64').toString('utf8') : fs.readFileSync('tests/fixtures/key-cert.pem')
},
db_path = process.env.DB_PATH,
//jslint nomen: false
// Some useful functions
connectWithRetry = function () {
return db_path && mongoose.connect(db_path, function (err) {
if (err) {
console.error('Failed to connect to mongo on startup - retrying in 5 sec', err);
setTimeout(connectWithRetry, 5000);
}
});
},
// Express
app = new Express();
connectWithRetry();
require('./config/passport')(passport); // pass passport for configuration
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
app.use(expressSession({
secret: process.env.DINAMO_APP_SECRET || 'dinamo',
//maxAge: new Date(Date.now() + 7 * 24 * 60 * 1000), // 1 week
//ttl: 7 * 24 * 60 * 1000,
store: db_path && new MongoStore({
mongooseConnection: mongoose.connections[0]
}),
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(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', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' === req.method) {
res.send(200);
} else {
next();
}
});
app.use(methodOverride());
app.use(errorHandler({
dumpExceptions: true,
showStack: true
}));
app.use('/auth', require('./routes/auth'));
app.use('/api', require('./routes/api'));
app.use(Express.static(path.join(__dirname, 'public')));
//create node.js http server and listen on port
http.createServer(app).listen(port, function () {
'use strict';
console.log("HTTP server is listening on..." + port);
});
https.createServer(certificate, app).listen(port_ssl, function () {
'use strict';
console.log("HTTPS server is listening on..." + port_ssl);
});