Giter Club home page Giter Club logo

node-scoped-http-client's Introduction

Scoped HTTP Client for Node.js

Node.js's HTTP client is great, but a little too low level for common purposes. It's common practice for some libraries to extract this out so it's a bit nicer to work with.

function(method, path, customHeaders, body, callback) {
  var client = http.createClient(url)
  client.request(method, path, headers)
  // ...
}

I hate functions with lots of optional arguments. Let's turn that into:

var scopedClient = require('./lib')
  , util         = require('util')

var client = scopedClient.create('https://api.github.com')
  .header('accept', 'application/json')
  .path('user/show/technoweenie')
  .get()(function(err, resp, body) {
    util.puts(body)
  })

You can scope a client to make requests with certain parameters without affecting the main client instance:

client.path('https://api.github.com') // reset path
client.scope('users/technoweenie', function(cli) {
  // cli's path is "https://api.github.com/users/technoweenie"
  cli.get()(function(err, resp, body) {
    util.puts(body)
  })
})

// client's path is back to just "https://api.github.com"

You can use .post(), .put(), .del(), and .head().

client.query({login:'technoweenie',token:'...'})
  .scope('users/technoweenie', function(cli) {
    var data = JSON.stringify({location: 'SF'})

    // posting data!
    cli.post(data)(function(err, resp, body) {
      util.puts(body)
    })
  })

Sometimes you want to stream the request body to the server. The request is a standard http.clientRequest.

client.post(function (req) {
  req.write("...")
  req.write("...")
})(function(err, resp, body) {
  // ...
})

And other times, you want to stream the response from the server. Simply listen for the request's response event yourself and omit the response callback.

client.get(function (err, req) {
  // do your own thing
  req.addListener('response', function (resp) {
    resp.addListener('data', function (chunk) {
      util.puts("CHUNK: " + chunk)
    })
  })
})()

Basic HTTP authentication is supported:

client.get(function (err, req) {
  // we'll keep this conversation secret...
  req.auth('technoweenie', '...')
})

Adding simple timeout support:

client = ScopedClient.create('http://10.255.255.1:9999');

client.timeout(100);

client.get()(function(err, resp, body) {
  if (err) {
    util.puts("ERROR: " + err);
   }
});

Development

Run this in the main directory to compile coffeescript to javascript as you go:

$ coffee -wc -o lib --no-wrap src/**/*.coffee

Copyright

Copyright (c) 2014 rick. See LICENSE for details.

node-scoped-http-client's People

Contributors

atmos avatar bltavares avatar ejfinneran avatar elecnix avatar iangreenleaf avatar jacobk avatar kamarcum avatar mdarveau avatar michaelansel avatar technicalpickles avatar technoweenie avatar tmm1 avatar zzzaaa avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.