Node-http2 is a node module which provides client and server implementation of HTTP/2 protocol for nodejs application. This node API is very similar to node https module with extended support for HTTP/2.
Install Node.Js
You may skip this step if you have already installed node.js on your system. If you don’t have node.js on your system, use following commands 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
Or you can also upgrade Node.js via npm.
Install Node-HTTP2 Module
node-http2 module is available under default npm library. So just execute following command to install it for your application.
$ npm install http2
Create Sample Node Server
Let’s create a sample node server with http/2 support. First create a self signed ssl certificate or get a valid SSL from authorized ssl providers.
$ openssl req -x509 -nodes -newkey rsa:2048 -keyout example.com.key -out example.com.crt
Now create http2-server.js file with following content.
1 2 3 4 5 6 7 8 9 10 | 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 Node Server
Let’s start the node.js server using following command. It will start a web server on port 8000 port on your system.
$ node http2-server.js
and access localhost on port 8000 like below.