【问题标题】:SSL_PROTOCOL_ERROR, Post request, client, api, sslSSL_PROTOCOL_ERROR,发布请求,客户端,api,ssl
【发布时间】:2020-10-10 06:45:26
【问题描述】:

我收到以下错误:(index):24 POST https://goldengates.club:3000/api/transact net::ERR_SSL_PROTOCOL_ERROR (anonymous) @ (index):24 goldengates.club/:1 Uncaught (in promise) TypeError: Failed to fetch Promise.then (async) (anonymous) @ (index):26 尝试从客户端向我的 api 后端发出发布请求时。我已经使用 express 打开了端口 3000,以便在客户端将数据发布到的名为 api/transact 的端点上接受请求。问题似乎出在我当前的 ssl 上,但我不知道具体是什么问题。 我与服务器提供商联系,他们告诉我引用“您可能需要重新配置在端口 3000 上运行的应用程序以使用 SSL 加密数据。如果您需要证书文件,它们应该在用户帐户的 ssl 目录中可用。”万一这给出了提示。

相关服务器API代码:

const Blockchain = require('./blockchain');
const PubSub = require('./app/pubsub');
const TransactionPool = require('./wallet/transaction-pool');
const Wallet = require('./wallet');
const TransactionMiner = require('./app/transaction-miner');
const PubSubNub = require('./app/pubsub.pubnub');
//127.0.0.1:6379
const isDevelopment = process.env.ENV === 'development';
//TRY PUBNUB (comment out)
/*const REDIS_URL = isDevelopment ?
  'redis://127.0.0.1:6379' : //try 6379 19289
  'redis://h:p602b6838e89da65c8c4d29a6a4f954452d1ece59c10b27a29ebf9808721cb8e2@ec2-35-153-115-238.compute-1.amazonaws.com:9819'//19289
*/  
const DEFAULT_PORT = 3000;
const ROOT_NODE_ADDRESS = 
`http://localhost:${DEFAULT_PORT}`;

const app = express();
const blockchain = new Blockchain();
const transactionPool = new TransactionPool();
const wallet = new Wallet();
//const pubsub = new PubSub({ blockchain, transactionPool, redisUrl: REDIS_URL });//redis
const pubsub = new PubSubNub({ blockchain, transactionPool, wallet }); // for PubNub //change back to PubSub if issues arise
const transactionMiner = new TransactionMiner({ blockchain, transactionPool, wallet, pubsub });
//DELETE THIS
/*const config =
{
    headers: {'Access-Control-Allow-Origin':'*'}
};*/
//TEST DELETE

app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'client/dist')));
//delete below
//app.options('*', cors());

app.use(cors({
    origin: 'https://goldengates.club',
    allowedHeaders: ["Content-Type"],
    methods: 'POST'
}));

SKIPPING TO FoCUSED ENDPOINT...

app.post('/api/transact',(req, res) => {

  const { amount, recipient } = req.body;
  //REMOVE
  console.log(amount);
  //REMOVE
  let transaction = transactionPool
    .existingTransaction({ inputAddress: wallet.publicKey });

  try {
    if (transaction) {
      transaction.update({ senderWallet: wallet, recipient, amount });
    } else {
      transaction = wallet.createTransaction({
        recipient,
        amount,
        chain: blockchain.chain
      });
    }
  } catch(error) {
    return res.status(400).json({ type: 'error', message: error.message });
  }

  transactionPool.setTransaction(transaction);

  pubsub.broadcastTransaction(transaction);

  res.json({ type: 'success', transaction });
});

客户端代码:

<!DOCTYPE html>

<html>
    <head>

    </head>
    <body>

    <script>
        const amount=10;
        const recipient=1221232;
        const data={amount,recipient};

        const options=
        {
            method: 'POST',
            headers:
            {
                'Content-Type': 'application/json',
            },

            body: JSON.stringify(data)
        };
        fetch('https://www.goldengates.club:3000/api/transact',options)
        .then(response => response.json())
        .then(data => console.log(data));
    </script>
    </body>
</html>

【问题讨论】:

    标签: node.js api ssl https cross-domain


    【解决方案1】:
    https://goldengates.club:3000/api/transact net::ERR_SSL_PROTOCOL_ERROR    
    

    您在端口 3000 上的应用程序未设置为 HTTPS,而仅用于 HTTP。尝试使用 HTTPS 访问普通 HTTP 服务器会导致您看到错误。请注意,您不能简单地将客户端中的 http://host:porthttps://host:port 切换,并期望服务器可以通过 HTTPS 神奇地使用。

    ...他们告诉我引用“您可能需要重新配置在端口 3000 上运行的应用程序以使用 SSL 加密数据。如果您需要证书文件,它们应该在用户帐户的 ssl 目录中可用。”

    他们指出的完全一样。

    除此之外,使用 HTTPS 公开 nodejs API 的常用方法不是让 nodejs 服务器本身能够支持 HTTPS,而是将其隐藏在诸如 nginx 之类的反向代理中 - 例如参见 Configuring HTTPS for Express and Nginx

    【讨论】:

    • 嘿,感谢您的回复。可以用apache配置我的3000端口在https上运行吗?
    • 我已经修复了错误,我添加了一个连接选项来通过 ssl 连接。我添加了以下对象以在侦听特定端口时使用...const httpsOptions = { cert: fs.readFileSync(path.join(__dirname,'ssl/certs','goldengates_club_ad6a3_c1c83_1599436799_f7d90f2bde299ad7cb014f9ef76de722.crt')), key: fs.readFileSync(path.join(__dirname,'ssl/keys','ad6a3_c1c83_17344a543062170b02640b27891d7770.key'))};
    • const PORT = process.env.PORT || PEER_PORT || DEFAULT_PORT; /*app.*/https.createServer(httpsOptions, app).listen(PORT, () =&gt; { console.log(`listening at localhost:${PORT}`); if (PORT !== DEFAULT_PORT) { syncWithRootState(); } });
    • 需要以下库... npm i https 和 npm i fs
    猜你喜欢
    • 1970-01-01
    • 2017-02-21
    • 2022-10-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2018-11-26
    • 2013-02-25
    相关资源
    最近更新 更多