forked from xhackjp1/line-messaging-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openWeatherMap.js
43 lines (39 loc) · 1.19 KB
/
openWeatherMap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const http = require('http');
const location = "Tokyo";
const units = 'metric';
const APIKEY = process.env.WEATHER_API_KEY;
const URL = 'http://api.openweathermap.org/data/2.5/weather?q='+ location +'&units='+ units +'&appid='+ APIKEY;
exports.weather = function (callback) {
// function weather(callback) {
http.get(URL, function(res) {
let body = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function(chunk) {
res = JSON.parse(body);
// console.log(res.weather[0].main);
callback(res.weather[0].main);
});
}).on('error', function(e) {
console.log(e.message);
});
}
exports.weatherWithPlace = function (place, callback) {
const urlPlace = 'http://api.openweathermap.org/data/2.5/weather?q='+ place +'&units='+ units +'&appid='+ APIKEY;
http.get(urlPlace, function(res) {
let body = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function(chunk) {
res = JSON.parse(body);
// console.log(res.weather[0].main);
callback(res.weather[0].main);
});
}).on('error', function(e) {
console.log(e.message);
});
}