Home > Web Front-end > JS Tutorial > body text

How to enable HTTP/2.0 in Node.Js?

不言
Release: 2019-03-21 14:20:17
Original
3968 people have browsed it

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.

How to enable HTTP/2.0 in Node.Js?

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
Copy after login

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
Copy after login

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
Copy after login

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);
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template