前端开发在调试 api 阶段,或者在 fix bug 时, 常常会遇到须要不断切换代理环境的问题, 很让人头大。因此一个灵活的代理逻辑能让你省去好几支烟的功夫。javascript
下面记录一次项目中代理的进化方案。前端
经过环境变量来肯定代理方向环境,java
const getEndpoint = () => { switch (process.env.EP) { case 'qa': return 'https://qa.xxx.cn'; case 'online': return 'https://xxx.cn'; case 'dev': return 'https://test.xxx.cn'; default: return 'http://dev2.xxx.cn'; } }; module.exports = { '/api': { target: endpoint, changeOrigin: true, headers: { Host: new URL(endpoint).hostname } } };
react-scripts
默认生效的代理文件 /src/setupProxy.js
:react
module.exports = function(app) { Object.entries(apiProxyConfig).forEach(([key, value]) => { app.use(key, proxy(value)); }); };
这种方式,在开发环境启动的过程当中就肯定了代理环境,且启动后没法改变代理环境, 除非修改环境变量,重启服务。当业务模块不断增长,开发环境启动的时间也愈来愈长,每次须要频繁切换环境验证 bug 时, 就会很耗时间。api
因此须要进化一下,帮咱们省一支烟的时间。浏览器
介绍 modheaderapp
Chrome
浏览器扩展,主要用来 添加/修改/删除请求和响应头,根据URL Pattern来只对特定网站生效,等等用法。以下图,详见modheader。网站
原理url
经过浏览器对请求信息的劫持再编辑,经过读取请求头自定义添加的值来肯定代理的环境, 这样就把代理环境的作成动态的,可经过浏览器插件自定义切换代理环境了。spa
module.exports = function (app) { const map = new Map(); Object.entries(apiProxyConfig).forEach(([key, value]) => { app.use(key, (req, res, next) => { const endpoint = req.headers['x-api-endpoint']; if (endpoint && /^https?:\/\//.test(endpoint)) { let handler = null; if (map.has(endpoint)) { handler = map.get(endpoint); } else { handler = createProxyMiddleware({ target: endpoint, changeOrigin: true, headers: { Host: new URL(endpoint).hostname }, pathRewrite: '^/api' }); map.set(endpoint, handler); } const path = req.url; const prefix = '/api'; // eslint-disable-next-line no-console console.log(`[Proxy] /api${path} => ${endpoint}${prefix}${path}`); return handler(req, res, next); } return next(); }); }); };
多人协做
为避免不一样开发者重复配置 modheader
,须要将 modheader
配置推广之,刚好modheader
支持导出和导入,如图:
因此只需一人配置,分享连接给其余人导入便可使用。
本地 mock 数据
本地 mock
和代理环境并存。
module.exports = function (app) { const mockFile = resolve(__dirname, '../mock/mock.js'); if (existsSync(mockFile)) { apiMocker(app, mockFile); } const map = new Map(); Object.entries(apiProxyConfig).forEach(([key, value]) => { // ... }) }
以上方案目前彻底能知足开发需求,且方便易用。
欢迎你们评论大家业务中的前端环境代理。
更复杂的代理,推荐文章以下: