Express JS https api 无法访问

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

我正在使用 Express js 将 node.js api 从 http 更新为 https。它托管在 AWS 上,通过其 IP 地址而不是 Web 域进行访问。

我遵循 Express 文档,并使用

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./selfsigned.key -out selfsigned.cert

创建了自签名证书

但是,我的应用程序无法使用 https 访问服务器,而它可以使用 http。这些是我得到的错误:

  • 申请回复:需要证书
  • 邮递员回复:连接被拒绝
  • Powershell 响应:Invoke-WebRequest:底层连接已关闭:无法建立 SSL/TLS 安全通道的信任关系。

问题可能是证书是自签名的吗?我的代码如下

let fs = require("fs");
let https = require('https');

const crypto = require('crypto')
let express = require('express');
let expressRateLimit = require("express-rate-limit");
let mysql = require('mysql');
let bodyParser = require('body-parser');
let winston = require('winston');
require('winston-daily-rotate-file');

let key = fs.readFileSync('sslcert/selfsigned.key');
let cert = fs.readFileSync('sslcert/selfsigned.crt');
let credentials = {key: key, cert: cert};

let app = express();
app.set('trust proxy', '127.0.0.1');
app.use(bodyParser.urlencoded({extended: true}));

//App config

let httpsServer = https.createServer(credentials, app);
let httpsPort = 443;

httpsServer.listen(httpsPort, function() {
   console.log("Started codeRedeem https REST API on port " + httpsPort);
});
node.js express https ssl-certificate
1个回答
0
投票

这段代码对我有用!

在app.json中

把这个放进去

{ “博览会”: {

"plugins": [
  ["expo-build-properties", {
    "android": {
      "usesCleartextTraffic": true
    },
    "ios": {
     
    }
  }]

} }

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