项目作者: 18boys

项目描述 :
?️ bee-proxy is useful express middleware , which proxys ajax requests to corresponding backend server specified by cli command. 用于本地开发代码的时候,将请求转发到本地的json文件,本地的函数或者是远端的mock服务器,或者是beta,线上服务器.
高级语言: JavaScript
项目地址: git://github.com/18boys/bee-proxy.git
创建时间: 2018-06-10T15:01:19Z
项目社区:https://github.com/18boys/bee-proxy

开源协议:

下载


bee-proxy

⚒ bee-proxy是一个express中间件,可以方便的将本地服务的请求代理到命令行指定的后端环境去.

0.主要特点

  • 1.可以按照不同环境使用不同的代理配置
  • 2.responder支持函数 本地文件夹 字符串域名
  • 3.支持全局规则
  • 4.支持url中存在变量
  • 5.自动生成默认的配置文件

1.主要目标

  • 我们在开发联调以及查找线上问题的时候,经常要使用本地的开发服务器来对接不同的后端系统,能不能找到一种方式,在启动本地服务器的时候
    通过命令行指定要连接的后端
  • 有的时候在前端进入开发之后,后端接口还未就绪,需要前端童鞋使用mock服务器或者本地的json来mock,那么怎么将本地服务的请求都转发到
    对应的mock服务器或者本地对应的json文件呢?

bee-proxy正是为了解决上述两个问题的.

2.开始使用

2.1 安装

  1. npm install bee-proxy --save-dev

安装之后会默认在工程根目录生成wproxy.js文件,里面有配置示例
如果在工程根目录没有发现wproxy.js文件,请手动创建此文件

2.2 在wproxy.js配置转发规则(配置项的详细含义见后面 [3.配置规则说明])

  1. module.exports = {
  2. local: [
  3. {
  4. pattern: /^\/proxy/,
  5. responder: 'https://www.easy-mock.com/mock/5a5e9b7aef967f55f1ce698d/example/',
  6. },
  7. {
  8. pattern: /^\/test/,
  9. responder: '/mock',
  10. },
  11. {
  12. pattern: /^\/test/,
  13. responder: function () {
  14. return {
  15. status: 1
  16. }
  17. },
  18. },
  19. ],
  20. mock: [
  21. {
  22. pattern: /^\/test/,
  23. responder: '/mock',
  24. },
  25. ],
  26. beta: [
  27. {
  28. pattern: /^\/test/,
  29. responder: '/mock',
  30. },
  31. ],
  32. prod: [
  33. {
  34. pattern: /^\/test/,
  35. responder: '/mock',
  36. },
  37. ],
  38. globalRules: [
  39. {
  40. pattern: /^\/test/,
  41. responder: '/mock',
  42. },
  43. ],
  44. };

2.3 给本地的服务器加上bee-proxy中间件

注意: 如果对此不熟,可以参照 [4.一步一步教你加中间件]

  1. var proxy = require('bee-proxy')
  2. app.use('/',proxy)

2.4 启动本地服务器,然后去玩把~

  1. npm start -- ams
  2. # 将后端代理到响应的beta环境,只需要要改变beta=[1,2,3,4,5,6,7,8]就可以将请求代理到对应beta环境
  3. npm start -- beta=4
  4. # 将后端代理到响应的prod环境
  5. npm start -- prod

3.配置规则说明

3.1环境参数配置

示例配置中的 local,mock等key是环境参数,和你启动的时候传入的环境变量参数一致. 这里可以随便指定后端的环境参数
比如说,你可以增加dev配置

  1. dev: [
  2. {
  3. pattern: /^\/test/,
  4. responder: '/mock',
  5. },
  6. ],

然后启动的时候使用

  1. npm start -- dev

然后就可以将所有的请求使用dev的配置进行转发

3.2 pattern参数

pattern指定你的配置规则,只允许正则表达式

3.3 responder参数

