server.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var express = require('express');
  2. var path = require('path');
  3. var logger = require('morgan');
  4. var compression = require('compression');
  5. var methodOverride = require('method-override');
  6. var session = require('express-session');
  7. var flash = require('express-flash');
  8. var bodyParser = require('body-parser');
  9. var expressValidator = require('express-validator');
  10. var dotenv = require('dotenv');
  11. var nunjucks = require('nunjucks');
  12. var markdown = require('nunjucks-markdown');
  13. var marked = require('marked');
  14. var mongoose = require('mongoose');
  15. var passport = require('passport');
  16. var routes = require('./routes');
  17. // Load environment variables from .env file
  18. dotenv.load();
  19. // Passport OAuth strategies
  20. require('./config/passport');
  21. var app = express();
  22. mongoose.connect(process.env.DB_PATH);
  23. mongoose.connection.on('error', function() {
  24. console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
  25. process.exit(1);
  26. });
  27. // view engine setup
  28. var njks = nunjucks.configure('views', {
  29. autoescape: true,
  30. express: app,
  31. marked: true
  32. });
  33. markdown.register(njks, marked);
  34. app.set('view engine', 'html');
  35. app.set('port', process.env.PORT || 3999);
  36. app.use(compression());
  37. app.use(logger('dev'));
  38. app.use(bodyParser.json());
  39. app.use(bodyParser.urlencoded({ extended: false }));
  40. app.use(expressValidator());
  41. app.use(methodOverride('_method'));
  42. app.use(session({ secret: process.env.SESSION_SECRET, resave: true, saveUninitialized: true }));
  43. app.use(flash());
  44. app.use(passport.initialize());
  45. app.use(passport.session());
  46. app.use(function(req, res, next) {
  47. res.locals.user = req.user;
  48. next();
  49. });
  50. app.use(express.static(path.join(__dirname, 'public')));
  51. /**
  52. * Setup Routes
  53. */
  54. routes.init(app, passport);
  55. uploadDir = path.join(__dirname, '/public/media/');
  56. // Production error handler
  57. if (app.get('env') === 'production') {
  58. app.use(function(err, req, res, next) {
  59. console.error(err.stack);
  60. res.sendStatus(err.status || 500);
  61. });
  62. }
  63. app.listen(app.get('port'), function() {
  64. console.log('Express server listening on port ' + app.get('port'));
  65. });
  66. module.exports = app;