const path = require('path') const fs = require('fs') const express = require('express') const serverConfig = require('./config/serverConfig.js') // Server Config const wrtColor = require('./components/common/wrtColor') //Http 80 Route traffic to the frontend VUE application running with HTTPS const http = require('http') const httpApp = express() httpApp.all('*', (req, res) => res.redirect(301, `${serverConfig.frontEndUrl}`)) //301 - Moved Permanently const httpServer = http.createServer(httpApp) httpServer.listen(80, () => { //console.log(`http redirectiong 80 to Vue front end at ${serverConfig.frontEndUrl}`) wrtColor.green(`http redirectiong 80 to Vue front end at ${serverConfig.frontEndUrl}`) }) //Https to run socket.io const https = require('https') const httpsApp = express() const keyPath = fs.readFileSync(path.resolve(__dirname, `${serverConfig.certKeyPath}`)) const certPath = fs.readFileSync(path.resolve(__dirname, `${serverConfig.certPath}`)) const servOptions = { key: keyPath, cert: certPath } const httpsServer = https.createServer(servOptions,httpsApp) httpsServer.listen(serverConfig.httpsPort, () => { //console.log(`https listening on ${serverConfig.httpsPort} to handle socket.io requests`) wrtColor.green(`https listening on ${serverConfig.httpsPort} to handle socket.io requests`) }) // Attach Socket.IO to https server const io = require('socket.io') (httpsServer, { cors: { //https://socket.io/docs/v3/handling-cors/ origin: `${serverConfig.frontEndUrl}`, methods: ["GET", "POST"] } }) //load gameManagement require('./components/serverMgmt') //pass socket.io connections to socketHandler const { socketHandler } = require('./components/serverMgmt.js') socketHandler(io)