obs-portal/app.js
2020-11-21 19:41:25 +01:00

88 lines
2.1 KiB
JavaScript

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