axios OPTIONS net::ERR_CONNECTION_REFUSED...但服务器是否按预期运行?!react/express

iezvtpos  于 2023-03-08  发布在  iOS
关注(0)|答案(1)|浏览(254)

我看到已经有很多关于这个主题的帖子了,所以如果这是重复的话,我很抱歉。
奇怪且可能独特的是(我不知道)服务器似乎在正常工作并正确执行API调用。
我有一个React前端,它的Express后端托管在AWS EC2示例上。如上所述,当我的前端发出axios.post请求时,服务器会执行它应该执行的所有操作,但返回了两个错误。
选件http://us-west-1.compute.amazonaws.com:3000网络::错误连接被拒绝
另一个是
错误:网络错误
在创建错误时(创建错误. js:17)
在XMLHttpRequest.handleError(xhr.js:87)中的错误信息
React代码为:

import React from "react";
import PaypalExpressBtn from "react-paypal-express-checkout";
import axios from "axios";

export default class Pay extends React.Component {

constructor(props) {
 super(props);
 this.state = {
  items: {}
  };
 }

  render() {
    const onSuccess = payment => {
      axios
        .post("http://compute.amazonaws.com:3000/", {
          value: this.props.value,
          fileName: this.props.fileName,
          hash: this.props.hash
        })
        .then(response => console.log(response.data))
        .catch(function(error) {
          console.log(error);
        });

      console.log(payment);
    };

    let env = "sandbox"; // you can set here to 'production' for production
    let currency = "USD"; // or you can set this value from your props or state
    let total = 3.33; // same as above, this is the total amount (based on 

    const client = {
      sandbox:
        "...key...",
      production: "YOUR-PRODUCTION-APP-ID"
    };

    return (
      <div>
        <PaypalExpressBtn
          onSuccess={onSuccess}
        />
      </div>
    );
  }
}

快递代码为:

const express = require("express");
const app = express();
const Tx = require("ethereumjs-tx");
var cors = require('cors')
const Web3 = require("web3");
const web3 = new Web3(
  "https://ropsten.infura.io/v3/d55489f8ea264a1484c293b05ed7eb85"
);

app.use(cors());

const abi = [...]

const contractAddress = "0x15E1ff7d97CB0D7C054D19bCF579e3147FC9009b";
const myAccount = "0x59f568176e21EF86017EfED3660625F4397A2ecE";
const privateKey1 = new Buffer(
  "...privateKey...",
  "hex"
);

app.post("/", function(req, res, next) {
 var hashValue = req.body.hash,
 fileName = req.body.fileName,
 value = req.body.value;

  const contract = new web3.eth.Contract(abi, contractAddress, {
    from: myAccount
    // gas: '50000'
  });

  web3.eth.getTransactionCount(myAccount, (err, txCount) => {
    //Smart contract data
    const data = contract.methods
      .setHashValue(value + " " + fileName + " " + hashValue)
      .encodeABI();

    // Build the transaction
    const txObject = {
      nonce: web3.utils.toHex(txCount),
      gasLimit: web3.utils.toHex(1000000),
      gasPrice: 20000000000,
      data: data,
      from: myAccount,
      to: contractAddress
    };

    // Sign the transaction
    const tx = new Tx(txObject);

    const serializedTx = tx.serialize();
    // const raw = '0x' + serializedTx.toString('hex')

    // Broadcast the transaction
    web3.eth
      .sendSignedTransaction("0x" + serializedTx.toString("hex"))
      .on("receipt", console.log);
    next();
  });
});

app.listen(3000, () => console.log("listening on 3000"));

我想重申的是服务器正在按预期广播以太坊交易,我问这个问题的原因是因为我不想出错,我正在检查这是否是我在JSON返回调用中遇到的更大问题的一部分。

kyks70gy

kyks70gy1#

我通过添加res.json()解决了此问题

web3.eth
  .sendSignedTransaction("0x" + serializedTx.toString("hex"))
  .on("receipt", console.log, res.json);

相关问题