0
現在の天気にアクセスするためにNode.js
を使用して天気アプリを作成しています。JavaScript内のクロージャー内の変数にアクセスする方法
私はopenweatherappのAPIを呼び出すと、私はmodule.exports
に渡すしようとしていますJSONを使って取得温度変数が閉鎖一連の機能の中にネストされています。
temperature
にアクセスしてmodule.exports
に渡す方法はありますか?他のファイルからデータを取得できるのですか?
var http = require('http')
const apiKey = "myAPIkey"
// Connect to API URL api.openweathermap.org/data/2.5/weather?q={city name}
function accessWeather(city, callback) {
var options = {
host: "api.openweathermap.org",
path: "/data/2.5/weather?q=" + city + "&appid=" + apiKey + "",
method: "GET"
}
var body = ""
var request = http.request(options, function(response) {
response.on('data', function(chunk) {
body += chunk.toString('utf8')
})
response.on('end', function() {
var json = JSON.parse(body)
var temperature = parseInt(json["main"]["temp"] - 273)
})
})
request.end()
}
temp = accessWeather("Calgary")
console.log(temp)
module.exports = {
accessWeather: accessWeather
}