您什么时候应该通过HTTP2使用节点的HTTP?

发布于 2025-02-07 13:02:17 字数 143 浏览 2 评论 0原文

在哪些情况下,您应该在http2模块上使用节点中的http模块?即使使用http/2的连接更快,大多数网站已经切换到它,大多数人和库似乎仍在使用http模块。这是什么原因?提前致谢。

In which cases should you use the http module in node over the http2 module? The majority of people and libraries seem to still be using the http module, even though connections using HTTP/2 are faster and most websites have already switched to it. What's the reason for that? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你穿错了嫁妆 2025-02-14 13:02:17

HTTP/2启用完整的请求和响应多路复用。实际上,这意味着可以使用浏览器与Web服务器建立的连接来发送多个请求并接收多个响应。这消除了为每个请求建立新连接所需的许多额外时间。

    /*
     * Example HTTP2 Server
     * Opening a full-duplex (stream) channel on port 6000
     *
     */
    
    // Dependencies
    var http2 = require('http2');
    
    // Init the server
    var server = http2.createServer();
    
    // On a stream, send back hello world html
    server.on('stream', function (stream, headers) {
      stream.respond({
        ':status': 200,
        'content-type': 'text/html',
      });
      stream.end('<html><body><p>Hello World</p></body></html>');
    });
    
    // Listen on 6000
    server.listen(6000);

/*
 * Example HTTP2 client
 * Connects to port 6000 and logs the response
 *
 */

// Dependencies
var http2 = require('http2');

// Create client
var client = http2.connect('http://localhost:6000');

// Create a request
var req = client.request({
  ':path': '/'
});

// When message is received, add the pieces of it together until you reach the end
var str = '';
req.on('data',function(chunk){
  str += chunk;
});

// When a message ends, log it out
req.on('end', function(){
  console.log(str);
});

// End the request
req.end();

HTTP/2 enables full request and response multiplexing. In practice, this means a connection made to a web server from your browser can be used to send multiple requests and receive multiple responses. This gets rid of a lot of the additional time that it takes to establish a new connection for each request.

    /*
     * Example HTTP2 Server
     * Opening a full-duplex (stream) channel on port 6000
     *
     */
    
    // Dependencies
    var http2 = require('http2');
    
    // Init the server
    var server = http2.createServer();
    
    // On a stream, send back hello world html
    server.on('stream', function (stream, headers) {
      stream.respond({
        ':status': 200,
        'content-type': 'text/html',
      });
      stream.end('<html><body><p>Hello World</p></body></html>');
    });
    
    // Listen on 6000
    server.listen(6000);

/*
 * Example HTTP2 client
 * Connects to port 6000 and logs the response
 *
 */

// Dependencies
var http2 = require('http2');

// Create client
var client = http2.connect('http://localhost:6000');

// Create a request
var req = client.request({
  ':path': '/'
});

// When message is received, add the pieces of it together until you reach the end
var str = '';
req.on('data',function(chunk){
  str += chunk;
});

// When a message ends, log it out
req.on('end', function(){
  console.log(str);
});

// End the request
req.end();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文