首页 > 移动平台 > 详细

[React] Setup an API Proxy in Create React App

时间:2020-05-05 10:08:27      阅读:65      评论:0      收藏:0      [点我收藏+]

For development, we‘ll be using a separate server address to reach our REST endpoints. In a production build, this will likely be a different address, and in many cases, our UI will be served from the same domain as our services. We can keep our code clean and still prepare for the differences between dev and prod, by using an API proxy and a relative path to our REST endpoints. In this lesson, we‘ll configure a proxy for create-react-app and refactor our code to use a relative API path.

 

Component:

// From
React.useEffect(() => {
    fetch(‘https://adaptive-basilisk.glitch.me/api/card)
      .then(res => res.json())
      .then(setCards)
  }, [])

// To
React.useEffect(() => {
    fetch(/api/card)
      .then(res => res.json())
      .then(setCards)
  }, [])

 

In pacakge.json add "proxy":

"proxy": "https://adaptive-basilisk.glitch.me"

 

[React] Setup an API Proxy in Create React App

原文:https://www.cnblogs.com/Answer1215/p/12829274.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!