3
1

index.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import express from 'express'
  2. import cors from 'cors'
  3. import path from 'path'
  4. import morgan from 'morgan'
  5. import socketio from 'socket.io'
  6. import ascii from './ascii'
  7. import db from './db'
  8. const pg = require('pg')
  9. delete pg.native
  10. import { connectUpstream } from './joystream/ws'
  11. import { setupSocket } from './socket'
  12. import { allowedOrigins, port } from './config.json'
  13. const PORT: number = process.env.PORT ? +process.env.PORT : +port
  14. const app = express()
  15. const server = app.listen(PORT, async () => {
  16. console.log(`[Express] Listening on port ${PORT}`, ascii)
  17. const api = await connectUpstream()
  18. const io: any = socketio(server)
  19. setupSocket(io, api)
  20. })
  21. app.use(morgan('dev'))
  22. app.use(cors({ credentials: true, origin: allowedOrigins.split(',') }))
  23. // body parsing middleware
  24. app.use(express.json())
  25. app.use(express.urlencoded({ extended: true }))
  26. app.use(require('body-parser').text())
  27. app.use('/api', require('./api'))
  28. app.use(
  29. '/static',
  30. express.static(path.resolve(__dirname, '..', 'build', 'static'))
  31. )
  32. app.get('/manifest.json', (req: any, res: any) => {
  33. res.sendFile(path.resolve(__dirname, '..', 'build', 'manifest.json'))
  34. })
  35. app.get('/favicon.png', (req: any, res: any) => {
  36. res.sendFile(path.resolve(__dirname, '..', 'build', 'favicon.png'))
  37. })
  38. app.use('*', express.static(path.resolve(__dirname, '..', 'build')))
  39. // error handling endware
  40. app.use((err: any, req: any, res: any, next: any) => {
  41. console.error(err)
  42. console.error(err.stack)
  43. res.status(err.status || 500).send(err.message || 'Internal server error.')
  44. next()
  45. })