简体   繁体   English

NodeJS 的“https”package 是否使“http”变得多余?

[英]Does “https” package of NodeJS make “http” redundant?

When I learned NodeJS a few years ago, 'http' module had been introduced.几年前我学习 NodeJS 时,已经引入了“http”模块。 But I suppose, it's not the best native module from the viewpoint of security and other functionality in 2021. The first that one that attracts the most attention is https .但我想,从安全性和其他功能的角度来看,它并不是 2021 年最好的原生模块。最受关注的第一个是https

I know that the HTTPS protocol is an essential of web development for the 2020's.我知道 HTTPS 协议是 2020 年 web 开发的必要条件。 But does the https package fully substitute the http module, and will it work if no https domain is available in a certain project?但是https ZEFE90A8E60A7C840EE840EE88D03AB7D67F67F67F67D8Z替代http 7002C24688C88C59FAAA8Z ANIF WILLID IF WILL CANIF and if and If Willif and If Wills If and if and If and if and if and if and if and if and if and if and if and if and if and if and if and if and if https

But does https package fully substitutes the http and will it work if no https domain available in specific project?但是https package 是否完全替代了http并且如果在特定项目中没有 Z5E056C500A1C487BADEZ1 可用,它会起作用吗?

https does not substitute http , because https specifically expects communication under the HTTPS protocol. https不能替代http ,因为https特别期望在 Z0E8433F9A404F1D3B160 协议下进行通信。

const req = require('https').request('http://www.example.com');
TypeError [ERR_INVALID_PROTOCOL]: Protocol "http:" not supported. Expected "https:"
    at new ClientRequest (_http_client.js:152:11)
    at Object.request (https.js:314:10)
    at repl:1:17
    at Script.runInThisContext (vm.js:120:20)
    at REPLServer.defaultEval (repl.js:433:29)
    at bound (domain.js:426:14)
    at REPLServer.runBound [as eval] (domain.js:439:12)
    at REPLServer.onLine (repl.js:760:10)
    at REPLServer.emit (events.js:327:22)
    at REPLServer.EventEmitter.emit (domain.js:482:12) {
  code: 'ERR_INVALID_PROTOCOL'
}

Whether there is ever interest in making the https module sufficiently flexible to also work with HTTP (with the purpose of making the http module redundant), regardless of the security implications, is a matter of opinion and eventual design decisions of Node.js, but seems too unlikely. Whether there is ever interest in making the https module sufficiently flexible to also work with HTTP (with the purpose of making the http module redundant), regardless of the security implications, is a matter of opinion and eventual design decisions of Node.js, but seems too不太可能。 Heavy changes to the Node.js standard library would affect a major portion of the JavaScript ecosystem, and those projects would not stop depending on http so soon, even with such a change. Node.js 标准库的重大更改将影响 JavaScript 生态系统的主要部分,并且这些项目不会因为http而停止,即使有这样的更改。

In practice, one is better off using one of the many user-friendly HTTP client libraries out there , most of which handle both protocols automatically.在实践中,最好使用许多用户友好的 HTTP 客户端库之一,其中大多数会自动处理这两种协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM