| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e6e99eb commit 04fac61
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,14 +16,25 @@ support for HTTP/2 protocol features. It is specifically *not* designed for | |||
| 16 | 16 | compatibility with the existing [HTTP/1][] module API. However, | |
| 17 | 17 | the [Compatibility API][] is. | |
| 18 | 18 | ||
| 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 | + | ||
| 19 | 25 | The following illustrates a simple, plain-text HTTP/2 server using the | |
| 20 | 26 | Core API: | |
| 21 | 27 | ||
| 22 | 28 | ```js | |
| 23 | 29 | const http2 = require('http2'); | |
| 30 | + const fs = require('fs'); | ||
| 24 | 31 | ||
| 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)); | ||
| 27 | 38 | ||
| 28 | 39 | server.on('stream', (stream, headers) => { | |
| 29 | 40 | // stream is a Duplex | |
@@ -34,34 +45,44 @@ server.on('stream', (stream, headers) => { | |||
| 34 | 45 | stream.end('<h1>Hello World</h1>'); | |
| 35 | 46 | }); | |
| 36 | 47 | ||
| 37 | - server.listen(80); | ||
| 48 | + server.listen(8443); | ||
| 38 | 49 | ``` | |
| 39 | 50 | ||
| 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 | ||
| 45 | 59 | ||
| 46 | 60 | The following illustrates an HTTP/2 client: | |
| 47 | 61 | ||
| 48 | 62 | ```js | |
| 49 | 63 | 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)); | ||
| 50 | 70 | ||
| 51 | - const client = http2.connect('http://localhost:80'); | ||
| 52 | - | ||
| 53 | - // req is a Duplex | ||
| 54 | 71 | const req = client.request({ ':path': '/' }); | |
| 55 | 72 | ||
| 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 | + } | ||
| 59 | 77 | }); | |
| 60 | 78 | ||
| 61 | - let data = ''; | ||
| 62 | 79 | 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 | + }); | ||
| 65 | 86 | req.end(); | |
| 66 | 87 | ``` | |
| 67 | 88 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments