neat-http
v0.0.10
Published
Light-weight Client Promise wrap for raw http.request
Downloads
4
Readme
neat-request
Light-weight Promise wrap for raw http.request
Install
npm install neat-http
Hello neat_http
// Now is mostly used for proxy
const request = require('neat-http');
const server = http.createServer(async function(sreq, sres) {
const url_parts = url.parse(sreq.url);
const opts = {
host: 'google.com',
port: 80,
path: url_parts.pathname,
method: sreq.method,
headers: sreq.headers
};
const ext = {
req: sreq, // pipe client request to server
// timeout(Num): If None, Default is 15sec
// toJSON(bool): Default is False.
};
const cres = await request(opts, ext);
sres.writeHead(cres.statusCode, cres.headers);
cres.pipe(sres); // pipe client to server response
});
server.listen(80, '0.0.0.0');
LBClient
const neat_http = require('../index.js');
const arr = [{
host: 'www.upstream1.com',
port: 8080
},
{
host: 'www.upstream2.com',
port: 8080
},
];
const client = new neat_http.LBClient({
path: '/path'
}, {
rr: arr,
timeout: 5000,
healthCheckOpts: {
path: '/check',
},
healthCheckFn: cres => cres.statusCode == 200,
healthCheckCycle: 3000, //ms, every cycle will check all hosts
});
(async() => {
const res = await client.send({
path: '/test/client'
});
})()
API
neat_http(request[opts [,ext]])
opts
(obj) - Default is{}
, the same as rawhttp.request(options)
's parameter.ext
(obj) - Default is{}
, is extension object.ext.req
(http.ClientRequest): rawhttp.ClientRequest
instance, used for request pipe.ext.timeout
(num): timeout inms
, Default is15 second
, if upstream not response, reject error.ext.toJSON
(bool): Default isfalse
, if set true, will return an Object parsed from response.ext.toText
(bool): Default isfalse
, if set true, will return an String parsed from response.
Class: neat_http.LBClient
new LBClient(opts [,ext])
ext.rr
(Array) - : Default isundefined
.Every element of arr will merge intooptions
, in a round-robin manner. especially when need Load-Balance. (Ifrr
's element have same key withoptions
, it will not merge into, please put common key in options, dynamic for Load-Banlance put inrr
)ext.healthCheckOpts
(obj): Default is{}
, use from healCheck requestoptions
.ext.healthCheckFn
(fn): Default when healthCheck's response's statusCode is200
, will recieve one parameter is rawhttp.ServerResponse
instance(healthCheck's response).ext.healthCheckCycle
(num): Default is5000(ms)
, one cycle will check all upstreams inrr
.ext.timeout
(num) - Defalut is15s
, timeout(ms) between proxy request send and recieve response.
client.send(opts)
The same as default neat-http
(request).