app.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. // nodejs modules
  3. const path = require('path'),
  4. fs = require('fs'),
  5. os = require('os');
  6. // npm modules
  7. const express = require('express'),
  8. session = require('express-session'),
  9. mongoose = require('mongoose'),
  10. mongoStore = require('connect-mongo')(session),
  11. bodyParser = require('body-parser'),
  12. config = require('config'),
  13. request = require('request'),
  14. passport = require('passport'),
  15. LocalStrategy = require('passport-local').Strategy,
  16. passportSocketIo = require("passport.socketio");
  17. // custom modules
  18. const global = require('./logic/global'),
  19. coreHandler = require('./logic/coreHandler'),
  20. socketHandler = require('./logic/socketHandler'),
  21. expressHandler = require('./logic/expressHandler');
  22. // database
  23. const MongoDB = mongoose.connect('mongodb://localhost:27017/musare').connection;
  24. MongoDB.on('error', function(err) {
  25. console.log('Database error: ' + err.message);
  26. });
  27. MongoDB.once('open', function() {
  28. console.log('Connected to database');
  29. });
  30. global.db = {
  31. user: require('./schemas/user')(mongoose)
  32. };
  33. // setup express and socket.io
  34. const app = express(MongoDB);
  35. const server = app.listen(80);
  36. global.io = require('socket.io')(server);
  37. app.use(passport.initialize());
  38. app.use(passport.session());
  39. app.use(session({
  40. secret: config.get('secret'),
  41. store: new mongoStore({ mongooseConnection: MongoDB }),
  42. resave: true,
  43. saveUninitialized: true
  44. }));
  45. global.io.use(passportSocketIo.authorize({
  46. secret: config.get('secret'),
  47. store: new mongoStore({ mongooseConnection: MongoDB })
  48. }));
  49. passport.serializeUser(function(user, done) {
  50. done(null, user);
  51. });
  52. passport.deserializeUser(function(user, done) {
  53. done(null, user);
  54. });
  55. passport.use('local-signup', new LocalStrategy(function(username, password, cb) {
  56. process.nextTick(function() {
  57. db.user.findOne({'username' : username }, function(err, user) {
  58. if (err) return cb(err);
  59. if (user) return cb(null, false);
  60. else {
  61. var newUser = new db.user({
  62. username: username
  63. });
  64. newUser.save(function(err) {
  65. if (err) throw err;
  66. return cb(null, newUser);
  67. });
  68. }
  69. });
  70. });
  71. }));
  72. passport.use('local-login', new LocalStrategy(function(username, password, cb) {
  73. process.nextTick(function() {
  74. db.user.findOne({'username' : username }, function(err, user) {
  75. if (err) return cb(err);
  76. if (!user) return cb(null, false);
  77. if (!user.services.token.password == password) return done(null, false);
  78. return done(null, user);
  79. });
  80. });
  81. }));
  82. app.use(bodyParser.json());
  83. app.use(bodyParser.urlencoded({
  84. extended: true
  85. }));
  86. app.use(express.static(__dirname + '/../frontend/build/'));
  87. socketHandler(coreHandler, global.io);
  88. expressHandler(coreHandler, app);