1.0.1 • Published 6 years ago

jf-http-request v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

jf-http-request stable

npm install jf-http-request

Simple wrapper for NodeJS HTTP request.

Options

OptionTypeDescription
authstringBasic authentication i.e. user:password to compute an Authorization header.
familynumberIP 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.
headersobjectAn object containing request headers.
hoststringA domain name or IP address of the server to issue the request to (default: localhost).
hostnamestringAlias for host. To support url.parse(), hostname is preferred over host.
localAddressstringLocal interface to bind for network connections.
methodstringA string specifying the HTTP request method (default: GET).
pathstringRequest path (default: /). Should include query string if any: /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.
portnumberPort of remote server (default: 80).
protocolstringProtocol to use (default: http:).
socketPathstringUnix Domain Socket (use one of host:port or socketPath).
timeoutnumberA number specifying the socket timeout in milliseconds. This will set the timeout before the socket is connected.
body*Content to send to server (default: undefined).
requestTypestringType of result to return ('promise', 'events') or use a function for use the callback system (defaults: events).
urlstringA string specifying the URL for request and passed to url.parse.

Response types

There are three types of responses:

  • ok : code >= 200 && code < 300 || code = 304
  • fail : code < 200 || (code >= 300 && code !== 304)
  • error : Any request error (timeout, no host, etc).

Request types:

With parameter requestType you can change value returned (default: promise).

Using callbacks

const jfHttpRequest = require('jf-http-request');
//...
jfHttpRequest(
    {
        url         : 'http://jsonplaceholder.typicode.com/posts/1',
        // Callback: NodeJS way
        requestType : (response, status) => {
            switch (status)
            {
                case 'request-error':
                    console.log('ERROR: %s', error.message);
                    break;
                case 'request-fail':
                    console.log('FAIL : %d', response.statusCode);
                    break;
                case 'request-ok':
                    console.log('OK   : %s', response.body);
                    break;
            }
        }
    }
)

Using events

const jfHttpRequest = require('jf-http-request');

// events: EDP way
jfHttpRequest(
        {
            requestType : 'events',
            url         : 'http://jsonplaceholder.typicode.com/posts/1'
        }
    )
    .on('request-error', error    => console.log('ERROR: %s', error.message))
    .on('request-fail',  response => console.log('FAIL : %d', response.statusCode))
    .on('request-ok',    response => console.log('OK   : %s', response.body));

Using promises

const jfHttpRequest = require('jf-http-request');

async function doRequest(url)
{
    try 
    {
        const response = await jfHttpRequest(
            {
                // Promise: async/await way
                requestType : Promise,
                url
            }
        );
        console.log(response); // ok & fail
    }
    catch (error)
    {
        console.log(error.message); // error
    }
}

doRequest('http://jsonplaceholder.typicode.com/posts/1');