npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jf-http-request

v1.0.1

Published

Simple wrapper for NodeJS HTTP request. Use promises, events or callbacks.

Downloads

19

Readme

jf-http-request stable

npm install jf-http-request

Simple wrapper for NodeJS HTTP request.

Options

Option | Type | Description -------------|--------|-------------- auth | string | Basic authentication i.e. user:password to compute an Authorization header. 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. headers | object | An object containing request headers. 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. localAddress | string | Local interface to bind for network connections. method | string | A string specifying the HTTP request method (default: GET). path | string | Request 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. port | number | Port of remote server (default: 80). protocol | string | Protocol to use (default: http:). socketPath | string | Unix Domain Socket (use one of host:port or socketPath). timeout | number | A 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). requestType | string | Type of result to return ('promise', 'events') or use a function for use the callback system (defaults: events). url | string | A 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');