index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const express = require("express");
  2. const path = require("path");
  3. const app = express();
  4. //const cors = require("cors");
  5. const morgan = require("morgan");
  6. const socketio = require("socket.io");
  7. const pg = require("pg");
  8. delete pg.native;
  9. const db = require("./db");
  10. const chalk = require("chalk");
  11. //const passport = require('passport')
  12. //const LocalStrategy = require('passport-local')
  13. //const session = require('express-session')
  14. //const SequelizeStore = require('connect-session-sequelize')(session.Store)
  15. //const sessionStore = new SequelizeStore({ db })
  16. //const bot = require('../ircbot')
  17. const PORT = process.env.PORT || 3500;
  18. //const URL = ["http://localhost:3400"];
  19. app.use(morgan("dev"));
  20. //app.use(cors({ credentials: true, origin: URL }))
  21. // passport.use(
  22. // new LocalStrategy(async (username, password, done) => {
  23. // const user = await db.models.user.findOne({ where: { username } })
  24. // if (!user) {
  25. // return done(null, false, { message: 'Incorrect username.' })
  26. // }
  27. // if (!user.correctPassword(password)) {
  28. // return done(null, false, { message: 'Incorrect password.' })
  29. // }
  30. // return done(null, user)
  31. // })
  32. // )
  33. //passport.serializeUser((user, cb) => cb(null, user.id))
  34. // passport.deserializeUser(async (id, cb) => {
  35. // try {
  36. // const user = await db.models.user.findByPk(id)
  37. // if (!user) return cb(null, { id: 0 })
  38. // const idType = user.isAdmin ? 'trainerId' : 'userId'
  39. // const { sessions, reviews, notes, referrers } = await user.fetchMyData(
  40. // idType
  41. // )
  42. // user.setDataValue('sessions', sessions)
  43. // user.setDataValue('reviews', reviews)
  44. // user.setDataValue('notes', notes)
  45. // user.setDataValue('referrers', referrers)
  46. // cb(null, user)
  47. // } catch (err) {
  48. // console.log('error', err)
  49. // cb(err)
  50. // }
  51. // })
  52. // app.use(
  53. // session({
  54. // secret: process.env.SESSION_SECRET || 'LAKaLIHWIUH9*&h3ISVAEOIUFHAW83w',
  55. // store: sessionStore,
  56. // resave: false,
  57. // saveUninitialized: false,
  58. // cookie: { maxAge: 3600000 },
  59. // })
  60. // )
  61. //
  62. // app.use(passport.initialize())
  63. // app.use(passport.session())
  64. // body parsing middleware
  65. app.use(express.json());
  66. app.use(express.urlencoded({ extended: true }));
  67. app.use(require("body-parser").text());
  68. //app.use("/api", require("./api"));
  69. //app.use('/auth', require('./auth'))
  70. app.use(
  71. "/static",
  72. express.static(path.resolve(__dirname, "..", "build", "static"))
  73. );
  74. app.get("/manifest.json", (req, res) => {
  75. res.sendFile(path.resolve(__dirname, "..", "build", "manifest.json"));
  76. });
  77. app.get("/favicon.png", (req, res) => {
  78. res.sendFile(path.resolve(__dirname, "..", "build", "favicon.png"));
  79. });
  80. app.use("*", express.static(path.resolve(__dirname, "..", "build")));
  81. // error handling endware
  82. app.use((err, req, res, next) => {
  83. console.error(err);
  84. console.error(err.stack);
  85. res.status(err.status || 500).send(err.message || "Internal server error.");
  86. next();
  87. });
  88. const startListening = () => {
  89. const server = app.listen(PORT, () => {
  90. console.log(chalk.blue(`[Express] Listening on port ${PORT}`));
  91. });
  92. const io = socketio(server);
  93. require("./socket")(io);
  94. };
  95. const startApp = async () => {
  96. //await sessionStore.sync();
  97. await startListening();
  98. };
  99. startApp();