微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

NodeJS将SSL添加到HTTPserver

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。