now
Make a web request to open-notify.org and check where the iss is now.
Run it like this:
cd path/to/steel-ant-input-output/examples-nodejs/iss-api/now
npm install
node index.js
index.js
/**
* call the now api of the iss
* and display the current location
*/
var request = require('request');
var moment = require('moment');
// this is our URL we make the request to
var url = "http://api.open-notify.org/iss-now.json";
// now do that
request(url, function(error, response, body) {
if (!error && response.statusCode == 200) { // error check
var obj = JSON.parse(body);// parse the resulting string to JSON
// convert the unix time to human readable time
var d = moment.unix(obj.timestamp);
// log that to the console
console.log("Position at date: %s", d.toISOString());
console.log("Latitude: %d", obj.iss_position.latitude);
console.log("Longitude: %d", obj.iss_position.longitude);
} else {
console.error(error);
}
});
package.json
{
"name": "now",
"version": "1.0.0",
"description": "getting current location of iss",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"moment": "^2.10.6",
"request": "^2.61.0"
}
}