Created
October 14, 2015 12:55
-
-
Save arecvlohe/696c986d02034d13a7a0 to your computer and use it in GitHub Desktop.
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
var http = require('http'), | |
url = 'http://api.openweathermap.org/data/2.5/weather?q=Tampa,FL&units=imperial&appid=bd82977b86bf27fb59a04b61b657fb6f', | |
port = 3000, | |
tampaTemp; | |
function logData(data) { | |
var json = JSON.parse(data); | |
tampaTemp = json.main.temp; | |
console.log(tampaTemp); | |
} | |
function getWeather(callback) { | |
var data = ''; | |
http.get(url, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
data+=chunk; | |
}); | |
res.on('end', function(){ | |
callback(data); | |
console.log('data logged'); | |
}); | |
}); | |
} | |
getWeather(logData); | |
var server = http.createServer(function(req, res) { | |
var temp = tampaTemp.toFixed(1); | |
res.writeHead(200, {'Content-Type':'application/json'}); | |
res.write('{\n\t currentTemp: ' + temp + '\n}', 'utf8'); | |
res.end(); | |
}); | |
server.listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment