Last active
August 27, 2017 22:29
-
-
Save armandofox/1a797f3ba58af2bfa9dbe8d3ac60f32c to your computer and use it in GitHub Desktop.
HTTP API GET in node.js
This file contains 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
http.get(endpoint + queryString, function (res) { | |
var noaaResponseString = ''; | |
if (res.statusCode != 200) { | |
tideResponseCallback(new Error("Non 200 Response")); | |
} | |
res.on('data', function (data) { | |
noaaResponseString += data; | |
}); | |
res.on('end', function () { | |
var noaaResponseObject = JSON.parse(noaaResponseString); | |
if (noaaResponseObject.error) { | |
console.log("NOAA error: " + noaaResponseObj.error.message); | |
tideResponseCallback(new Error(noaaResponseObj.error.message)); | |
} else { | |
var highTide = findHighTide(noaaResponseObject); | |
tideResponseCallback(null, highTide); | |
} | |
}); | |
}).on('error', function (e) { | |
console.log("Communications error: " + e.message); | |
tideResponseCallback(new Error(e.message)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment