From aafa8e2509a771ee593772d6c7641a3c5dc1b76f Mon Sep 17 00:00:00 2001 From: tangxinyue <524779910@qq.com> Date: Fri, 24 Jul 2026 15:07:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=9D=99=E6=80=81?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E8=B5=84=E6=BA=90=20MIME=20=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=8F=8A=E4=BB=A3=E7=90=86=E5=8C=B9=E9=85=8D=E4=BC=98?= =?UTF-8?q?=E5=85=88=E7=BA=A7=E5=BC=95=E5=8F=91=E7=9A=84=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/core/router/index.cjs | 44 ++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/service/core/router/index.cjs b/service/core/router/index.cjs index 3cb812f..12a614a 100644 --- a/service/core/router/index.cjs +++ b/service/core/router/index.cjs @@ -55,14 +55,42 @@ const matchRouter = async (request, response) => { return false } } +function getMimeType(filePath) { + const ext = filePath.split('.').pop()?.toLowerCase() + const defaultMimes = { + png: 'image/png', + jpg: 'image/jpeg', + jpeg: 'image/jpeg', + gif: 'image/gif', + webp: 'image/webp', + svg: 'image/svg+xml', + ico: 'image/x-icon', + js: 'text/javascript', + css: 'text/css', + html: 'text/html', + json: 'application/json', + woff: 'font/woff', + woff2: 'font/woff2', + ttf: 'font/ttf', + otf: 'font/otf' + } + if (ext && defaultMimes[ext]) { + return defaultMimes[ext] + } + if (mime && typeof mime.getType === 'function') { + return mime.getType(filePath) || 'application/octet-stream' + } + return 'application/octet-stream' +} + const matchFile = async (request, response) => { return new Promise((resolve) => { - const path = request.url.split('?')[0] - fs.readFile(dist + decodeURIComponent(path), (err, file) => { + const cleanPath = decodeURIComponent(request.url.split('?')[0]) + fs.readFile(dist + cleanPath, (err, file) => { if (!err) { - const mimetype = mime.getType(request.url) + const mimetype = getMimeType(cleanPath) const responseHdeaders = { 'Content-Type': mimetype } - if (path.endsWith("-sw.js")) { + if (cleanPath.endsWith("-sw.js")) { responseHdeaders["Service-Worker-Allowed"] = '/' } response.writeHead(httpOk, responseHdeaders) @@ -92,14 +120,14 @@ const matchType = async (request, response) => { if (isRouter) { return } - let isProxy = matchProxy(request, response) - if (isProxy) { - return - } let isFile = await matchFile(request, response) if (isFile) { return } + let isProxy = matchProxy(request, response) + if (isProxy) { + return + } await endIndex(request, response) }