const path = require('path'); const express = require('express'); const bodyParser = require('body-parser'); const session = require('express-session'); const cors = require('cors'); const errorhandler = require('errorhandler'); const mongoose = require('mongoose'); const auth = require('./routes/auth'); const isProduction = process.env.NODE_ENV === 'production'; // Create global app object const app = express(); app.use(cors()); app.use(auth.getUserIdMiddleware); app.use(auth.loadUserMiddleware); // 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(path.join(__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... const port = process.env.PORT || 3000; app.listen(port, () => { console.log('Listening on port ' + port); });