gpt4 book ai didi

javascript - NodeJS 从现有的 tls.Server 创建 https

转载 作者:行者123 更新时间:2023-12-02 18:35:00 25 4
gpt4 key购买 nike

是否有可能在现有的 tls.Server 之上创建 https 服务器?文档说:“这个类是 tls.Server 的子类..”。我想使用 tls.Server 来处理纯数据流,如果需要,让 https 服务器处理其余的事情。 (就像带https的express一样,只是在较低的一层)

问候

最佳答案

没有任何官方/支持的方法。

但是,如果你查看 https 服务器的源代码,它只是将 TLS 服务器和 HTTP 连接处理程序连接在一起的粘合剂:

function Server(opts, requestListener) {
if (!(this instanceof Server)) return new Server(opts, requestListener);

if (process.features.tls_npn && !opts.NPNProtocols) {
opts.NPNProtocols = ['http/1.1', 'http/1.0'];
}

/// This is the part where we instruct TLS server to use
/// HTTP code to handle incoming connections.
tls.Server.call(this, opts, http._connectionListener);

this.httpAllowHalfOpen = false;

if (requestListener) {
this.addListener('request', requestListener);
}

this.addListener('clientError', function(err, conn) {
conn.destroy();
});

this.timeout = 2 * 60 * 1000;
}

要在 TLS 连接处理程序中切换到 HTTPS,您可以执行以下操作:

var http = require('http');

function myTlsRequestListener(cleartextStream) {
if (shouldSwitchToHttps) {
http._connectionListener(cleartextStream);
} else {
// do other stuff
}
}

上面的代码基于版本0.11(即当前的master)。

警告

在升级到新版本的过程中,使用内部 Node API 可能会给您带来麻烦(即您的应用程序可能会在升级后停止工作)。

关于javascript - NodeJS 从现有的 tls.Server 创建 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17426849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com