VPS 后端服务器是 http 而不是 https

问题描述 投票:0回答:1

我有一个基于 React 的网站,该域名也托管在 godaddy VPS 上。我不太熟悉服务器管理。我通过 Godaddy VPS 服务器拥有 CPANEL 和 WHM。

我的前端反应应用程序

const handleSubmit = async (e) => {
    e.preventDefault();
    try {
        const response = await fetch('https://example.com:8443/send-email', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ email })
        });

还有我的后端 ExpressJS

app.post('/发送电子邮件', (req, res) => { const { email } = req.body;

const mailOptions = {
    from: '[email protected]',
    to: email,
    subject: 'Formatted Email',
    html: `
        <h1>This is a formatted email</h1>
        <p>This email contains some formatted content.</p>
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
        </ul>
        <p>You can also include links: <a href="https://example.com">Example Link</a></p>
    `
};

我可以卷曲并将测试发送到

http://example.com:3001/send-email
而不是
https://example.com:8443/send-email
。 Http 与 Https。

Curl 到 HTTPS 将导致curl: (35) error:0A00010B:SSL 例程::版本号错误

Curl 到 HTTP 就会成功。

我不确定是什么控制或使我的后端服务转到 HTTP,而我的网站使用 HTTPS,如果您尝试转到 http,它将被迫重定向到 https。

reactjs http ssl vps
1个回答
0
投票

要在 Express 上通过 https 协议与后端服务器通信,我们必须使用 https 包。这里是一些 https 通信的示例代码。

const express = require('express');
const https = require('https');
const fs = require('fs');
const path = require('path');

const app = express();

const httpsOptions = {
  key: fs.readFileSync(path.join(__dirname, 'ssl', 'server.key')),
  cert: fs.readFileSync(path.join(__dirname, 'ssl', 'server.crt')),
  // If you have a CA (Certificate Authority) chain, include it here
  // ca: [fs.readFileSync(path.join(__dirname, 'ssl', 'ca1.crt')),
  //     fs.readFileSync(path.join(__dirname, 'ssl', 'ca2.crt'))]
};

https.createServer(httpsOptions, app).listen(443, () => {
  console.log('HTTPS Server running on port 443');
});

在此示例中,您的 ssl 目录中需要有 server.key 和 server.crt 文件。这些文件用于 SSL 认证。

请参考此网址。 在express.js上启用HTTPS

© www.soinside.com 2019 - 2024. All rights reserved.