diff --git a/package.json b/package.json index a94f981..76b9fad 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "@babel/preset-env": "^7.16.5", "acorn": "^8.6.0", "babel-loader": "^8.2.3", + "cors": "^2.8.5", "express": "^4.17.1", "express-force-https": "^1.0.0", "express-rate-limit": "^6.0.1", diff --git a/server/api/GamesAPI.js b/server/api/GamesAPI.js index 677c2b4..c65d4d5 100644 --- a/server/api/GamesAPI.js +++ b/server/api/GamesAPI.js @@ -4,6 +4,7 @@ const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase()) const logger = require('../modules/Logger')(debugMode); const GameManager = require('../modules/GameManager.js'); const rateLimit = require('express-rate-limit').default +const globals = require('../config/globals'); const gameManager = new GameManager().getInstance(); @@ -19,7 +20,7 @@ if (process.env.NODE_ENV.trim() === 'production') { // in prod, limit clients to } router.post('/create', function (req, res) { - logger.debug('Received request to create new game: ' + JSON.stringify(req.body, null, 4)); + logger.trace('Received request to create new game: ' + JSON.stringify(req.body, null, 4)); const gameCreationPromise = gameManager.createGame(req.body, false); gameCreationPromise.then((result) => { if (result instanceof Error) { @@ -27,6 +28,10 @@ router.post('/create', function (req, res) { } else { res.send(result); // game was created successfully, and access code was returned } + }).catch((e) => { + if (e === globals.ERROR_MESSAGE.BAD_CREATE_REQUEST) { + res.status(400).send(globals.ERROR_MESSAGE.BAD_CREATE_REQUEST); + } }); }); diff --git a/server/config/globals.js b/server/config/globals.js index 48dee1d..131659e 100644 --- a/server/config/globals.js +++ b/server/config/globals.js @@ -33,7 +33,8 @@ const globals = { SPECTATOR: "spectator" }, ERROR_MESSAGE: { - GAME_IS_FULL: "This game is full" + GAME_IS_FULL: "This game is full", + BAD_CREATE_REQUEST: "Game has invalid options." }, EVENTS: { PLAYER_JOINED: "playerJoined", diff --git a/server/main.js b/server/main.js index 8b19615..fedfe26 100644 --- a/server/main.js +++ b/server/main.js @@ -4,6 +4,7 @@ const https = require('https'); const path = require('path'); const fs = require('fs'); const app = express(); +const cors = require('cors') const bodyParser = require('body-parser'); const GameManager = require('./modules/GameManager.js'); const globals = require('./config/globals'); @@ -66,6 +67,12 @@ app.set('port', port); let io; if (process.env.NODE_ENV.trim() === 'development') { + const corsOptions = { + origin: "http://localhost:" + port, + optionsSuccessStatus: 200, + methods: ["GET", "POST"] + } + app.use(cors(corsOptions)); io = require("socket.io")(main, { cors: { origin: "http://localhost:" + port, @@ -75,6 +82,13 @@ if (process.env.NODE_ENV.trim() === 'development') { } }); } else { + const corsOptions = { + origin: ["https://playwerewolf.uk.r.appspot.com"], + methods: ["GET", "POST"], + allowedHeaders: ["Content-Type", "X-Requested-With", "Accept"], + optionsSuccessStatus: 200, + } + app.use(cors(corsOptions)); io = require("socket.io")(main, { cors: { origin: ["https://playwerewolf.uk.r.appspot.com", "wss://playwerewolf.uk.r.appspot.com"], diff --git a/server/modules/GameManager.js b/server/modules/GameManager.js index f93466f..35ce963 100644 --- a/server/modules/GameManager.js +++ b/server/modules/GameManager.js @@ -176,7 +176,7 @@ class GameManager { const expectedKeys = ['deck', 'hasTimer', 'timerParams']; if (typeof gameParams !== 'object' || expectedKeys.some((key) => !Object.keys(gameParams).includes(key))) { this.logger.error('Tried to create game with invalid options: ' + JSON.stringify(gameParams)); - return Promise.reject('Tried to create game with invalid options: ' + gameParams); + return Promise.reject(globals.ERROR_MESSAGE.BAD_CREATE_REQUEST); } else { // to avoid excessive memory build-up, every time a game is created, check for and purge any stale games. pruneStaleGames(this.activeGameRunner.activeGames, this.activeGameRunner.timerThreads, this.logger);