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.
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:
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.
Result
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
