weixin_39709367
2020-12-25 18:14 阅读 0

Proxy didn't work when proxy and API endpoint configs both exist

Environment

"mocker-api": "^1.12.2"

Issue

I have an back-end API server running locally on port 2080, and I want my front-end which running locally as well to call this local API server, because I want to do integrate test between front-end and back-end. But I found that Proxy didn't work when proxy and API endpoint configs both exist(just returned the response.json, and API server didn't receive any request ):

(Chinese translation) 我在本地有前端和后端两个服务,后端API server运行于端口2080,如下, 如果我同时配置proxy和endpoint,只会返回/response.json,并没有proxy到2080的API server。这无法完成本地前后端联调测试的需求。

java
const proxy = {
  _proxy: {
    proxy: {
      '/api/v1/(.*)': 'http://localhost:2080/'
    }
  },

  // API endpoint describe here
   'GET /api/v1/login/:userId': (req, res) => {
     return res.json(require('./response.json'));
   }

};
module.exports = proxy;

If I remove the endpoint config, front end will call proxy which is http://localhost:2080/:

(Chinese translation) 如果我删除endpoint配置,只留proxy,这时/api/v1/login/可以转发到我的本地2080 API server中。

java
const proxy = {
  _proxy: {
    proxy: {
      '/api/v1/(.*)': 'http://localhost:2080/'
    }
  },

  // API endpoint describe here  (REMOVE IT !)
  // 'GET /api/v1/login/:userId': (req, res) => {
  //    return res.json(require('./response.json'));
  // }

};
module.exports = proxy;

Then the API server locally will receive request call for /api/v1/login/:userId now.

Please help about what's wrong here? Thanks

该提问来源于开源项目:jaywcjlove/mocker-api

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • weixin_39825045 weixin_39825045 2020-12-25 18:14

    -glp 可以参考实例中配置 _proxy 只是个方便的配置,另外个地方可以配置,是不是冲突了

    点赞 评论 复制链接分享
  • weixin_39709367 weixin_39709367 2020-12-25 18:14

    Thx for your reply.

    我参考了示例配置,依旧不行。能否给我个example链接?

    你是否测试过我这种配置方法?当两者(proxy和endpoint)都配置的情况下,是否proxy不起作用,而endpoint的优先级更高?

    点赞 评论 复制链接分享
  • weixin_39825045 weixin_39825045 2020-12-25 18:14

    -glp https://github.com/jaywcjlove/mocker-api/tree/master/example

    点赞 评论 复制链接分享
  • weixin_39709367 weixin_39709367 2020-12-25 18:14

    -glp https://github.com/jaywcjlove/mocker-api/tree/master/example

    没有看到使用 proxy: { '/api/v1/(.*)': 'http://localhost:2080/' } 的例子,都被注释了。

    你是否测试过我这种配置方法?当两者(proxy和endpoint)都配置的情况下,是否proxy不起作用,而endpoint的优先级更高?

    点赞 评论 复制链接分享

相关推荐