-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
49 lines (40 loc) · 1.3 KB
/
app.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// @ts-ignore
const expressApp = require('express')();
const bodyParser = require('body-parser');
expressApp.use(bodyParser.urlencoded({extended: true}));
expressApp.use(bodyParser.json());
const server = require('http').Server(expressApp);
const io = require('socket.io')(server);
const nextJs = require('next');
const nextApp = nextJs({dev: process.env.NODE_ENV != 'production'});
const nextHandler = nextApp.getRequestHandler();
// @ts-ignore
const GameManager = new (require('./logic/room-manager').GameManager)(io);
nextApp.prepare().then(() => {
expressApp.post('/create', (req, res) => {
// @ts-ignore
GameManager.createGame(req, res);
});
expressApp.get('/api/activeGames', (req, res) => {
// @ts-ignore
GameManager.getActiveGames(req, res);
});
// Send people who join the game to the game room
expressApp.get('/game/:gameCode', (req, res) => {
// @ts-ignore
GameManager.sendToGame(req, res, nextHandler);
});
expressApp.get('*', (req, res) => {
return nextHandler(req, res);
});
// Start the server for socket.io
const envPort = parseInt(process.env.PORT);
const port = envPort >= 0 ? envPort : 3000;
server.listen(port, (err) => {
if (err) throw err;
console.log("Listening on port " + port);
})
});
module.exports = {
gameManager: GameManager
};