jaywcjlove/mocker-api

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

Opened this issue · 4 comments

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。这无法完成本地前后端联调测试的需求。

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中。

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

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

@jaywcjlove
Thx for your reply.

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

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

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

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

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