jaywcjlove / jaywcjlove/mocker-api

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

Open
#97 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
500
Forks
64
PR merge metrics
No merged PRs in 30d

Description

### 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

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the proxy and GET endpoint configuration shown in the issue, then reproduce the two cases: both entries present and the endpoint removed. Trace which route handles `/api/v1/login/:userId`; done means the behavior is explicitly resolved so the local API at `http://localhost:2080/` receives the request when both configurations are present.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.