daily pastebin goal
19%
SHARE
TWEET

Untitled

a guest Aug 14th, 2016 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express');
  2. var cookieParser = require('cookie-parser');
  3. var path = require('path');
  4. var logger = require('morgan');
  5. var compression = require('compression');
  6. var methodOverride = require('method-override');
  7. var session = require('express-session');
  8. var flash = require('connect-flash');
  9. var bodyParser = require('body-parser');
  10. var expressValidator = require('express-validator');
  11. var dotenv = require('dotenv');
  12. var exphbs = require('express-handlebars');
  13. var mongoose = require('mongoose');
  14. var passport = require('passport');
  15. var RedisStore = require('connect-redis')(session);
  16. // Load environment variables from .env file
  17.  
  18.  
  19. // Controllers
  20. var HomeController = require('./controllers/home');
  21. var userController = require('./controllers/user');
  22. var contactController = require('./controllers/contact');
  23.  
  24. // Passport OAuth strategies
  25. require('./config/passport');
  26.  
  27. var app = express();
  28. dotenv.load();
  29.  
  30. mongoose.connect(process.env.MONGODB_URI || process.env.MONGODB);
  31. mongoose.connection.on('error', function() {
  32.   console.log('MongoDB Connection Error. Please make sure that MongoDB is running.');
  33.   process.exit(1);
  34. });
  35.  
  36. var hbs = exphbs.create({
  37.   defaultLayout: 'main',
  38.   helpers: {
  39.     ifeq: function(a, b, options) {
  40.       if (a === b) {
  41.         return options.fn(this);
  42.       }
  43.       return options.inverse(this);
  44.     },
  45.     toJSON : function(object) {
  46.       return JSON.stringify(object);
  47.     }
  48.   }
  49. });
  50.  
  51. app.engine('handlebars', hbs.engine);
  52. app.set('view engine', 'handlebars');
  53. app.set('port', process.env.PORT || 3000);
  54. app.use(compression());
  55. app.use(logger('dev'));
  56. app.use(bodyParser.json());
  57. app.use(bodyParser.urlencoded({ extended: false }));
  58. app.use(expressValidator());
  59. app.use(methodOverride('_method'));
  60.  
  61. app.use(flash());
  62. app.use(express.static(path.join(__dirname, 'public')));
  63. app.use(cookieParser()); // required before session.
  64. if (process.env.REDISTOGO_URL) {
  65.   var redisUrl = require('url').parse(process.env.REDISTOGO_URL),
  66.   redisAuth = redisUrl.auth.split(':');  
  67.   app.set('redisHost', redisUrl.hostname);
  68.   app.set('redisPort', redisUrl.port);
  69.   app.set('redisDb', redisAuth[0]);
  70.   app.set('redisPass', redisAuth[1]);
  71. } else {
  72.   var redis = require("redis").createClient();
  73. }
  74. if ('production' == app.get('env')) {
  75.   app.use(session({
  76.     secret: process.env.SESSION_SECRET,
  77.     cookie : {secure : true},
  78.     resave: true, saveUninitialized: true,
  79.     store: new RedisStore({
  80.       host: app.set('redisHost'),
  81.       port: app.set('redisPort'),
  82.       db: app.set('redisDb'),
  83.       pass: app.set('redisPass')
  84.     })  
  85.   }));
  86. } else if ('development' == app.get('env')) {        
  87.   app.use(session({ secret: process.env.SESSION_SECRET, resave: true, saveUninitialized: true }));
  88. };
  89. app.use(passport.initialize());
  90. app.use(passport.session());
  91. app.use(function(req, res, next) {
  92.   res.locals.user = req.user;
  93.   next();
  94. });
  95.  
  96.  
  97. app.get('/', HomeController.index);
  98. app.get('/dashboard', HomeController.dashboard);
  99.  
  100. app.get('/contact', contactController.contactGet);
  101. app.post('/contact', contactController.contactPost);
  102. app.get('/account', userController.ensureAuthenticated, userController.accountGet);
  103. app.put('/account', userController.ensureAuthenticated, userController.accountPut);
  104. app.delete('/account', userController.ensureAuthenticated, userController.accountDelete);
  105. app.get('/signup', userController.signupGet);
  106. app.post('/signup', userController.signupPost);
  107. app.get('/login', userController.loginGet);
  108. app.post('/login', userController.loginPost);
  109. app.get('/forgot', userController.forgotGet);
  110. app.post('/forgot', userController.forgotPost);
  111. app.get('/reset/:token', userController.resetGet);
  112. app.post('/reset/:token', userController.resetPost);
  113. app.get('/logout', userController.logout);
  114. app.get('/unlink/:provider', userController.ensureAuthenticated, userController.unlink);
  115.  
  116. // Production error handler
  117. if (app.get('env') === 'production') {
  118.   app.use(function(err, req, res, next) {
  119.     console.error(err.stack);
  120.     res.sendStatus(err.status || 500);
  121.   });
  122. }
  123.  
  124. app.listen(app.get('port'), function() {
  125.   console.log('Express server listening on port ' + app.get('port'));
  126. });
  127.  
  128. module.exports = app;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top