Skip to content

Instantly share code, notes, and snippets.

@robwilkerson
Last active February 24, 2016 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwilkerson/00481a56a730015beaae to your computer and use it in GitHub Desktop.
Save robwilkerson/00481a56a730015beaae to your computer and use it in GitHub Desktop.
/* jshint node:true */
process.binding('http_parser').HTTPParser = require('http-parser-js').HTTPParser;
var http = require('http');
var url = require('url');
var printf = require('util').format;
var Promise = require('bluebird');
var modemRequest = Promise.method(function(modemUrl) {
var urlComponents = url.parse(modemUrl);
return new Promise(function(resolve, reject) {
var options = {
hostname: urlComponents.hostname,
path: '/getdeviceinfo/info.bin',
timeout: 3000
};
var request = http.request(options, function(response) {
// Build the body
var modemData = '';
response.on('data', function(chunk) {
modemData += chunk;
});
// Resolve the promise when the response ends
response.on('end', function() {
response.body = modemData;
resolve(response);
});
});
// Handle errors
request
.on('error', function(error) {
console.log('Problem with request:', error.message);
reject(error);
})
.end();
});
});
var modemUrl = printf('http://%s/getdeviceinfo/info.bin', '192.168.0.1');
modemRequest(modemUrl)
.then(function(response) {
console.log('AND WE\'RE BACK!');
console.log(response.body);
})
.catch(function(err) {
console.log(err);
})
.finally(function() {
console.log('FINALLY...');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment