【问题标题】:Proxy error: Could not proxy request /payment from localhost:3000 to https://localhost:5000/代理错误:无法将请求/付款从 localhost:3000 代理到 https://localhost:5000/
【发布时间】:2020-06-20 20:35:47
【问题描述】:

我正在尝试使用 reactJS 和 expressJS 创建条带支付应用程序,但出现此错误:

代理错误:无法将请求/付款从 localhost:3000 代理到 https://localhost:5000/
请参阅https://nodejs.org/api/errors.html#errors_common_system_errors 了解更多信息(EPROTO)

在 package.json 文件中,我将代理设置为 -

"proxy": "https://localhost:5000"

在我的反应组件中我有 -

const onToken = token => {
    axios({
      url: "payment",
      method: "post",
      data: {
        amount: priceForStripe,
        token: token
      }
    })
      .then(response => {
        alert("succesful payment");
      })
      .catch(error => {
        console.log("Payment Error: ", error);
        alert(
          "There was an issue with your payment! Please make sure you use the provided credit card."
        );
      });
  };

在我的 server.js 中我有 -

const stripe = require("stripe")("sk_test_...");
app.post("/payment", (req, res) => {
  const body = {
    source: req.body.token.id,
    amount: req.body.amount,
    currency: "usd"
  };

  stripe.charges.create(body, (stripeErr, stripeRes) => {
    if (stripeErr) {
      res.status(500).send({ error: stripeErr });
    } else {
      res.status(200).send({ success: stripeRes });
    }
  });
});

每当我提交任何付款时都会出错 -

我尝试了所有链接here 的方法,但无法解决该问题。如果有人解释该问题的任何解决方案,我衷心感谢。

【问题讨论】:

  • 您确定监听localhost:5000的服务使用的是HTTPS吗?也许您需要http://localhost:5000 而不是https://localhost:5000
  • 尝试在不使用前端的情况下使用邮递员之类的东西来测试后端
  • 好主意。让我测试一下@GregM
  • 我也试过http://localhost:5000@CherryDT
  • @NajmunNahar 这表明错误与条纹有关,并且可能与您发送的信息有关。我认为您可能会遗漏正文中的 customer.id。这篇帖子arjunphp.com/node-stripe-express-js 将charges.create 请求的主体显示为{ // charge the customer amount, description: "Sample Charge", currency: "usd", customer: customer.id }

标签: reactjs stripe-payments


【解决方案1】:

我认为代理错误是一个红鲱鱼。真正的问题是您的服务器上的解析,导致 500。

看起来by default Axios 为您编码了 json(但您应该仔细检查请求)。要在 Express 中访问 JSON 编码的请求正文数据,您需要使用 body-parser middleware

请参阅此答案以获取示例:How do I consume the JSON POST data in an Express application

【讨论】:

    【解决方案2】:

    正如@CherryDT 提到的,首先我将代理设置为"proxy": "http://localhost:5000"。然后我按照@Greg M 的建议更改我的后端代码 -

    app.post("/payment", (req, res) => {
      stripe.customers
        .create({
          email: req.body.email, // customer email, which user need to enter while making payment
          source: req.body.token.id // token for the given card
        })
        .then(customer =>
          stripe.charges.create({
            // charge the customer
            amount: req.body.amount,
            description: "Sample Charge",
            currency: "usd",
            customer: customer.id
          })
        )
        .then(charge => res.status(200).send({ success: "success" }));
    });
    

    就是这样。我的付款方式完美无缺。

    【讨论】:

      【解决方案3】:

      由于您的后端在没有条带的情况下可以正常工作,因此 500 错误表明条带是问题所在。

      这与您在条带费用.create 请求的正文中发送的信息有关。我认为您缺少 customer.id。

      这篇文章 arjunphp.com/node-stripe-express-js 将charges.create请求显示为

      { amount, 
        description: "Sample Charge", 
          currency: "usd", 
          customer: customer.id 
      }
      

      【讨论】:

        【解决方案4】:

        我正在学习 Andre 的确切反应课程。我的解决方案是启动后端服务器。

        因此,无论谁在同一门课程中遇到这个问题,要么尝试上面的答案,要么:

        npm start
        

        yarn start
        

        【讨论】:

          猜你喜欢
          • 2023-02-02
          • 1970-01-01
          • 2018-01-04
          • 2021-09-26
          • 1970-01-01
          • 1970-01-01
          • 2021-09-28
          • 2022-11-22
          • 2020-01-31
          相关资源
          最近更新 更多