.. | ||
examples | ||
lib | ||
test/e2e | ||
.travis.yml | ||
http.js | ||
https.js | ||
LICENSE | ||
package.json | ||
README.md |
HTTP2 client
Drop-in replacement for Nodes http and https that transparently make http request to both http1 / http2 server. Currently, it's the only http2/https compatible API for clients.
Motivation
http2 in Node.JS works entirely differently, while in browsers the experience is the same.
http2-client
was created to enable http2 / http1.1 requests with the same interface as http1.1.
The reason is that many NPM modules cannot upgrade to use http2.0 as these are coupled into http1.1 interface.
With http2-client
it should be very straight forward.
Meaning you don't need to know which protocol the destination supports before making the request http2-client
will chose the one that works.
If the Node.js version you are using is not supporting http2 http2-client
will automatically fallback to http.
Features
Transparently supports all http protocol.
- Http/1.1
- Https/1.1
- Http/2.0
In case of http1.1
- Connection pool is managed as usual with an http agent.
In case of http2.0
- Connection pool is managed by Http2 agent.
- Requests to the same "origin" will use the same tcp connection (per request manager) - automatically.
- All Http2 features are available except push.
Usage - Same interface
request()
const {request} = require('http2-client');
const h1Target = 'http://www.example.com/';
const h2Target = 'https://www.example.com/';
const req1 = request(h1Target, (res)=>{
console.log(`
Url : ${h1Target}
Status : ${res.statusCode}
HttpVersion : ${res.httpVersion}
`);
});
req1.end();
const req2 = request(h2Target, (res)=>{
console.log(`
Url : ${h2Target}
Status : ${res.statusCode}
HttpVersion : ${res.httpVersion}
`);
});
req2.end();
get()
const {get} = require('http2-client');
const h1Target = 'http://www.example.com/';
const h2Target = 'https://www.example.com/';
get(h1Target, (res)=>{
console.log(`
Url : ${h1Target}
Status : ${res.statusCode}
HttpVersion : ${res.httpVersion}
`);
});
get(h2Target, (res)=>{
console.log(`
Url : ${h2Target}
Status : ${res.statusCode}
HttpVersion : ${res.httpVersion}
`);
});
API
The module mimics the nodejs http module interface of ClientRequest, get() and request(). Same API as regular http/s modules. Different options will be used depending on the destination this method will get.
- Http/1.1
- Https/1.1
- Http/2.0
HttpRequestManager
By default this module exports a default request method the will try to detect the currect protocol to use (http2/http1.1/https1.1). However, you can always create different request manager with your specfic defaults and seperated cache.
- options
<Object>
- keepH2ConnectionFor
<number>
Time to keep http2 connection after used last time. Default: 1000ms. - keepH1IdentificationCacheFor
<number>
TTL time for identification results of http1.1. Default: 30000ms. - useHttp
<boolean>
Should enforce http socket. - useHttps
<boolean>
Should enforce https socket.
- keepH2ConnectionFor
//Use the default
const {request} = require('http2-client');
//Make a request
const req = request(/*....*/);
req.end();
//Alternatively create a new request
const {HttpRequestManager} = require('http2-client');
const httpRequestManager = new HttpRequestManager();
//Make a request
const req = httpRequestManager.request(/*....*/);
req.end();
Http/1.1 - request(options[, callback]) | request(url [,options][, callback])
- options
<Object> | <string> | <URL>
- protocol
<string>
Protocol to use. Default: 'http:'. - host
<string>
A domain name or IP address of the server to issue the request to. Default: 'localhost'. - hostname
<string>
Alias for host. To support url.parse(), hostname is preferred over host. - family
<number>
IP address family to use when resolving host and hostname. Valid values are 4 or 6.When unspecified, both IP v4 and v6 will be used. - port
<number>
Port of remote server. Default: 80. - localAddress
<string>
Local interface to bind for network connections. - socketPath
<string>
Unix Domain Socket (use one of host:port or socketPath). - method
<string>
A string specifying the HTTP request method. Default: 'GET'. - path
<string>
Request path. Should include query string if any. E.G. '/index.html?page=12'. An exception is thrown when the request path contains illegal characters. Currently, only spaces are rejected but that may change in the future. Default: '/'. - headers
- protocol