From fd9a0cc355ec22086d06cd5555d2f4d9604a2df7 Mon Sep 17 00:00:00 2001 From: ck <21735205+cyperdark@users.noreply.github.com> Date: Thu, 29 Feb 2024 02:25:59 +0300 Subject: [PATCH] fix: Add error handle to main page --- packages/server/router/index.ts | 63 +++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/packages/server/router/index.ts b/packages/server/router/index.ts index e894894d..e918dd2b 100644 --- a/packages/server/router/index.ts +++ b/packages/server/router/index.ts @@ -313,39 +313,48 @@ export default function buildBaseApi(app: HttpServer) { }); app.route(/.*/, 'GET', (req, res) => { - const url = req.pathname || '/'; - const folderPath = - config.staticFolderPath || path.join(pkgRunningFolder, 'static'); + try { + const url = req.pathname || '/'; + const folderPath = + config.staticFolderPath || + path.join(pkgRunningFolder, 'static'); - if (url == '/') { - if (req.query?.tab == '1') { - return buildExternalCounters(res); - } + if (url == '/') { + if (req.query?.tab == '1') { + return buildExternalCounters(res); + } - if (req.query?.tab == '2') { - return buildSettings(res); - } + if (req.query?.tab == '2') { + return buildSettings(res); + } + + if (req.query?.tab == '3') { + return buildInstructionLocal(res); + } - if (req.query?.tab == '3') { - return buildInstructionLocal(res); + return buildLocalCounters(res); } - return buildLocalCounters(res); - } + const extension = path.extname(url); + if (extension == '' && !url.endsWith('/')) { + res.writeHead(301, { Location: url + '/' }); + return res.end(); + } - const extension = path.extname(url); - if (extension == '' && !url.endsWith('/')) { - res.writeHead(301, { Location: url + '/' }); - return res.end(); + const selectIndexHTML = url.endsWith('/') + ? url + 'index.html' + : url; + directoryWalker({ + _htmlRedirect: true, + res, + baseUrl: url, + pathname: selectIndexHTML, + folderPath + }); + } catch (error) { + return sendJson(res, { + error: (error as any).message + }); } - - const selectIndexHTML = url.endsWith('/') ? url + 'index.html' : url; - directoryWalker({ - _htmlRedirect: true, - res, - baseUrl: url, - pathname: selectIndexHTML, - folderPath - }); }); }