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) }