<p>์š”์ฒญ์€ " รญ ", " รซ ", " รฅ " ์™€ ๊ฐ™์€ ๋ฌธ์ž๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.</p>

์— ๋งŒ๋“  2018๋…„ 08์›” 24์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: request/request

์š”์•ฝ

์•ˆ๋…•ํ•˜์„ธ์š”, ๋ฉฐ์น  ๋™์•ˆ ๋ฌธ์ œ๋กœ ์”จ๋ฆ„ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. [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 }

๋ชจ๋“  3 ๋Œ“๊ธ€

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 ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