proxy.conf.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * 本地开发代理(`npm start` / `npm run start:local` 自动启用)
  3. *
  4. * ## 为什么需要
  5. *
  6. * 线上部署在 `https://ai.fmode.cn/`,接口在 `https://server.fmode.cn/`——
  7. * **两者同属 fmode.cn 主域**。平台的会话(`r:` SessionToken 之外的部分)依赖同域
  8. * Cookie 与同域鉴权链路,本地点 `http://localhost:4200` 是跨域,行为与线上不一致,
  9. * 表现为「线上登录正常、本地登录报错」。
  10. *
  11. * 代理把接口挂到 dev server 自身的路径下,浏览器看来就是**同源请求**,
  12. * 与线上完全一致,业务代码一行不改。
  13. *
  14. * ## 路径映射
  15. *
  16. * | 本地请求 | 转发到 | 说明 |
  17. * |-------------------------|-------------------------------------|------|
  18. * | `/parse/**` | `https://server.fmode.cn/parse/**` | Parse REST(users/me、roles、classes) |
  19. * | `/api-platform/**` | `https://server.fmode.cn/api/**` | 云函数入口 + `apig/message` + `fmode/mobile`(CORS 兼容层) |
  20. * | `/api/**` | `https://server.fmode.cn/api/**` | 旧路径,保留兼容;新代码一律走 `/api-platform/**` |
  21. *
  22. * 🔴 为什么 app 代码改用 `/api-platform/**`(2026-11-12):
  23. * `server.fmode.cn` 的 `/api/**` 响应偶尔带两行 `Access-Control-Allow-Origin`
  24. * (一行回显 origin,一行 `*`),浏览器按「多个值」直接拦掉。
  25. * 代理把请求挂在 dev server 自己的路径下 → 同源请求 → 浏览器不做 CORS 检查。
  26. * 生产环境同样要求 nginx 反代 `/api-platform/`,见 `docs/05-operations/平台代理部署.md`。
  27. *
  28. * 🔴 只代理这三个前缀,其余请求仍走 SPA 自身路由,避免把前端路由转发出去。
  29. */
  30. module.exports = [
  31. {
  32. context: ['/parse', '/api-platform'],
  33. target: 'https://server.fmode.cn',
  34. changeOrigin: true,
  35. secure: true,
  36. pathRewrite: { '^/api-platform': '/api' },
  37. // 保留 Cookie / Referer 同源语义,与线上一致
  38. cookieDomainRewrite: 'localhost',
  39. logLevel: 'warn',
  40. },
  41. {
  42. // 兼容旧路径:存量调用与脚本仍可能直接打 /api/**
  43. context: ['/api'],
  44. target: 'https://server.fmode.cn',
  45. changeOrigin: true,
  46. secure: true,
  47. cookieDomainRewrite: 'localhost',
  48. logLevel: 'warn',
  49. },
  50. ];