Translator: Prepress
The original link
A minute ago I opened initial pull-Request, which will provide an HTTP/2 implementation for Node.js core. While far from ready to hit the production line, it marks a key milestone.
Since this is just a pull request, you can play with it, but there are some extra steps.
First, you need to make sure you set up for building local Node.js according to the instructions, which can be found at github.com/nodejs/node… .
Then, switch to the work branch
$ git clone [https://github.com/jasnell/node](https://github.com/jasnell/node)
$ git checkout initial-pr
Copy the code
Next create…
$ ./configure
$ make -j8
Copy the code
Warning, it takes a while to build Node.js from the web. So, while it’s going on, grab a quick snack.
Once that’s done, you can create a well-functioning HTTP / 2 server with a few lines of code:
const http2 = require('http2');
const server = http2.createServer();
server.on('stream', (stream, requestHeaders) => {
stream.respond({ ':status': 200, 'content-type': 'text/plain' });
stream.write('hello ');
stream.end('world');
});
server.listen(8000);
Copy the code
Since HTTP / 2 support is still experimental, in order to run this server, you must launch the Node.js instance with the –expose-http2 command line argument:
$ node --expose-http2 h2server.js
Copy the code
Note that the server above uses a plain text TCP connection, so the server will not be accessible from a Web browser that requires TLS. However, we can create a simple HTTP / 2 client:
const http2 = require('http2');
const client = http2.connect('http://localhost:8000');
const req = client.request({ ':method': 'GET', ':path': '/' });
req.on('response', (responseHeaders) => {
// do something with the headers
});
req.on('data', (chunk) => {
// do something with the data
});
req.on('end', () => client.destroy());
Copy the code
Setting up an HTTP / 2 server with TLS enabled only requires a few additional steps:
const http2 = require('http2'); const options = { key: getKeySomehow(), cert: getCertSomehow() }; const server = http2.createSecureServer(options); server.on('stream', (stream, requestHeaders) => { stream.respond(); stream.end('secured hello world! '); }); server.listen(43);Copy the code
Please refer to the node.jstls.createserver () documentation to learn more about configuration options for key and cert.
While there are many details that still need to be worked out and probably many issues that need to be fixed… This initial implementation provides enough functionality to get started, including:
-
Push stream support
-
RespondWithFile () and respondWithFD() apis, allowing very efficient sending of raw file data bypassing the Streams API.
-
TLS and plain text connections
-
Full support for stream reuse
-
HTTP / 2 priority and traffic control
-
Support HTTP / 2 trailers
-
HPACK header compression support
-
Compatibility API layer, as close as possible to the existing HTTP / 1 API
Development will continue, as will security enhancements, performance optimizations and API refinements. The more feedback we get, the better.
I wish you all every kind of happiness 🙂