@@ -16,14 +16,25 @@ support for HTTP/2 protocol features. It is specifically *not* designed for
1616compatibility with the existing [ HTTP/1] [ ] module API. However,
1717the [ Compatibility API] [ ] is.
1818
19+ The ` http2 ` Core API is much more symmetric between client and server than the
20+ ` http ` API. For instance, most events, like ` error ` and ` socketError ` , can be
21+ emitted either by client-side code or server-side code.
22+
23+ ### Server-side example
24+
1925The following illustrates a simple, plain-text HTTP/2 server using the
2026Core API:
2127
2228``` js
2329const http2 = require (' http2' );
30+ const fs = require (' fs' );
2431
25- // Create a plain-text HTTP/2 server
26- const server = http2 .createServer ();
32+ const server = http2 .createSecureServer ({
33+ key: fs .readFileSync (' localhost-privkey.pem' ),
34+ cert: fs .readFileSync (' localhost-cert.pem' )
35+ });
36+ server .on (' error' , (err ) => console .error (err));
37+ server .on (' socketError' , (err ) => console .error (err));
2738
2839server .on (' stream' , (stream , headers ) => {
2940 // stream is a Duplex
@@ -34,34 +45,44 @@ server.on('stream', (stream, headers) => {
3445 stream .end (' <h1>Hello World</h1>' );
3546});
3647
37- server .listen (80 );
48+ server .listen (8443 );
3849```
3950
40- Note that the above example is an HTTP/2 server that does not support SSL.
41- This is significant as most browsers support HTTP/2 only with SSL.
42- To make the above server be able to serve content to browsers,
43- replace ` http2.createServer() ` with
44- ` http2.createSecureServer({key: /* your SSL key */, cert: /* your SSL cert */}) ` .
51+ To generate the certificate and key for this example, run:
52+
53+ ``` bash
54+ openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj ' /CN=localhost' \
55+ -keyout localhost-privkey.pem -out localhost-cert.pem
56+ ```
57+
58+ ### Client-side example
4559
4660The following illustrates an HTTP/2 client:
4761
4862``` js
4963const http2 = require (' http2' );
64+ const fs = require (' fs' );
65+ const client = http2 .connect (' https://localhost:8443' , {
66+ ca: fs .readFileSync (' localhost-cert.pem' )
67+ });
68+ client .on (' socketError' , (err ) => console .error (err));
69+ client .on (' error' , (err ) => console .error (err));
5070
51- const client = http2 .connect (' http://localhost:80' );
52-
53- // req is a Duplex
5471const req = client .request ({ ' :path' : ' /' });
5572
56- req .on (' response' , (headers ) => {
57- console .log (headers[' :status' ]);
58- console .log (headers[' date' ]);
73+ req .on (' response' , (headers , flags ) => {
74+ for (const name in headers) {
75+ console .log (` ${ name} : ${ headers[name]} ` );
76+ }
5977});
6078
61- let data = ' ' ;
6279req .setEncoding (' utf8' );
63- req .on (' data' , (d ) => data += d);
64- req .on (' end' , () => client .destroy ());
80+ let data = ' ' ;
81+ req .on (' data' , (chunk ) => { data += chunk; });
82+ req .on (' end' , () => {
83+ console .log (` \n ${ data} ` );
84+ client .destroy ();
85+ });
6586req .end ();
6687```
6788
0 commit comments