node-http2 is a node module that provides client and server implementations of the HTTP/2 protocol for nodejs applications. This node API is very similar to the node HTTPS module which extends support for HTTP/2.
Installing Node.Js
You can skip this step if node.js is already installed on your system. If node.js is not available on your system, use the following command to install it.
$ sudo apt-get install python-software-properties python g++ make $ curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash - $ sudo apt-get update $ sudo apt-get install nodejs
Of course, you can also upgrade node.js through NPM.
Install the Node-HTTP2 module
The node-http2 module is available under the default npm library. So just execute the following command to install it for your application.
$ npm install http2
Create node server
Let’s create a sample node server that supports HTTP/2. Start by creating a custom SSL certificate or obtain a valid SSL from an authorized SSL provider.
$ openssl req -x509 -nodes -newkey rsa:2048 -keyout example.com.key -out example.com.crt
Now create the http2-server.js file with the following content.
var fs = require('fs'); var options = { key: fs.readFileSync('./example.com.key'), cert: fs.readFileSync('./example.com.crt') }; require('http2').createServer(options, function(request, response) { response.end('Welcome HTTP/2.0'); console.log("Server listening on: http://localhost:8000"); }).listen(8000);
Start the node server
Let us start the node.js server using the following command. It will start the web server on the system's port 8000.
$ node http2-server.js
and access localhost on port 8000.
This article has ended here. For more other exciting content, you can pay attention to the node.js tutorial video column on the PHP Chinese website!
The above is the detailed content of How to enable HTTP/2.0 in Node.Js?. For more information, please follow other related articles on the PHP Chinese website!