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

handy-http

v1.0.2

Published

Simple wrapper for native NodeJS http[s].request

Downloads

5,143

Readme

NodeJS handy http[s] client

Handy-http module provides simple and flexible interface for NodeJS native http.request.

Description

This class is very lightweight and handful if you need to make various types of http requests with just a few lines of code. In my projects it lets me to avoid copy-pasting and makes my code more readable and maintainable. Class provides 'open' method that takes a connection object as the first param and a callback function as the second one. It returns the request object that can be listened to all kinds of events from original NodeJS http.ClientRequest. Main features:

  • accepts various types of data to send from plain javascript objects to NodeJS Buffer and ReadableStream.
  • is able to send multiple files with 'multipart/form-data' Content-Type header
  • returns parsed JSON object if server respond with properly Content-Type header
  • supports proxy requests
  • provides ability to extend request headers
  • supports https protocol

Example

var HTTPClient = require('handy-http'),
    client = new HTTPCLient(),
    onResponse = function (err, res) {
        console.log(err || res);
    };

/**
 * The simplest case is the 'GET' request'.
 */
client.open('http://_url_', onResponse);

// It is shortcut for
client.open({ url: 'http://_url_' }, onResponse);

/**
 * You can combine url params in querystring with a hash of additional params
 * It's not nessesary urlencode your params first, it will be done for you.
 */
client.open({
    url: 'http://_url_?one=1&two=2',
    data: { three: 3, more: '4 and 5' }
}, onResponse);
// Resulting URL will look like http://_url_?one=1&two=2&three=3&more=4%20and%205'

/**
 * Your should note 'POST' or any connection method if you want that type of request.
 */
client.open({
    url: 'http://_url_',
    method: 'POST',
    data: { param1: 'value1', param2: 'value2' }
}, onResponse);

/**
 * You can send a file or any binary data within request using NodeJS Buffer as data value.
 * You can also use NodeJS <a href="http://nodejs.org/api/stream.html">Stream-like</a> object, for example <a href="http://nodejs.org/api/fs.html#fs_class_fs_readstream">fs.ReadStream</a>
 * Stream will be sent by chunks with 'Transfer-Encoding: chunked' HTTP-header
 */
var fileStream = fs.createReadStream('_path_to_file_'),
    fileBuffer = fs.readFileSync('_path_to_file_');

client.open({
    url: 'http://_url_',
    method: 'POST',
    data: fileBuffer,
    // data: fileStream // wil be sended with 'Transfer-Encoding: chunked' HTTP-header,
    headers: {
        'content-type': 'application/octet-stream'
    }
}, onResponse);

/**
 * You can send some files and additional data with 'multipart/form-data' Content-Type header.
 */
client.open({
    url: 'http://_url_',
    method: 'POST',
    data: { param1: 'value1', param2: 'value2' },
    files: [
        { fieldname: 'fileField1', name: 'test.png', type: 'image/png', value: file1Buffer },
        { fieldname: 'fileField2', name: 'test.jpg', type: 'image/jpeg', value: file2Buffer }
    ],
    headers {
        'content-type': 'multipart/form-data'
    }
}, onResponse);

/**
 * You can proxy your request.
 */
client.open({
    url: 'http://_url_',
    method: 'POST',
    data: { param1: 'value1', param2: 'value2' },
    proxy: {
        host: '_proxy_host_',
        port: '_proxy_port_'
    }
}, onResponse);

/**
 * If you need a lower level of manipulation you can still listen to all events provided by native NodeJS <a href="http://nodejs.org/api/http.html#http_class_http_clientrequest">http.ClientRequest</a>.
 */
var request = client.open(...);

request.on('socket', function (socket) {
    ...
});

// You can close connection if you need it.
request.close();