When `proxy` in package.json is an object, each `context` object must have a `target` property specified as a url string
时间: 2023-10-22 10:05:20 浏览: 106
that represents the destination for the proxy. The `context` property specifies the path or paths that should be redirected to the specified `target`.
For example, if we have the following `proxy` configuration in our `package.json` file:
```json
"proxy": {
"/api": {
"target": "http://localhost:3000"
}
}
```
This means that any request that starts with `/api` will be redirected to `http://localhost:3000`. So, if we have a request like `http://localhost:8000/api/users`, it will be redirected to `http://localhost:3000/api/users`.
Additionally, we can specify other properties in the `context` object to further configure the proxy behavior. For example, we can use the `pathRewrite` property to rewrite the path before it is sent to the target:
```json
"proxy": {
"/api": {
"target": "http://localhost:3000",
"pathRewrite": {
"^/api": "/v1"
}
}
}
```
In this case, any request that starts with `/api` will be redirected to `http://localhost:3000/v1`, effectively rewriting the path by replacing `/api` with `/v1`.