tmd改了2小时才发现问题部署一个nodejs项目本地很好用一切顺利上宝塔云服务器。。。如图所示重启服务多次没用怀疑文件没上传完整一看名字都对又手动访问js和css文件路径结果浏览器里能看到完整代码。感谢GLM5.2返回 500但响应时间只有 17-18ms。几百 KB 的 JS 文件正常传输至少几十毫秒。17ms 说明请求根本没走到静态文件服务那一步。想起来看了下日志Error: Not allowed by CORS at origin (/www/wwwroot/main/index.js:48:8) at /www/wwwroot/main/node_modules/.pnpm/cors2.8.6/node_modules/cors/lib/index.js:219:13 at optionsCallback (/www/wwwroot/main/node_modules/.pnpm/cors2.8.6/node_modules/cors/lib/index.js:199:9) at corsMiddleware (/www/wwwroot/main/node_modules/.pnpm/cors2.8.6/node_modules/cors/lib/index.js:204:7) at Layer.handle [as handle_request] (/www/wwwroot/main/node_modules/.pnpm/express4.22.2/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/www/wwwroot/main/node_modules/.pnpm/express4.22.2/node_modules/express/lib/router/index.js:328:13) at /www/wwwroot/main/node_modules/.pnpm/express4.22.2/node_modules/express/lib/router/index.js:286:9 at router.process_params (/www/wwwroot/main/node_modules/.pnpm/express4.22.2/node_modules/express/lib/router/index.js:346:12) at next (/www/wwwroot/main/node_modules/.pnpm/express4.22.2/node_modules/express/lib/router/index.js:280:10) at expressInit (/www/wwwroot/main/node_modules/.pnpm/express4.22.2/node_modules/express/lib/middleware/init.js:40:5)not allowed by cors同一个来源的请求被跨域给拦了再次感谢GLM5.2CORS 中间件抛了 Error看了一眼代码瞬间明白const DEFAULT_CORS_ORIGINS [ http://localhost:3001, http://127.0.0.1:3001, http://localhost:5173, http://127.0.0.1:5173, ]; app.use(cors({ origin: (origin, cb) { if (!origin) return cb(null, true); if (ALLOWED_ORIGINS.includes(origin)) return cb(null, true); cb(new Error(Not allowed by CORS)); // ← 罪魁祸首 }, credentials: false, }));1. 为什么浏览器请求会触发 CORSVite 构建产物的index.html中script和link标签默认带crossorigin属性2. 为什么本地正常、服务器 500三个关键现象一次解释清楚本地正常Origin 在白名单公网 500Origin 不在白名单抛 Error 被 Express 转成 500curl 正常没有 Origin 头直接放行3. 为什么 CSS 报 MIME 错误JS 报 500CSS 请求的 500 响应体是 Express 的错误页面HTML浏览器拿到text/html内容去当 CSS 解析所以报 MIME 不匹配。JS 则是直接看到 500 状态码。两者根因相同只是浏览器对 CSS 和 JS 的错误表现不同。4. 为什么响应时间只有 15ms请求在CORS 中间件最先注册的中间件就被拦截抛错了根本没走到express.static所以中间件链路极短响应极快。这个反常细节是定位问题的关键线索。修改把抛 Error 改成不设置 CORS 头app.use(cors({ origin: (origin, cb) { if (!origin) return cb(null, true); if (ALLOWED_ORIGINS.includes(origin)) return cb(null, true); // 不抛错仅不设置 CORS 头同源请求仍可正常访问跨域由浏览器拦截 cb(null, false); }, credentials: false, }));