NodeJS将SSL添加到HTTPserver

问题描述

我正在尝试将SSL添加到示例WebRTC视频聊天应用程序的HTTPserver。 我已经尝试将SSL添加到我的Lighttpd中,并且仅添加到代理中,但是Socket.IO连接由于混合了https / nonhttps内容而无法正常工作。我想为此需要一个独立的节点https服务器应用程序。 我是Node的新手,需要帮助...

这是我的应用程序:

index.ts

BearerTokenHandler

server.ts

IdentityModel

解决方法

使用https库而不是http

const https = require('https');
const fs = require('fs');
const privateKey = fs.readFileSync('./localhost.key','utf8');
const certificate = fs.readFileSync('./localhost.crt','utf8');

const credentials = {
  key: privateKey,cert: certificate,};

const httpsServer = https.createServer(credentials,this.app);

可以这样生成自签名证书:

openssl req -x509 -out localhost.crt -keyout localhost.key \
  -newkey rsa:2048 -nodes -sha256 \
  -subj '/CN=localhost' -extensions EXT -config <( \
   printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")

有关更多信息,请参见https://letsencrypt.org/docs/certificates-for-localhost/#making-and-trusting-your-own-certificates

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...