zoukankan      html  css  js  c++  java
  • [React] Setup an API Proxy in Create React App

    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"
  • 相关阅读:
    【leetcode】下一个排列
    【leetcode】配对交换
    【leetcode】两个相同字符之间的最长子字符串
    052-126&127
    052-125
    052-124
    052-123
    052-122
    052-121
    052-120
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12829274.html
Copyright © 2011-2022 走看看