This is a more suitable name now that each of these objects is
actually an express router and not a full-fledged express app.
const express = require("express");
const express = require("express");
-const app = express.Router();
+const router = express.Router();
const GameState = {
JOIN: 1,
const GameState = {
JOIN: 1,
-app.get('/', (request, response) => {
+router.get('/', (request, response) => {
if (! request.session.nickname)
response.render('choose-nickname.html', { game_name: "Empires" });
else
response.render('empires-game.html');
});
if (! request.session.nickname)
response.render('choose-nickname.html', { game_name: "Empires" });
else
response.render('empires-game.html');
});
-app.post('/spectator', (request, response) => {
+router.post('/spectator', (request, response) => {
const game = request.game;
var name = request.session.nickname;
const game = request.game;
var name = request.session.nickname;
response.send(JSON.stringify(id));
});
response.send(JSON.stringify(id));
});
-app.delete('/spectator/:id', (request, response) => {
+router.delete('/spectator/:id', (request, response) => {
const game = request.game;
game.remove_spectator(parseInt(request.params.id));
response.send();
});
const game = request.game;
game.remove_spectator(parseInt(request.params.id));
response.send();
});
-app.post('/register', (request, response) => {
+router.post('/register', (request, response) => {
const game = request.game;
var name = request.session.nickname;;
const game = request.game;
var name = request.session.nickname;;
-app.post('/deregister/:id', (request, response) => {
+router.post('/deregister/:id', (request, response) => {
const game = request.game;
game.remove_player(parseInt(request.params.id));
response.send();
});
const game = request.game;
game.remove_player(parseInt(request.params.id));
response.send();
});
-app.post('/reveal', (request, response) => {
+router.post('/reveal', (request, response) => {
const game = request.game;
game.reveal();
response.send();
});
const game = request.game;
game.reveal();
response.send();
});
-app.post('/start', (request, response) => {
+router.post('/start', (request, response) => {
const game = request.game;
game.start();
response.send();
});
const game = request.game;
game.start();
response.send();
});
-app.post('/reset', (request, response) => {
+router.post('/reset', (request, response) => {
const game = request.game;
game.reset();
response.send();
});
const game = request.game;
game.reset();
response.send();
});
-app.post('/capture/:captor/:captee', (request, response) => {
+router.post('/capture/:captor/:captee', (request, response) => {
const game = request.game;
game.capture(parseInt(request.params.captor), parseInt(request.params.captee));
response.send();
});
const game = request.game;
game.capture(parseInt(request.params.captor), parseInt(request.params.captee));
response.send();
});
-app.post('/liberate/:id', (request, response) => {
+router.post('/liberate/:id', (request, response) => {
const game = request.game;
game.liberate(parseInt(request.params.id));
response.send();
});
const game = request.game;
game.liberate(parseInt(request.params.id));
response.send();
});
-app.post('/restart', (request, response) => {
+router.post('/restart', (request, response) => {
const game = request.game;
game.restart(parseInt(request.params.id));
response.send();
});
const game = request.game;
game.restart(parseInt(request.params.id));
response.send();
});
-app.get('/characters', (request, response) => {
+router.get('/characters', (request, response) => {
const game = request.game;
response.send(game.characters);
});
const game = request.game;
response.send(game.characters);
});
-app.get('/empires', (request, response) => {
+router.get('/empires', (request, response) => {
const game = request.game;
response.send(game.empires);
});
const game = request.game;
response.send(game.empires);
});
-app.get('/spectators', (request, response) => {
+router.get('/spectators', (request, response) => {
const game = request.game;
response.send(game.spectators);
});
const game = request.game;
response.send(game.spectators);
});
-app.get('/players', (request, response) => {
+router.get('/players', (request, response) => {
const game = request.game;
response.send(game.players);
});
const game = request.game;
response.send(game.players);
});
-app.get('/events', handle_events);
+router.get('/events', handle_events);
+exports.router = router;
exports.name = "empires";
exports.Game = Game;
exports.name = "empires";
exports.Game = Game;
/* Mount sub apps. only _after_ we have done all the middleware we need. */
for (let key in engines) {
const engine = engines[key];
/* Mount sub apps. only _after_ we have done all the middleware we need. */
for (let key in engines) {
const engine = engines[key];
- app.use(`/${engine.name}/[a-zA-Z0-9]{4}/`, engine.app);
+ app.use(`/${engine.name}/[a-zA-Z0-9]{4}/`, engine.router);
}
app.listen(4000, function () {
}
app.listen(4000, function () {
const express = require("express");
const express = require("express");
-const app = express.Router();
+const router = express.Router();
class TicTacToe {
constructor() {
class TicTacToe {
constructor() {
-app.get('/', (request, response) => {
+router.get('/', (request, response) => {
const game = request.game;
if (! request.session.nickname)
const game = request.game;
if (! request.session.nickname)
response.render('tictactoe-game.html');
});
response.render('tictactoe-game.html');
});
-app.post('/move', (request, response) => {
+router.post('/move', (request, response) => {
const game = request.game;
const square = request.body.square;
const game = request.game;
const square = request.body.square;
game.broadcast_move(square);
});
game.broadcast_move(square);
});
-app.get('/events', (request, response) => {
+router.get('/events', (request, response) => {
const game = request.game;
/* These headers will keep the connection open so we can stream events. */
const game = request.game;
/* These headers will keep the connection open so we can stream events. */
+exports.router = router;
exports.name = "tictactoe";
exports.Game = TicTacToe;
exports.name = "tictactoe";
exports.Game = TicTacToe;