From b7b3a0df3699c42624bef27324e1f33fa2ff34ef Mon Sep 17 00:00:00 2001 From: patdelphi Date: Tue, 26 Aug 2025 12:48:20 +0800 Subject: [PATCH] fix: set explicit MIME types for static files in Koyeb deployment - Add setHeaders configuration to express.static middleware - Explicitly set Content-Type for CSS files as 'text/css' - Explicitly set Content-Type for JS files as 'application/javascript' - Add MIME type support for fonts, images, and other assets - Resolve Koyeb deployment MIME type errors --- server/index.cjs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/server/index.cjs b/server/index.cjs index 5c3374b..9ad627f 100644 --- a/server/index.cjs +++ b/server/index.cjs @@ -155,7 +155,32 @@ if (isProduction) { console.log('dist目录内容:', fs.readdirSync(distPath)); } - app.use(express.static(distPath)); + // 配置静态文件服务,明确设置MIME类型 + app.use(express.static(distPath, { + setHeaders: (res, path) => { + if (path.endsWith('.css')) { + res.setHeader('Content-Type', 'text/css'); + } else if (path.endsWith('.js')) { + res.setHeader('Content-Type', 'application/javascript'); + } else if (path.endsWith('.mjs')) { + res.setHeader('Content-Type', 'application/javascript'); + } else if (path.endsWith('.json')) { + res.setHeader('Content-Type', 'application/json'); + } else if (path.endsWith('.woff') || path.endsWith('.woff2')) { + res.setHeader('Content-Type', 'font/woff2'); + } else if (path.endsWith('.ttf')) { + res.setHeader('Content-Type', 'font/ttf'); + } else if (path.endsWith('.svg')) { + res.setHeader('Content-Type', 'image/svg+xml'); + } else if (path.endsWith('.png')) { + res.setHeader('Content-Type', 'image/png'); + } else if (path.endsWith('.jpg') || path.endsWith('.jpeg')) { + res.setHeader('Content-Type', 'image/jpeg'); + } else if (path.endsWith('.webp')) { + res.setHeader('Content-Type', 'image/webp'); + } + } + })); // SPA路由处理 - 只处理非API请求 app.get('*', (req, res, next) => {