我有这个问题:
Failed to load resource: net::ERR_SSL_PROTOCOL_ERROR
There was an error! TypeError: Failed to fetch
我正在尝试向服务器发出 https POST 或 GET 请求
此问题仅发生在 Chrome 浏览器中(Firefox、postman 工作正常)
我做了一个简单的服务器:
const express = require('express');
const cors = require('cors');
const fs = require('fs');
const https = require('https');
const bodyParser = require('body-parser');
const app = express();
// CORS middleware to allow cross-origin requests
app.use(cors());
// Parse JSON request body
app.use(bodyParser.json());
app.post('/hello_world', (req, res) => {
const { data } = req.body;
// Your logic...
res.status(200).send("Your response");
});
// SSL certificate
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/myhostname.com/privkey.pem', 'utf8'),
cert: fs.readFileSync('/etc/letsencrypt/live/myhostname.com/cert.pem', 'utf8'),
ca: fs.readFileSync('/etc/letsencrypt/live/myhostname.com/chain.pem', 'utf8')
};
const httpsServer = https.createServer(options, app);
// Listen on either port 443 for https production server
httpsServer.listen(443, () => {
console.log('HTTPS Server running on port 443');
});
请求(前端):
const response = await fetch("https://myhostname.com/hello_world", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({data: "123"}),
});
const rebus = await response.json();
创建的证书:
certbot certonly -d myhostname.com
可能是什么问题以及如何理解原因?
我尝试了其他浏览器(Firefox 效果很好,postman 也很好)