Node.js has support for building HTTP2 communication. HTTP2 has better efficiency compared to HTTP1.1 as described in my earlier post. In this example, we will build a client-server program that utilizes the HTTP2 module.
Create server application server.js
.
const http2 = require('http2');
const server = http2.createServer();
server.on('stream', (stream, headers) => {
stream.respond({
status: 200,
'content-type': 'text/html'
});
stream.end('<html><head><title>Hello</title></head><body><p>Hello World</p></body></html>')
});
server.listen(6000);
Create client application client.js
.
const http2 = require('http2');
const client = http2.connect('http://localhost:6000');
const request = client.request({
':path': '/'
});
let str = '';
request.on('data', (chunk)=>{
str+=chunk;
});
request.on('end', ()=>{
console.log(str);
});
request.end();
Run the server then run the client on another terminal.
node server.js
node client.js
Comments
Post a Comment