NodeJS从现有tl创建https.服务器

NodeJS create https from existing tls.Server

本文关键字:创建 https 服务器 tl NodeJS      更新时间:2023-09-26

是否有可能在现有tls之上创建https服务器。服务器文档中写道:"这个类是tls.Server..的一个子类。"。我想使用tls。服务器处理纯数据流,如果需要,让https服务器处理其余部分

问候

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

然而,如果您查看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版本(即当前主版本)。

警告

在升级到新版本的过程中,使用内部Nodeneneneba API可能会咬到你(即,你的应用程序可能在升级后停止工作)。