当前位置: 首页 > news >正文

Node.js 中http 和 http/2 是两个不同模块对比

1. 核心模块对比

特性http 模块 (HTTP/1.1)http2 模块 (HTTP/2)
协议版本HTTP/1.1(文本协议)HTTP/2(二进制协议)
多路复用不支持(需多个 TCP 连接)支持(单连接多流)
头部压缩HPACK 压缩算法
服务器推送不支持支持
TLS 依赖可选(但生产环境建议启用)强制要求 TLS(可通过 allowHTTP1 降级)
Node.js 版本所有版本8.4.0+(实验性),10.0.0+(稳定)

2. 使用场景

  • http 模块

    • 传统 HTTP/1.1 服务
    • 简单请求/响应模型
    • 需要兼容旧客户端或代理
    • 无需 HTTP/2 高级特性
  • http2 模块

    • 高并发场景(如 API 服务、实时应用)
    • 需要减少延迟(多路复用)
    • 传输大量重复头部(如 Cookies)
    • 服务器推送资源(如提前发送 CSS/JS)

3. 代码示例

HTTP/1.1 服务器
const http = require('http');const server = http.createServer((req, res) => {res.writeHead(200, { 'Content-Type': 'text/plain' });res.end('Hello HTTP/1.1!');
});server.listen(3000, () => {console.log('HTTP/1.1 server on port 3000');
});
HTTP/2 服务器(需 TLS)
const http2 = require('http2');
const fs = require('fs');const server = http2.createSecureServer({key: fs.readFileSync('localhost-privkey.pem'),cert: fs.readFileSync('localhost-cert.pem'),allowHTTP1: true // 允许 HTTP/1.1 降级
});server.on('stream', (stream, headers) => {stream.respond({'content-type': 'text/html',':status': 200});stream.end('<h1>Hello HTTP/2!</h1>');
});server.listen(3001, () => {console.log('HTTP/2 server on port 3001');
});

4. 关键注意事项

  1. 证书要求

    • HTTP/2 默认需要 TLS,可通过 insecure 选项禁用(仅限开发环境):
      const server = http2.createServer({ insecure: true });
      
  2. 客户端兼容性

    • 使用 http2.connect() 连接 HTTP/2 服务器:
      const client = http2.connect('https://localhost:3001');
      const req = client.request({ ':path': '/' });
      req.on('response', (headers) => {// 处理响应
      });
      
  3. 性能优化

    • 启用 settings 配置优化流控:
      server.on('session', (session) => {session.settings({enablePush: true,initialWindowSize: 65535});
      });
      

5. 迁移建议

  1. 渐进式迁移

    • 使用 allowHTTP1: true 让服务器同时支持 HTTP/1.1 和 HTTP/2。
    • 通过 ALPN 协议自动协商版本。
  2. 工具链支持

    • 使用 curl --http2 或 Postman 测试 HTTP/2。
    • 监控工具:Wireshark 或 nghttp2 -v

官方文档

  • Node.js HTTP 模块
  • Node.js HTTP/2 模块

如果需要更具体的场景实现(如双向流、动态推送),请提供详细需求!

http://www.xdnf.cn/news/1126459.html

相关文章:

  • React源码4 三大核心模块之一:Schedule,scheduleUpdateOnFiber函数
  • GBase 8a 与 Spring Boot + MyBatis 整合实战:从环境搭建到CRUD操作
  • Springboot集成SpringSecurity的介绍及使用
  • 【实时Linux实战系列】使用系统调用实现实时同步
  • 【PTA数据结构 | C语言版】前序遍历二叉树
  • 2025国自然青基、面上资助率,或创新低!
  • 板凳-------Mysql cookbook学习 (十一--------11)
  • C#,List<T> 与 Vector<T>
  • 焊接机器人智能节气阀
  • 关于list
  • 微信小程序入门实例_____从零开始 开发一个每天记账的微信小程序
  • 【GPIO】从STM32F103入门GPIO寄存器
  • 153.在 Vue 3 中使用 OpenLayers + Cesium 实现 2D/3D 地图切换效果
  • 淘宝扭蛋机小程序开发:重构电商娱乐化体验的新范式
  • Kruskal重构树
  • Linux操作系统从入门到实战(九)Linux开发工具(中)自动化构建-make/Makefile知识讲解
  • 12.6 Google黑科技GShard:6000亿参数MoE模型如何突破显存限制?
  • 导出内存溢出案例分析
  • 学习秒杀系统-实现秒杀功能(商品列表,商品详情,基本秒杀功能实现,订单详情)
  • JavaScript认识+JQuery的依赖引用
  • ethers.js-8-bigNmber和callstatic模拟
  • 2025年最新香港站群服务器租用价格参考
  • 探索阿里云ESA:开启边缘安全加速新时代
  • 基于Ruoyi和PostgreSQL的统一POI分类后台管理实战
  • 论文阅读:arxiv 2025 A Survey on Data Contamination for Large Language Models
  • 从12kW到800V,AI服务器电源架构变革下,功率器件如何解题?
  • redisson 设置了过期时间,会自动续期吗
  • 【网络安全】大型语言模型(LLMs)及其应用的红队演练指南
  • 经典排序算法之希尔排序
  • docker 方式gost代理搭建以及代理链实施