Last active
February 24, 2016 15:55
-
-
Save robwilkerson/00481a56a730015beaae to your computer and use it in GitHub Desktop.
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
/* 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