在我的react应用程序中。我正在尝试根据不同的环境(如本地、开发和产品)配置端点。因此,为了实现这一点,我创建了一个配置文件,其中包含以下详细信息-
端点.json-
{
"User1" : {
"local" : "http://localhost:8080",
"dev" : "http://dev:8080",
"prod" : "http://prod:8080"
},
"User2" : {
"local" : "http://localhost:8081",
"dev" : "http://dev.com:8081",
"prod" : "http://prod.com:8081"
},
"User3" : {
"local" : "http://localhost:8082",
"dev" : "http://dev.com:8082",
"prod" : "http://prod.com:8082"
}
}
组态JSON
{
"Ramy": {
"id": "123",
"user": "Ramy",
"member": "User1"
},
"Joe": {
"id": "134",
"user": "Joe",
"member": "User2"
},
"Joey": {
"id": "356",
"user": "Joey",
"member": "User3"
}
}
我正在action.js中执行api调用。
这是密码-
import configData from "../config/config";
import endpoint from "../config/endpoint";
export const createData = (postData,obj) => dispatch => {
axios({
method: 'POST',
url: "http://dev.com:8080/api",
headers: {
'Content-Type': 'application/json',
'UserData': localStorage.getItem('UserData') || 'No Token Found'
},
data: postData
})
.then (response => {
dispatch({
type: API_CREATE,
payload: response.data
})
})
.catch(function(error) {
dispatch({
type: SET_ERROR_DESC
})
})
}
我希望url具有足够的可配置性,这样我就不需要为不同环境中的不同用户更改端点,它应该会自动检测并相应地重定向。
另外,我的配置中有数据,json,我正在对其进行编码并存储到localstorage。因此,基于登录的用户,我希望为speck端口和环境配置api url。有没有人能让我知道我在这里错过了什么,我怎样才能做到这一点。非常感谢您的帮助。