lib/couch-client.js
function request(method, path, body, callback) {
    var stream;
    // Body is optional
    if (typeof body === 'function' && typeof callback === 'undefined') {
      callback = body;
      body = undefined;
    }
    // Return a stream if no callback is specified
    if (!callback) {
      stream = new EventEmitter();
      stream.setEncoding = function () {
        throw new Error("This stream is always utf8");
      };
    }

    function errorHandler(err) {
      if (callback) { callback(err); }
      if (stream) { stream.emit('error', err); }
    }

    var headers = {
      "Host": uri.host
    };
    // add the authorization header if provided and using https
    if (uri.auth) {
      headers["Authorization"] = "Basic " + new Buffer(uri.auth, "ascii").toString("base64");
    }

    if (body) {
      body = JSON.stringify(body);
      headers["Content-Length"] = Buffer.byteLength(body);
      headers["Content-Type"] = "application/json";
    }

    var options = {
      host: uri.hostname,
      method: method,
      path: path,
      port: uri.port,
      headers: headers
    };
    var request = uri.protocolHandler.request(options, function (response) {
      response.setEncoding('utf8');
      var body = "";
      response.on('data', function (chunk) {
        if (callback) { body += chunk; }
        if (stream) { stream.emit('data', chunk); }
      });
      response.on('end', function () {
        if (callback) {
          try {
            var parsedBody = JSON.parse(body);
            callback(null, parsedBody);
          } catch(err) {
            callback(err);
          }
        }
        if (stream) { stream.emit('end'); }
      });
      response.on('error', errorHandler);
    });
    request.on('error', errorHandler);

    if (body) { request.write(body, 'utf8'); }
    request.end();

    return stream;
  }
function request(method, path, body, callback) {
    var stream;
    // Body is optional
    if (typeof body === 'function' && typeof callback === 'undefined') {
      callback = body;
      body = undefined;
    }
    // Return a stream if no callback is specified
    if (!callback) {
      stream = new EventEmitter();
      stream.setEncoding = function () {
        throw new Error("This stream is always utf8");
      };
    }

    function errorHandler(err) {
      if (callback) { callback(err); }
      if (stream) { stream.emit('error', err); }
    }

    var headers = {
      "Host": uri.host
    };
    // add the authorization header if provided and using https
    if (uri.auth) {
      headers["Authorization"] = "Basic " + new Buffer(uri.auth, "ascii").toString("base64");
    }

    if (body) {
      body = JSON.stringify(body);
      headers["Content-Length"] = Buffer.byteLength(body);
      headers["Content-Type"] = "application/json";
    }

    var options = {
      host: uri.hostname,
      method: method,
      path: path,
      port: uri.port,
      headers: headers
    };
    var request = uri.protocolHandler.request(options, function (response) {
      response.setEncoding('utf8');
      var body = "";
      response.on('data', function (chunk) {
        if (callback) { body += chunk; }
        if (stream) { stream.emit('data', chunk); }
      });
      response.on('end', function () {
        if (callback) {
          try {
            var parsedBody = JSON.parse(body);
            callback(null, parsedBody);
          } catch(err) {
            callback(err);
          }
        }
        if (stream) { stream.emit('end'); }
      });
      response.on('error', errorHandler);
    });
    request.on('error', errorHandler);

    if (body) { request.write(body, 'utf8'); }
    request.end();

    return stream;
  }
function request ( method , path , body , callback ) { var stream ; // Body is optional if ( typeof body === 'function' && typeof callback === 'undefined' ) { callback = body ; body = undefined ; } // Return a stream if no callback is specified if ( ! callback ) { stream = new EventEmitter ( ) ; stream . setEncoding = function ( ) { throw new Error ( "This stream is always utf8" ) ; } ; } function errorHandler ( err ) { if ( callback ) { callback ( err ) ; } if ( stream ) { stream . emit ( 'error' , err ) ; } } var headers = { "Host" : uri . host } ; // add the authorization header if provided and using https if ( uri . auth ) { headers [ "Authorization" ] = "Basic " + new Buffer ( uri . auth , "ascii" ) . toString ( "base64" ) ; } if ( body ) { body = JSON . stringify ( body ) ; headers [ "Content-Length" ] = Buffer . byteLength ( body ) ; headers [ "Content-Type" ] = "application/json" ; } var options = { host : uri . hostname , method : method , path : path , port : uri . port , headers : headers } ; var request = uri . protocolHandler . request ( options , function ( response ) { response . setEncoding ( 'utf8' ) ; var body = "" ; response . on ( 'data' , function ( chunk ) { if ( callback ) { body += chunk ; } if ( stream ) { stream . emit ( 'data' , chunk ) ; } } ) ; response . on ( 'end' , function ( ) { if ( callback ) { try { var parsedBody = JSON . parse ( body ) ; callback ( null , parsedBody ) ; } catch ( err ) { callback ( err ) ; } } if ( stream ) { stream . emit ( 'end' ) ; } } ) ; response . on ( 'error' , errorHandler ) ; } ) ; request . on ( 'error' , errorHandler ) ; if ( body ) { request . write ( body , 'utf8' ) ; } request . end ( ) ; return stream ; }
A simple wrapper around node's http(s) request.
A simple wrapper around node s http ( s ) request .