指定对应的pattern的响应处理规则,可以是 web url(如https://baidu.com),也可以是本地目录(如/mock),
也可以是函数[函数的入参是(req)]

web url中可以使用 #{beta} 来获取参数,比如说

  1. {
  2. pattern: /^\/proxy/,
  3. responder: 'https://#{beta}.easy-mock.com/mock/#{id}}/example/',
  4. },

在启动的时候传入 beta id 的值

  1. npm start -- local=mock id=5a5e9b7aef967f55f1ce698d

这样最终responder会被重写为 https://mock.easy-mock.com/mock/5a5e9b7aef967f55f1ce698d/example/

这在对应多个beta环境的时候特别有用,比如说可以这样配置

  1. beta:[
  2. {
  3. pattern: /^\/proxy/,
  4. responder: 'https://beta#{beta}.easy-mock.com/mock/123',
  5. },
  6. ]

这样在启动的时候

  1. npm start -- beta=4

就可以将proxy开头的请求打到https://beta4.easy-mock.com/mock/123 环境

这样在启动的时候使用另外的命令

  1. npm start -- beta=5

就可以将proxy开头的请求打到https://beta5.easy-mock.com/mock/123 环境

3.4 全局配置globalRules

🆙 有的时候希望所有的环境的某个请求都走同一个代理,此时可以在globalRules配置
注意此配置的优先级比较低,如果请求同时符合globalRules以及对应环境的配置,此时
globalRules中的配置将会被忽略.

4.一步一步教你加中间件

4.1 使用ykit来进行代码打包构建的工程配置示例

  1. var proxy = require('bee-proxy')
  2. var path = require('path');
  3. module.exports = {
  4. config: {
  5. exports: ['./scripts/app.js'],
  6. modifyWebpackConfig: function (baseConfig) {
  7. // 示例:处理 .hello 类型文件,全部返回文字 "hello!"
  8. this.applyMiddleware(proxy)
  9. return baseConfig;
  10. }
  11. }
  12. };

4.2 vue-cli等使用webpack打包构建的工程 配置示例

早期版本使用http-proxy-middleware来作为本地服务器,这种情况下找到dev-server.js,加入中间件即可.

  1. var proxy = require('bee-proxy')
  2. app.use('/',proxy)

近期版本(2018年6月),使用webpack-dev-server来直接启动本地服务器,像下面的配置

  1. "scripts": {
  2. "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
  3. "start": "npm run dev -- --env",
  4. },

配置稍稍麻烦
首先,找到 /build/webpack.dev.conf.js 文件,找到配置devServer的地方.比如说

  1. const devWebpackConfig = merge(baseWebpackConfig, {
  2. module: {
  3. rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
  4. },
  5. // cheap-module-eval-source-map is faster for development
  6. devtool: config.dev.devtool,
  7. // these devServer options should be customized in /config/index.js
  8. // these devServer options should be customized in /config/index.js
  9. devServer: {
  10. disableHostCheck: true,
  11. clientLogLevel: 'warning',
  12. historyApiFallback: {
  13. rewrites: [
  14. { from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html') },
  15. ],
  16. },
  17. hot: true,
  18. contentBase: false, // since we use CopyWebpackPlugin.
  19. compress: true,
  20. host: HOST || config.dev.host,
  21. port: PORT || config.dev.port,
  22. open: config.dev.autoOpenBrowser,
  23. overlay: config.dev.errorOverlay
  24. ? { warnings: false, errors: true }
  25. : false,
  26. publicPath: config.dev.assetsPublicPath,
  27. proxy: config.dev.proxyTable,
  28. quiet: true, // necessary for FriendlyErrorsPlugin
  29. watchOptions: {
  30. poll: config.dev.poll,
  31. }
  32. },
  33. }

在文件开头引入bee-proxy,然后在devServer引入before项的配置即可.

  1. const proxy = require('bee-proxy')
  2. const devWebpackConfig = merge(baseWebpackConfig, {
  3. module: {
  4. rules: utils.styleLoaders({sourceMap: config.dev.cssSourceMap, usePostCSS: true})
  5. },
  6. // cheap-module-eval-source-map is faster for development
  7. devtool: config.dev.devtool,
  8. // these devServer options should be customized in /config/index.js
  9. devServer: {
  10. disableHostCheck: true,
  11. clientLogLevel: 'warning',
  12. historyApiFallback: {
  13. rewrites: [
  14. {from: /.*/, to: path.posix.join(config.dev.assetsPublicPath, 'index.html')},
  15. ],
  16. },
  17. hot: true,
  18. contentBase: false, // since we use CopyWebpackPlugin.
  19. compress: true,
  20. host: HOST || config.dev.host,
  21. port: PORT || config.dev.port,
  22. open: config.dev.autoOpenBrowser,
  23. overlay: config.dev.errorOverlay
  24. ? {warnings: false, errors: true}
  25. : false,
  26. publicPath: config.dev.assetsPublicPath,
  27. proxy: config.dev.proxyTable,
  28. quiet: false, // necessary for FriendlyErrorsPlugin
  29. watchOptions: {
  30. poll: config.dev.poll,
  31. },
  32. // 主要是下面的配置
  33. before: function (app) {
  34. app.use(proxy)
  35. }
  36. },
  37. }