From: Carl Worth Date: Sun, 24 May 2020 20:39:01 +0000 (-0700) Subject: lmno: Generalize middleware to not be specific to empires X-Git-Url: https://git.cworth.org/git?p=empires-server;a=commitdiff_plain;h=96694cc3d2adad5d23dccdc287f10b62a8d9a722 lmno: Generalize middleware to not be specific to empires We're planning to extend LMNO past the original single game of Empires soon. So, we adjust this middleware here to work for any game engine prior to the game ID in the path, (and simply preserve that engine string in the result of the redirect). --- diff --git a/lmno.js b/lmno.js index c990cac..61c7f5c 100644 --- a/lmno.js +++ b/lmno.js @@ -136,14 +136,15 @@ app.get('/[a-zA-Z0-9]{4}', (request, response) => { }); /* LMNO middleware to lookup the game. */ -app.use('/empires/:game_id([a-zA-Z0-9]{4})', (request, response, next) => { +app.use('/:engine([^/]+)/:game_id([a-zA-Z0-9]{4})', (request, response, next) => { + const engine = request.params.engine; const game_id = request.params.game_id; const canon_id = lmno_canonize(game_id); /* Redirect user to page with the canonical ID in it. */ if (game_id !== canon_id) { - const new_url = request.originalUrl.replace("/empires/" + game_id, - "/empires/" + canon_id); + const new_url = request.originalUrl.replace(`/${engine}/${game_id}`, + `/${engine}/${canon_id}`); response.redirect(301, new_url); return; }