app.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /********** depend **************/
  2. var express = require('express');
  3. var exphbs = require('express-handlebars');
  4. var session = require('express-session');
  5. var flash = require('express-flash');
  6. var cookieParser = require('cookie-parser');
  7. var compress = require('compression');
  8. var bodyParser = require('body-parser');
  9. var favicon = require('serve-favicon');
  10. var errorHandler = require('errorhandler');
  11. var events = require('events');
  12. var http = require('http');
  13. var path = require('path');
  14. var fs = require('fs');
  15. var logger = require('morgan');
  16. var _ = require('lodash');
  17. var mongoose = require('mongoose');
  18. var MongoStore = require('connect-mongo')(session);
  19. var methodOverride = require('method-override');
  20. var passport = require('passport');
  21. var lusca = require('lusca');
  22. var expressValidator = require('express-validator');
  23. /********** Config **************/
  24. var conf = require('./config/config');
  25. var secure = require('./config/secure');
  26. var passportConf = require('./config/passport');
  27. var app = express();
  28. /********** MongoDB **************/
  29. mongoose.connect(secure.db);
  30. mongoose.connection.on('error', function() {
  31. console.log('MongoDB Connection Error!');
  32. process.exit(1);
  33. });
  34. /********** app.engine **************/
  35. app.set('port', process.env.PORT || 3000);
  36. app.set('views', path.join(__dirname, 'app/views'));
  37. app.set('view engine', 'jade');
  38. app.use(compress());
  39. /********** app.use **************/
  40. app.use(express.static(path.join(__dirname, 'app'), { maxAge: 31557600000 }));
  41. app.use(favicon(path.join(__dirname, 'app/img', 'favicon.png')));
  42. app.use(bodyParser.json());
  43. app.use(bodyParser.urlencoded({ extended: true }));
  44. app.use(expressValidator());
  45. app.use(methodOverride());
  46. app.use(cookieParser());
  47. app.use(session({
  48. resave: true,
  49. saveUninitialized: true,
  50. secret: secure.sessionSecret,
  51. store: new MongoStore({ url: secure.db, autoReconnect: true })
  52. }));
  53. app.use(passport.initialize());
  54. app.use(passport.session());
  55. app.use(flash());
  56. app.use(lusca({
  57. csrf: true,
  58. xframe: 'SAMEORIGIN',
  59. xssProtection: true
  60. }));
  61. app.use(function(req, res, next) {
  62. res.locals.user = req.user;
  63. next();
  64. });
  65. app.use(function(req, res, next) {
  66. if (/api/i.test(req.path)) {
  67. req.session.returnTo = req.path;
  68. }
  69. next();
  70. });
  71. /********** routes controllers **************/
  72. var router = express.Router();
  73. app.use(router);
  74. var homeController = require('./controllers/home_control');
  75. var userController = require('./controllers/user_control');
  76. var contactController = require('./controllers/contact_control');
  77. var eventController = require('./controllers/event_control');
  78. var entryController = require('./controllers/entry_control');
  79. var apiController = require('./controllers/api_control');
  80. /********** user routes **************/
  81. router.get('/', homeController.index);
  82. router.get('/login', userController.getLogin);
  83. router.post('/login', userController.postLogin);
  84. router.get('/user/:username', userController.getUserURL);
  85. router.get('/logout', userController.logout);
  86. router.get('/forgot', userController.getForgot);
  87. router.post('/forgot', userController.postForgot);
  88. router.get('/reset/:token', userController.getReset);
  89. router.post('/reset/:token', userController.postReset);
  90. router.get('/register', userController.getSignup);
  91. router.post('/register', userController.postSignup);
  92. router.get('/account', passportConf.isAuthenticated, userController.getAccount);
  93. router.post('/account/profile', passportConf.isAuthenticated, userController.postUpdateProfile);
  94. router.post('/account/password', passportConf.isAuthenticated, userController.postUpdatePassword);
  95. router.post('/account/delete', passportConf.isAuthenticated, userController.postDeleteAccount);
  96. router.get('/account/unlink/:provider', passportConf.isAuthenticated, userController.getOauthUnlink);
  97. /********** event routes **************/
  98. var eventRouter = require('./routes/event_route');
  99. app.use('/event', eventRouter);
  100. router.get('/event', eventController.getEvent);
  101. router.get('/event/new', eventController.getEvent_new);
  102. router.post('/event/new', passportConf.isAuthenticated, eventController.postEvent_new);
  103. /********** entry routes **************/
  104. var entryRouter = require('./routes/entry_route');
  105. app.use('/entry', entryRouter);
  106. router.get('/entry', entryController.getEntry);
  107. router.get('/entry/new', entryController.getEntry_new);
  108. router.post('/entry/new', passportConf.isAuthenticated, entryController.postEntry_new);
  109. /********** static routes controllers **************/
  110. router.get('/contact', contactController.getContact);
  111. router.post('/contact', contactController.postContact);
  112. router.get('/about', function(req, res) {
  113. res.render('pages/about', { title: 'About' });
  114. });
  115. router.get('/docs', function(req, res) {
  116. res.render('pages/docs', { title: 'Documentation' });
  117. });
  118. router.get('/privacy', function(req, res) {
  119. res.render('pages/privacy', { title: 'Privacy Policy' });
  120. });
  121. router.get('/terms', function(req, res) {
  122. res.render('pages/terms', { title: 'Terms & Conditions' });
  123. });
  124. router.get('/status', function(req, res) {
  125. res.render('pages/status', { title: 'System Status' });
  126. });
  127. router.get('/support', function(req, res) {
  128. res.render('pages/support', { title: 'Support' });
  129. });
  130. /************* log **************/
  131. app.use(logger('common', {
  132. stream: fs.createWriteStream('./log/access.log', {flags: 'a'})
  133. }));
  134. app.use(logger('dev'));
  135. var logger = new events.EventEmitter();
  136. logger.on('newEvent', function(event, data) {
  137. console.log('%s: %s', event, JSON.stringify(data));
  138. });
  139. /************* errors **************/
  140. app.use(function handleNotFound(req, res, next){
  141. res.status(404);
  142. if (req.accepts('html')) {
  143. res.render('404', { url: req.url, error: '404 Not found' });
  144. return;
  145. }
  146. if (req.accepts('json')) {
  147. res.send({ error: 'Not found' });
  148. return;
  149. };
  150. });
  151. if (process.env.NODE_ENV === 'development') {
  152. app.use(errorHandler())
  153. };
  154. if (process.env.NODE_ENV !== 'development') {
  155. app.use(function(err, req, res, next) {
  156. res.status(err.status || 500);
  157. res.render('500', {
  158. message: err.message,
  159. error: {}
  160. });
  161. });
  162. }
  163. /********** app.listen **************/
  164. app.listen(app.get('port'), function() {
  165. console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env'));
  166. });
  167. module.exports = app;