2020-04-13 00:02:40 +00:00
|
|
|
var http = require('http'),
|
2020-10-20 19:25:00 +00:00
|
|
|
path = require('path'),
|
|
|
|
methods = require('methods'),
|
|
|
|
express = require('express'),
|
|
|
|
bodyParser = require('body-parser'),
|
|
|
|
session = require('express-session'),
|
|
|
|
cors = require('cors'),
|
|
|
|
passport = require('passport'),
|
|
|
|
errorhandler = require('errorhandler'),
|
|
|
|
mongoose = require('mongoose');
|
2020-04-13 00:02:40 +00:00
|
|
|
|
|
|
|
var isProduction = process.env.NODE_ENV === 'production';
|
|
|
|
|
|
|
|
// Create global app object
|
|
|
|
var app = express();
|
|
|
|
|
|
|
|
app.use(cors());
|
|
|
|
|
|
|
|
// Normal express config defaults
|
|
|
|
app.use(require('morgan')('dev'));
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }));
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
|
|
|
|
app.use(require('method-override')());
|
|
|
|
app.use(express.static(__dirname + '/public'));
|
|
|
|
|
2020-10-20 19:25:00 +00:00
|
|
|
app.use(session({ secret: 'obsobs', cookie: { maxAge: 60000 }, resave: false, saveUninitialized: false }));
|
2020-04-13 00:02:40 +00:00
|
|
|
|
|
|
|
if (!isProduction) {
|
|
|
|
app.use(errorhandler());
|
|
|
|
}
|
|
|
|
|
2020-11-17 16:32:36 +00:00
|
|
|
const mongodbUrl = process.env.MONGODB_URL || (isProduction ? 'mongodb://localhost/obs' : 'mongodb://localhost/obsTest')
|
|
|
|
mongoose.connect(mongodbUrl);
|
|
|
|
mongoose.set('debug', !isProduction);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-08-14 10:37:34 +00:00
|
|
|
require('./models/TrackData');
|
2020-04-13 00:02:40 +00:00
|
|
|
require('./models/User');
|
|
|
|
require('./models/Track');
|
|
|
|
require('./models/Comment');
|
|
|
|
require('./config/passport');
|
|
|
|
|
|
|
|
app.use(require('./routes'));
|
|
|
|
|
|
|
|
/// catch 404 and forward to error handler
|
2020-10-20 19:25:00 +00:00
|
|
|
app.use(function (req, res, next) {
|
2020-04-13 00:02:40 +00:00
|
|
|
var err = new Error('Not Found');
|
|
|
|
err.status = 404;
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
/// error handlers
|
|
|
|
|
|
|
|
// development error handler
|
|
|
|
// will print stacktrace
|
|
|
|
if (!isProduction) {
|
2020-10-20 19:25:00 +00:00
|
|
|
app.use(function (err, req, res, next) {
|
2020-04-13 00:02:40 +00:00
|
|
|
console.log(err.stack);
|
|
|
|
|
|
|
|
res.status(err.status || 500);
|
|
|
|
|
2020-10-20 19:25:00 +00:00
|
|
|
res.json({
|
|
|
|
'errors': {
|
|
|
|
message: err.message,
|
|
|
|
error: err
|
|
|
|
}
|
|
|
|
});
|
2020-04-13 00:02:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// production error handler
|
|
|
|
// no stacktraces leaked to user
|
2020-10-20 19:25:00 +00:00
|
|
|
app.use(function (err, req, res, next) {
|
2020-04-13 00:02:40 +00:00
|
|
|
res.status(err.status || 500);
|
2020-10-20 19:25:00 +00:00
|
|
|
res.json({
|
|
|
|
'errors': {
|
|
|
|
message: err.message,
|
|
|
|
error: {}
|
|
|
|
}
|
|
|
|
});
|
2020-04-13 00:02:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// finally, let's start our server...
|
2020-10-20 19:25:00 +00:00
|
|
|
var server = app.listen(process.env.PORT || 3000, function () {
|
2020-04-13 00:02:40 +00:00
|
|
|
console.log('Listening on port ' + server.address().port);
|
|
|
|
});
|