์๋
ํ์ธ์, ๋ฉฐ์น ๋์ ๋ฌธ์ ๋ก ์จ๋ฆํ๊ณ ์์ต๋๋ค. [openweathermap api](https://openweathermap.org/current)๋ฅผ ์ฌ์ฉํ์ฌ ๋์์ ๋ํ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ค๊ณ ํฉ๋๋ค. ์ผ๋ฐ์ ์ธ ์์ฒญ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
http://api.openweathermap.org/data/2.5/weather?q= CITY &units=metric&appid= APIKEY
(๋ฌด๋ฃ API ํค๋ฅผ ์ฌ์ฉํ์ฌ ๋ธ๋ผ์ฐ์ ์์ ์ด๋ฅผ ์ฌํํ ์ ์์ต๋๋ค)
๊ฒฐ๊ณผ: (JSON)
{"coord":{"lon":-73.61,"lat":45.5},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"stations","main":{"temp":24.65,"pressure":1012,"humidity":38,"temp_min":24,"temp_max":25},"visibility":48279,"wind":{"speed":7.2,"deg":280,"gust":11.3},"clouds":{"all":40},"dt":1535058000,"sys":{"type":1,"id":3829,"message":0.0049,"country":"CA","sunrise":1535018756,"sunset":1535068008},"id":6077243,"name":"Montreal","cod":200}
์์ฒญ ๋ชจ๋๋ก ๋ง๋ ์ด ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
``
function req (url, jsonStatus){
const options = {
uri: url,
json: jsonStatus
}
console.log(`Sent: ${options.uri}`)
rp(options)
.then(repos => {
console.log(`Success: ${repos.sys.country}`) // Country code is returned
})
.catch(err => {
console.log(`Error: ${err}`)
})
}}
const owmUrl = `http://api.openweathermap.org/data/2.5/weather?q=${City}&units=metric&appid=${apiKey}`;
req(owmUrl, true);
์ถ๋ ฅ(์ฝ์)(์ฌ๊ธฐ์ ๋์๋ '๋ชฌํธ๋ฆฌ์ฌ'):
`ฮป ๋
ธ๋ ์ฑ
์ ์ก: http://api.openweathermap.org/data/2.5/weather?q=montreal&units=metric&appid= APIKEY
์ฑ๊ณต: CA`
์ฌ๊ธฐ์์ ๋ณผ ์ ์๋ฏ์ด ์์ฒญ์ด ์ฑ๊ณตํ์ต๋๋ค. ๋ธ๋ผ์ฐ์ ์์ "Chamberรญ"์ ๊ฐ์ ํน์ ๋ฌธ์๊ฐ ์๋ ๋์๋ก ์๋ํ๋ฉด ๋ชจ๋ ๊ฒ์ด ์ ๋๊ณ URL์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
http://api.openweathermap.org/data/2.5/weather?q=Chamberรญ&units=metric&appid= APIKEY
๊ฒฐ๊ณผ๋ (JSON ๋ค์):
{"coord":{"lon":-3.71,"lat":40.44},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":27.24,"pressure":1020,"humidity":32,"temp_min":27,"temp_max":28},"visibility":10000,"wind":{"speed":3.6,"deg":110},"clouds":{"all":0},"dt":1535059800,"sys":{"type":1,"id":5488,"message":0.0039,"country":"ES","sunrise":1535002448,"sunset":1535050778},"id":3117735,"name":"Chamberรญ","cod":200}
๊ทธ๋ฌ๋ ๋ด๊ฐ ๋ง๋ ์ฝ๋์ ํจ๊ป ์ด๊ฒ์ ๋ณด๋ด๋ฉด ๋ค์๊ณผ ๊ฐ์ด ๋๋ฉ๋๋ค.
ฮป node app
Test
Sent: http://api.openweathermap.org/data/2.5/weather?q=Chamberรญ&units=metric&appid=0ec25df3f69726843a58a325b97e845d
Error: StatusCodeError: 404 - {"cod":"404","message":"city not found"}
๋ด ๋ธ๋ผ์ฐ์ ์์ ๋์ผํ URL์ ์๋ํ๋ฉด ์๋ฒฝํ๊ฒ ์๋ํ๊ธฐ ๋๋ฌธ์ ์ด ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ์ด์ ๋ฅผ ์ดํดํ ์ ์์ต๋๋ค. request-promise ๋ฐ request-promise-any ๋ชจ๋์ ์ฌ์ฉํด ๋ณด์์ง๋ง ๋ ๋ค ๋์ผํ ์ค๋ฅ๋ฅผ ๋ฐํํฉ๋๋ค.
์ด๊ฒ์ ๋ญ๊ฐ ์๋ชป๋ ๊ฒ์ด๊ณ ๊ทธ๊ฒ์ด ๋ชจ๋์ ์ ์ธ์ง ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ๋์์ ๋ฐ์ ์ ์๋ค๋ฉด ๋งค์ฐ ๊ฐ์ฌํ๊ฒ ์ต๋๋ค. :)
๊ฐ์ฌํฉ๋๋ค.
(์ ๋ฐํ ์์ฒญ์ ๋ฌธ์ ๊ฐ ์์๋ค๋ฉด ์ฃ์กํฉ๋๋ค. ์ฒ์ ํด๋ณด๋ ๊ฒ์ ๋๋ค :) )
| ์ํํธ์จ์ด | ๋ฒ์
| ---------------- | -------
| ์์ฒญ | ์์ฒญ@2.87.0
| ๋
ธ๋ | v10.9.0
| npm | 6.2.0
| ์ด์ ์ฒด์ | ์๋์ฐ 8.1 x64
| ๋ธ๋ผ์ฐ์ | ์คํ๋ผ
URL ๋งค๊ฐ๋ณ์๋ฅผ ์ธ์ฝ๋ฉํ๋ ๋ฐฉ๋ฒ์ ๋ฌด์์ ๋๊น?
ASCII๊ฐ ์๋ ๋ฌธ์๊ฐ ํฌํจ๋ ์ ์๋ ๊ฒฝ์ฐ ๋์ ์ด๋ฆ์ ์ธ์ฝ๋ฉํฉ๋๋ค.
console.log(encodeURIComponent('Montreal'));
console.log(encodeURIComponent('Montrรฉal'));
console.log(encodeURIComponent('Chamberรญ'));
์ฐ์ถ:
Montr%C3%A9al
Chamber%C3%AD
์์ฒญํ๊ธฐ ์ ์ ์ด๋ฆ์ ์ธ์ฝ๋ฉํ ํ Chamberรญ์ ๋ํ ๋ฐ์ดํฐ:
{ coord: { lon: -3.71, lat: 40.44 },
weather:
[ { id: 800, main: 'Clear', description: 'clear sky', icon: '01n' } ],
base: 'stations',
main:
{ temp: 21.98,
pressure: 1020,
humidity: 49,
temp_min: 21,
temp_max: 23 },
visibility: 10000,
wind: { speed: 1.5, deg: 40 },
clouds: { all: 0 },
dt: 1535068800,
sys:
{ type: 1,
id: 5488,
message: 0.0038,
country: 'ES',
sunrise: 1535088854,
sunset: 1535137170 },
id: 3117735,
name: 'Chamberรญ',
cod: 200 }
๊ณ ๋ง์ต๋๋ค !!
:+1:
@davejagoda ๊ฐ์ฌํฉ๋๋ค
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
URL ๋งค๊ฐ๋ณ์๋ฅผ ์ธ์ฝ๋ฉํ๋ ๋ฐฉ๋ฒ์ ๋ฌด์์ ๋๊น?
ASCII๊ฐ ์๋ ๋ฌธ์๊ฐ ํฌํจ๋ ์ ์๋ ๊ฒฝ์ฐ ๋์ ์ด๋ฆ์ ์ธ์ฝ๋ฉํฉ๋๋ค.
์ฐ์ถ:
์์ฒญํ๊ธฐ ์ ์ ์ด๋ฆ์ ์ธ์ฝ๋ฉํ ํ Chamberรญ์ ๋ํ ๋ฐ์ดํฐ: