尝试使支持HTTPS请求的Express.JS服务器运行很麻烦。
这是我到目前为止编写的代码:
import express from 'express';
import fs from 'fs';
import https from 'https';
const app = express();
const port = 3000;
const options = {
cert: fs.readFileSync('server.crt', 'UTF-8'),
key: fs.readFileSync('server.key', 'UTF-8'),
};
app.get('/', (request, response) => response.send('Success!'));
https.createServer(options, app).listen(port, () => console.log(`Listening on port ${port}`));
我已经在macOS上使用OpenSSL创建了SSL凭据,如下所示:
openssl req -nodes -new -x509 -out server.crt -keyout server.key
我还尝试了其他一些论点,包括将密钥强度增加到2048位,但没有任何效果。我相当确定OpenSSL不是这里的问题。对https://localhost:3000/
的请求失败。我已经通过request
模块通过Google Chrome,Node.JS以及cURL发出了请求。
Chrome错误:NET::ERR_CERT_INVALID
Node.JS错误:Error: connect ETIMEDOUT 172.0.0.1:3000
cURL错误:curl: (7) Failed to connect to 172.0.0.1 port 3000: Operation timed out
我已经用尽了Google搜索功能,因此任何帮助将不胜感激。谢谢。