gpt4 book ai didi

node.js - Node 中的 HTTPS TLS 设置

转载 作者:太空宇宙 更新时间:2023-11-03 22:21:21 25 4
gpt4 key购买 nike

我今天查看了我的代码库,设置服务器的部分并发现了以下行:

var https = require('https');
https.globalAgent.options.secureProtocol = 'TLSv1_2_method';

function createHttpsServer(app) {
var https = require('https');
var fs = require('fs');
const options = {
secureProtocol: 'TLSv1_2_method',
// ...
};
var server = https.createServer(options, app);
return server;
}

对我来说,这看起来像是代码重复,我不确定为什么它们会做不同的事情(或者它们确实如此?)。

我的一位同事告诉我,最上面的一个是用于控制 NodeJS 发出的 HTTPS 请求中的 TLS,这反过来又使我们能够访问用于所有相关内容的 https.agent到客户端 HTTP 请求。

这也与 .NET 世界中的 ServicePointManager 进行了比较。

那么这些方法都做不同的事情吗?在某些时候,我们的代码会:

var server = protocol === 'https' ? createHttpsServer(app) : createHttpServer(app);

最终不会使用同一台服务器吗?

最佳答案

var server = protocol === 'https' ? createHttpsServer(app) : createHttpServer(app);

上面的行创建了相同的服务器,唯一的区别是如果协议(protocol)是“https”,它将在 HTTPS 服务器上运行(这需要 SSL 证书),而如果协议(protocol)是 http,它将在 HTTP 服务器上运行。

关于node.js - Node 中的 HTTPS TLS 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54527028/

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