Added high level restful http client
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 9d44e23 · Browse files · 1 file·+47 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+47 added−14 removed
- other:+47 added−14 removed
Provenance
- Commit
f452250- Tree
2e05d3e- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
9d44e23
1 file changed+47 added−14 removed
modified:lib/express/http.js+47 added-14 removed
12345667891011121314151617181920218222324252627282930313233341935363738394041424344452346474849502 unmodified lines53545556343536575859606162636465663839676869707172734243444546747576777879
// Express - HTTP - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
var http = require('http'), events = require('events'), parse = require('url').parse parse = require('url').parse, queryString = require('querystring') /** * Request using the given _method_, _url_ * followed by optional _headers_, and _data_. * * @param {string} method * @param {string} url * @param {hash} headers * @param {hash} data * @param {Promise} promise * @return {Promise} * @api private */ function request(method, url, headers, promise) {function request(method, url, headers, data, promise) { var buf = '', promise = promise || new events.Promise, url = parse(url), path = url.pathname || '/', search = url.search || '', hash = url.hash || '', port = url.port || 80, headers = process.mixin(headers, { host: url.hostname }), client = http.createClient(port, url.hostname) client.addListener('error', function(e){ promise.emitError(new Error("client failed to " + method + " `" + url.href + "'")) }) client.request(method, path, headers).finish(function(response){ if (data) { data = queryString.stringify(data) headers['content-length'] = data.length headers['content-type'] = 'application/x-www-form-urlencoded' } var request = client.request(method, path + search + hash, headers) if (data) request.sendBody(data) request.finish(function(response){ if (response.statusCode < 200 || response.statusCode >= 400) promise.emitError(new Error('request failed with status ' + response.statusCode + ' "' + http.STATUS_CODES[response.statusCode] + '"')) else if (response.statusCode >= 300 && response.statusCode < 400) request(method, response.headers.location, headers, promise) request(method, response.headers.location, headers, data, promise) else { response.setBodyEncoding('utf8') response .addListener('body', function(chunk){ buf += chunk })2 unmodified lines }) return promise}
exports.get = function(url, headers) { return request('GET', url, headers)}/** * Return HTTP Client function for the given _method_, * which optionally may _allowData_ to be passed. * * @param {string} method * @param {bool} allowData * @return {function} * @api private */
exports.post = function(url, headers) { return request('POST', url, headers)function client(method, allowData) { return function(url, headers, data) { if (allowData) data = data || {} return request(method.toUpperCase(), url, headers, data) }}
exports.post('http://google.com') .addCallback(function(content, response){ puts(content) p(response.headers) })// --- Public API
exports.get = exports.view = client('get')exports.post = exports.create = client('post', true)exports.put = exports.update = client('put', true)exports.del = exports.destroy = client('delete', true)