Mastering HTTP/2 in Node.js: A Hands‑On Guide with Real‑World Examples

This article explains HTTP/2 fundamentals, showcases its adoption by major Chinese sites, and provides a step‑by‑step Node.js tutorial—including installation, SSL setup, server code, and a demo that fires 100 parallel requests—to help developers experience the protocol’s benefits.

Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
Tencent IMWeb Frontend Team
Mastering HTTP/2 in Node.js: A Hands‑On Guide with Real‑World Examples

Purpose

The author, still using HTTP/1.1, decided to spend time exploring HTTP/2 usage in Node.js.

HTTP/2 Basics

Binary protocol.

Multiplexing allows parallel requests over a single connection, removing HTTP/1.x ordering and blocking constraints.

Header compression reduces repeated header transmission.

Server push lets the server pre‑emptively send resources.

Alt‑Svc support enables smarter CDN caching.

Client‑Hints allow browsers to convey device or network needs.

Secure cookie prefixes help ensure cookies aren’t tampered with.

Current Adoption

Major Chinese platforms have already adopted HTTP/2:

Taobao example
Taobao example
JD.com example
JD.com example
Zhihu example
Zhihu example
Company not yet using HTTP/2
Company not yet using HTTP/2

Node.js Implementation

Steps to run HTTP/2 in Node.js:

Install the http2 module: npm i --save http2 Obtain an SSL certificate (the author used a pre‑generated one for testing).

Create the server file. The code reads files via fs, uses a helper plugin, and, unlike HTTP/1.1, imports http2 and calls http2.createSecureServer(options, callback). The options must include the certificate.

Start the project; the browser will render the HTML and load resources.

Key point: the HTML file makes the browser issue 100 fetch requests to demonstrate HTTP/2’s multiplexing.

Project Structure & Environment

Development environment: macOS 10.12.6, Node v8.9.4.

Project structure diagram
Project structure diagram

Result

Rendered page screenshot
Rendered page screenshot

Source Code

GitHub repository: https://github.com/hyy1115/http2-test

Conclusion

Testing confirms that HTTP/2 uses a single TCP connection per domain, compresses headers, and supports server push (not tested here). To try it yourself, download the source, install the plugin, and ensure your environment supports HTTP/2. For deeper knowledge, search Zhihu for related articles.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Node.jsTutorialHTTP/2Server Push
Tencent IMWeb Frontend Team
Written by

Tencent IMWeb Frontend Team

IMWeb Frontend Community gathering frontend development enthusiasts. Follow us for refined live courses by top experts, cutting‑edge technical posts, and to sharpen your frontend skills.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.