From 552d95b1e69fbbd99f5bc300a127457e1b97b9df Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 25 Jul 2019 16:56:41 +0200 Subject: Move controller in dedicated functions --- server/controllers/client.ts | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'server/controllers') diff --git a/server/controllers/client.ts b/server/controllers/client.ts index f51470b41..18b8b58e9 100644 --- a/server/controllers/client.ts +++ b/server/controllers/client.ts @@ -48,7 +48,26 @@ for (const staticClientFile of staticClientFiles) { clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE })) clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE })) -clientsRouter.use('/client/locales/:locale/:file.json', function (req, res) { +clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations) + +// 404 for static files not found +clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => { + res.sendStatus(404) +}) + +// Always serve index client page (the client is a single page application, let it handle routing) +// Try to provide the right language index.html +clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML)) + +// --------------------------------------------------------------------------- + +export { + clientsRouter +} + +// --------------------------------------------------------------------------- + +async function serveServerTranslations (req: express.Request, res: express.Response) { const locale = req.params.locale const file = req.params.file @@ -59,16 +78,9 @@ clientsRouter.use('/client/locales/:locale/:file.json', function (req, res) { } return res.sendStatus(404) -}) - -// 404 for static files not found -clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => { - res.sendStatus(404) -}) +} -// Always serve index client page (the client is a single page application, let it handle routing) -// Try to provide the right language index.html -clientsRouter.use('/(:language)?', async function (req, res) { +async function serveIndexHTML (req: express.Request, res: express.Response) { if (req.accepts(ACCEPT_HEADERS) === 'html') { try { await generateHTMLPage(req, res, req.params.language) @@ -79,16 +91,8 @@ clientsRouter.use('/(:language)?', async function (req, res) { } return res.status(404).end() -}) - -// --------------------------------------------------------------------------- - -export { - clientsRouter } -// --------------------------------------------------------------------------- - async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang) -- cgit v1.2.3