【问题标题】:How to Setup Proxy Target in React Native with Nodejs?如何使用 Nodejs 在 React Native 中设置代理目标?
【发布时间】:2020-05-12 12:19:16
【问题描述】:

尝试配置代理以响应本机与节点运行 axios 调用。

在 server/package.json 中尝试了以下代码

"proxy": {
    "/*": {
      "target": "http://localhost:5000/"
    }
  },
  "scripts": {
    "start": "node index.js",
    "server": "nodemon index.js",
    "client": "cd ../client && yarn ios",
    "dev": "concurrently \"yarn server\" \"yarn client\""
  }

服务器/authRouter.js

const authRouter = require('express').Router();
authRouter.get('/test', (req, res) => {
    res.send('proxy success');
  });

module.exports = authRouter;

服务器/index.js

const express = require('express');
const authRouter = require('./authRouter');
const app = express();
app.use('/auth', authRouter);
const PORT = process.env.PORT || 5000;
app.listen(PORT);

客户端/app.js

await axios.get('/auth/test');

当我运行 yarn dev 并测试 axios 调用时,它会记录以下错误

 LOG      [Error: Network Error]

任何帮助将不胜感激。

【问题讨论】:

    标签: node.js react-native express axios backend


    【解决方案1】:

    尝试在axios中直接调用代理 http://localhost:5000/auth/test

    我不知道为什么它不能那样工作,即使我过去遇到过问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-13
      • 2018-05-30
      • 1970-01-01
      相关资源
      最近更新 更多