Weathermap.org api 사용법

Calvin Park·2022년 6월 5일
0
post-thumbnail
const express = require("express");
const https = require("https");


const app = express();

app.get("/", (req, res)=>{

  const url = "https://api.openweathermap.org/data/2.5/weather?q=Seoul&units=metric&appid=yourAPIKEY";

	//data를 가져오기
  https.get(url, function(response){
	//data답을 하고
    response.on("data", (data)=>{
		//JSON.parse를 사용홰서  JSON 데이터를 Javascript Objects로 변환.
        
      const weatherData = JSON.parse(data);

      console.log(weatherData);

      const temp1 = weatherData['main']['temp'];
      const temp2 = weatherData.main.temp

      const feels_like = weatherData.main.feels_like;
      
      const desc = weatherData.weather[0].description;

      console.log(temp1);
      console.log(temp2);
      console.log(feels_like);
      console.log(desc);
      
    })
  })

  res.send("Server is running.");
})




app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

profile
Personal Velog Note

0개의 댓글