Skip to content

Commit 04fac61

Browse files
jshagibfahn
authored andcommitted
doc: improve http2 documentation
Provide section headings for server-side and client side examples. Add error handling and TLS to server-side example, following example of `https`. Add error handling, TLS, more efficient Buffer usage, and header printing to client example. PR-URL: #16366 Fixes: #16345 Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent e6e99eb commit 04fac61

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

doc/api/http2.md

+38-17
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,25 @@ support for HTTP/2 protocol features. It is specifically *not* designed for
1616
compatibility with the existing [HTTP/1][] module API. However,
1717
the [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+
1925
The following illustrates a simple, plain-text HTTP/2 server using the
2026
Core API:
2127

2228
```js
2329
const 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

2839
server.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

4660
The following illustrates an HTTP/2 client:
4761

4862
```js
4963
const 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
5471
const 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 = '';
6279
req.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+
});
6586
req.end();
6687
```
6788

0 commit comments

Comments
 (0)