Last active
August 29, 2015 14:03
-
-
Save dhigginbotham/d77c99aad2f321145c97 to your computer and use it in GitHub Desktop.
simple example of server side requests in node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('./request'), | |
expect = require('expect.js'); | |
describe('Request Library Test Suite', function() { | |
it('should be able to handle a GET request to github', function(done) { | |
var opts = { | |
url: 'https://api.github.com/users/dhigginbotham', | |
headers: { | |
'User-Agent': 'dhigginbotham', | |
'Content-Type': 'application/json; charset=utf-8' | |
} | |
}; | |
request(opts, function(err, resp) { | |
if (err) return done(err); | |
expect(err).to.be(null); | |
return done(); | |
}); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Request Library | |
// ------------------------------------ | |
// @author: [email protected] | |
// @license: MIT | |
// | |
var http = require('http'); | |
var https = require('https'); | |
var url = require('url'); | |
var helpers = require('./helpers'); | |
function onEnd(resp, bytes, fn) { | |
var r = resp || [], | |
resp = (bytes ? new Buffer(bytes) : null), | |
copied = 0; | |
if (r.length) { | |
for (var i=0;i<r.length;++i) { | |
r[i].copy(resp, copied, 0); | |
copied += r[i].length; | |
} | |
} | |
return fn(resp); | |
} | |
function translation(settings, secured) { | |
if (typeof settings == 'boolean' && | |
typeof secured == 'undefined') return {}; | |
var translate = { | |
host: null, | |
path: '', | |
method: 'GET', | |
data: {}, | |
headers: { | |
'Content-type': 'application/json; charset=utf-8' | |
} | |
}; | |
if (secured && (!settings.hasOwnProperty('port'))) { | |
translate.port = 443; | |
} | |
if (Object.keys(settings.headers).length) { | |
helpers.merge(translate.headers, settings.headers); | |
} | |
if (Object.keys(settings.data).length) { | |
helpers.merge(translate.data, settings.data); | |
} | |
translate.host = settings.url.host; | |
translate.path = settings.url.pathname; | |
translate.agent = false; | |
translate.secureOptions = require('constants').SSL_OP_NO_TLSv1_2; | |
if (translate.data) translate.data = JSON.stringify(translate.data); | |
return translate; | |
} | |
// Public request method | |
function request(options, fn) { | |
var req, _req, settings, secured, translate; | |
settings = { | |
url: null, | |
method: 'GET', | |
data: {}, | |
headers: {}, | |
params: {} | |
}; | |
if (options) helpers.merge(settings, options); | |
settings.url = (settings.url ? url.parse(settings.url) : null); | |
// detect if we're using a secure schema | |
secured = settings.url.protocol.indexOf("https") > -1; | |
if (settings.url) { | |
// build translation object | |
translate = translation(settings, secured); | |
// check whether or not to fired secured | |
// or not | |
_req = (secured ? https : http); | |
req = _req.request(translate, function (res) { | |
var bytes = 0, resp = []; | |
res.on('data', function(chunk) { | |
if (chunk) resp.push(chunk); | |
bytes += chunk.length; | |
}); | |
res.on('end', function() { | |
onEnd(resp, bytes, function(resp){ | |
return fn(null, resp); | |
}); | |
}); | |
}).on('error', function(e) { | |
return fn(e, null); | |
}) | |
// write the data if its there | |
if (Object.keys(settings.data).length) { | |
req.write(translate.data); | |
} | |
req.end(); | |
} else { | |
return fn(new Error('You must provide a url to request'), null); | |
} | |
} | |
module.exports = request; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment