Skip to content

Improve HTTP2 documentation. #16366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions doc/api/http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,34 +45,45 @@ server.on('stream', (stream, headers) => {
stream.end('<h1>Hello World</h1>');
});

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();
```

Expand Down