[英]Django/React app API in production vs development?
So I have made a React app that uses Axios to fetch api's. 因此,我制作了一个使用Axios来获取api的React应用。 During development, I would have an api call to 127.0.0.1.
在开发过程中,我将对127.0.0.1进行api调用。 However, my ReactApp resided on localhost:3000.
但是,我的ReactApp驻留在localhost:3000上。 Therefore, it development, I can't just use:
因此,在开发中,我不能仅仅使用:
axios.get('/api/'),
In dev I would need to use: 在开发人员中,我需要使用:
axios.get('127.0.0.1/api/'),
Anybody have any good ideas on how to resolve this conflict so I can see some data in dev? 任何人都对如何解决此冲突有好主意,因此我可以在dev中看到一些数据? Kinda tough to design an UI without any data to populate it.
Kinda很难设计一个无需填充任何数据的UI。 Kinda like buying a shirt without trying it on first (which, I never try anything on, so this is a horrible analogy.)
Kinda喜欢买衬衫而不先尝试(这是我从来没有尝试过的东西,所以这很可怕。)
Use it as in the first example. 与第一个示例一样使用它。 Because it is relative , it will resolve fine for different hosts:
因为它是相对的 ,所以可以解决不同主机的问题:
axios.get('/api')
Will automatically resolve to: 将自动解析为:
// if called by https://example.com/index.js for example
"https://example.com/api"
// if called by localhost/index.js
"https(s)://localhost/api"
In your second example, if you prepend the host
and port
, you will get duplication! 在第二个示例中,如果在
host
和port
前缀,则会得到重复!
For example, I just tried your first example on my localhost:3000 and the result is 例如,我刚刚在本地主机上尝试了第一个示例:3000,结果是
GET http://localhost:3000/api 404 (Not Found)
Which makes sense because I don't have a /api. 这是有道理的,因为我没有/ api。 But did you notice it appended /api correctly after my host and port?
但是您是否注意到它在我的主机和端口之后正确附加了/ api?
Now your second example: 现在您的第二个示例:
GET http://localhost:3000/127.0.0.1/api 404 (Not Found)
It duplicates the host and port. 它复制主机和端口。 In your case it would be
127.0.0.1:3000/127.0.0.1/api
在您的情况下为
127.0.0.1:3000/127.0.0.1/api
Just use the first example and it will resolve fine for different hosts (and ports) because it's relative! 仅使用第一个示例,由于它是相对的,它可以很好地解决不同主机(和端口)的问题! Did you try it out?
你有试过吗
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.