經常使用的ajax庫:前端
在package.json中追加以下配置node
"proxy":"http://localhost:5000"
複製代碼
說明:react
第一步:建立代理配置文件ios
在src下建立配置文件:src/setupProxy.js
複製代碼
編寫setupProxy.js配置具體代理規則:ajax
const proxy = require('http-proxy-middleware')
module.exports = function(app) {
app.use(
proxy('/api1', { //api1是須要轉發的請求(全部帶有/api1前綴的請求都會轉發給5000)
target: 'http://localhost:5000', //配置轉發目標地址(能返回數據的服務器地址)
changeOrigin: true, //控制服務器接收到的請求頭中host字段的值
/* changeOrigin設置爲true時,服務器收到的請求頭中的host爲:localhost:5000 changeOrigin設置爲false時,服務器收到的請求頭中的host爲:localhost:3000 changeOrigin默認值爲false,但咱們通常將changeOrigin值設爲true */
pathRewrite: {'^/api1': ''} //去除請求前綴,保證交給後臺服務器的是正常請求地址(必須配置)
}),
proxy('/api2', {
target: 'http://localhost:5001',
changeOrigin: true,
pathRewrite: {'^/api2': ''}
})
)
}
複製代碼
說明:json