diff --git a/doc/api/http2.md b/doc/api/http2.md index c85de0092a72a2..5a0859648f0bc5 100644 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -16,14 +16,25 @@ support for HTTP/2 protocol features. It is specifically *not* designed for compatibility with the existing [HTTP/1][] module API. However, the [Compatibility API][] is. +The `http2` Core API is much more symmetric between client and server than the +`http` API. For instance, most events, like `error` and `socketError`, can be +emitted either by client-side code or server-side code. + +### Server-side example + The following illustrates a simple, plain-text HTTP/2 server using the Core API: ```js const http2 = require('http2'); +const fs = require('fs'); -// Create a plain-text HTTP/2 server -const server = http2.createServer(); +const server = http2.createSecureServer({ + key: fs.readFileSync("localhost-privkey.pem"), + cert: fs.readFileSync("localhost-cert.pem") +}); +server.on('error', (err) => console.error(err)); +server.on('socketError', (err) => console.error(err)); server.on('stream', (stream, headers) => { // stream is a Duplex @@ -34,34 +45,45 @@ server.on('stream', (stream, headers) => { stream.end('

Hello World

'); }); -server.listen(80); +server.listen(8443); +``` + +To generate the certificate and key for this example, run: + +```bash +openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \ + -keyout localhost-privkey.pem -out localhost-cert.pem ``` -Note that the above example is an HTTP/2 server that does not support SSL. -This is significant as most browsers support HTTP/2 only with SSL. -To make the above server be able to serve content to browsers, -replace `http2.createServer()` with -`http2.createSecureServer({key: /* your SSL key */, cert: /* your SSL cert */})`. +### Client-side example The following illustrates an HTTP/2 client: ```js const http2 = require('http2'); +const fs = require('fs'); +const client = http2.connect('https://localhost:8443', { + ca: fs.readFileSync("localhost-cert.pem") +}); +client.on('socketError', (err) => console.error(err)) +client.on('error', (err) => console.error(err)) -const client = http2.connect('http://localhost:80'); - -// req is a Duplex const req = client.request({ ':path': '/' }); -req.on('response', (headers) => { - console.log(headers[':status']); - console.log(headers['date']); +req.on('response', (headers, flags) => { + for (const name in headers) { + console.log(name + ": " + headers[name]); + } }); -let data = ''; req.setEncoding('utf8'); -req.on('data', (d) => data += d); -req.on('end', () => client.destroy()); +let rawData = ''; +req.on('data', (chunk) => { rawData += chunk; }); +req.on('end', () => { + console.log(); + console.log(rawData); + client.destroy() +}); req.end(); ```