44 lines
1.1 KiB
Markdown
44 lines
1.1 KiB
Markdown
|
# 代理:
|
||
|
### 方式一
|
||
|
只能使用一种代理
|
||
|
package.json文件中添加
|
||
|
"proxy":"http://localhost:5000"
|
||
|
自身没有的资源会请求5000,自身请求http://localhost:3000/
|
||
|
### 方式二
|
||
|
src根目录下创建文件setupProxy.js
|
||
|
```html
|
||
|
const proxy = require('http-proxy-middleware')
|
||
|
module.exports=function(app){
|
||
|
app.use(
|
||
|
proxy('/api1',{
|
||
|
target:'http://localhost:5000',
|
||
|
changeOrigin:true,
|
||
|
pathRewrite:{'^/api1':''}
|
||
|
}),
|
||
|
proxy('/api2',{
|
||
|
target:'http://localhost:5001',
|
||
|
changeOrigin:true,
|
||
|
pathRewrite:{'^/api2':''}
|
||
|
})
|
||
|
)
|
||
|
}
|
||
|
```
|
||
|
|
||
|
|
||
|
```
|
||
|
const {createProxyMiddleware} = require('http-proxy-middleware')
|
||
|
module.exports=function(app){
|
||
|
app.use(
|
||
|
createProxyMiddleware('/api1',{
|
||
|
target:'https://github.com',
|
||
|
changeOrigin:false,
|
||
|
pathRewrite:{'^/api1':''}
|
||
|
}),
|
||
|
createProxyMiddleware('/api2',{
|
||
|
target:'http://localhost:5001',
|
||
|
changeOrigin:true,
|
||
|
pathRewrite:{'^/api2':''}
|
||
|
})
|
||
|
)
|
||
|
}
|
||
|
```
|