简体   繁体   English

如何添加 axios 代理?

[英]How to add axios proxy?

I'm trying to add a proxy to my application so when I make a query to my backend, instead of having to make a query to 'https://localhost:5000/api/users' I only have to make a query to 'api/users'.我正在尝试向我的应用程序添加代理,因此当我对后端进行查询时,不必查询“https://localhost:5000/api/users”,我只需查询'api/用户'。 How would I go about doing this?我将如何 go 这样做? Below is my package.json, I would really appreciate any direction on how to modify it to make this adjustment.以下是我的 package.json,我非常感谢任何有关如何修改它以进行此调整的指导。 Thank you!谢谢!

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject",
    "debug": "open 'rndebugger://set-debugger-loc?host=localhost&port=19001'"
  },
  "dependencies": {
    "@expo-google-fonts/quicksand": "^0.1.0",
    "@react-native-community/masked-view": "0.1.10",
    "@react-navigation/bottom-tabs": "^5.11.7",
    "@react-navigation/native": "^5.9.2",
    "@react-navigation/stack": "^5.14.2",
    "axios": "^0.21.1",
    "expo": "~40.0.0",
    "expo-app-loading": "^1.0.1",
    "expo-font": "~8.4.0",
    "expo-image-picker": "~9.2.0",
    "expo-status-bar": "~1.0.3",
    "moment": "^2.29.1",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-moment": "^1.1.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-40.0.1.tar.gz",
    "react-native-dropdown-picker": "^3.8.0",
    "react-native-gesture-handler": "~1.8.0",
    "react-native-gifted-chat": "^0.16.3",
    "react-native-reanimated": "~1.13.0",
    "react-native-safe-area-context": "3.1.9",
    "react-native-screens": "~2.15.2",
    "react-native-web": "~0.13.12",
    "react-navigation": "^4.4.3",
    "react-redux": "^7.2.2",
    "react-router-dom": "^5.2.0",
    "redux": "^4.0.5",
    "redux-devtools-extension": "^2.13.8",
    "redux-thunk": "^2.3.0",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "@babel/core": "~7.9.0"
  },
  "private": true
}

add the proxy option to your axios request, like so:proxy选项添加到您的 axios 请求中,如下所示:

const res = await axios.get('/api/users', {
  proxy: {
    host: 'localhost',
    port: 5000
  }
});

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

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