index.ts 3.0 KB

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