简体   繁体   English

如何避免在 reactJS 中硬编码 URL?

[英]How to avoid hard coding URL in reactJS?

I am new to reactJS and our current web application(Spring MVC) is HTTP based API.我是 reactJS 的新手,我们当前的 Web 应用程序(Spring MVC)是基于 HTTP 的 API。 We are trying to convert JSPs to reactJS components.我们正在尝试将 JSP 转换为 reactJS 组件。 I observed that reactJS developer hard-coded base url(like http://DNSName:8080/AppContext ) and using that making API call.我观察到 reactJS 开发人员硬编码基本 url(如http://DNSName:8080/AppContext )并使用它进行 API 调用。 But, this base url is different for each client like url contains different DNS Name and port number.但是,这个基本 url 对于每个客户端都是不同的,比如 url 包含不同的 DNS 名称和端口号。 Is there any other way to not hard-code base url and get it from environment?有没有其他方法可以不硬编码基本网址并从环境中获取它?

您可以使用相对 url /AppContext,或者如果这不是您想要的,您可以查看节点环境变量并像 process.env.my_url 一样访问它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM