Taking baby-developer steps

오늘의 Snippet 정리(express.js with node.js) 본문

2021 Web Development Bootcamp/20. APIs

오늘의 Snippet 정리(express.js with node.js)

Surin Lee 2021. 7. 17. 16:45
app.get("/", function(req, res){

  const url = "https://api.openweathermap.org/data/2.5/weather?q=London&appid=abaf6b7affaaf80dc46cf5afd8ac3545";

  https.get(url, function(response){
    console.log(response);

    response.on("data", function(data){

      const weatherData = JSON.parse(data);
      const temp = weatherData.main.temp;
      console.log(temp);
    });

  });

  res.send("Server is up and running");

});

 

app.get 과 https.get의 기능이 전혀 다름에 유의!

 app.get("/", function(req, res{~~}); 는, 사용자(user)가 내 서버에 get require을 할 때, ~~를 하겠다.

https.get(url, function(response){~~~}); 는, url(API)를 통해 내 서버가 다른 사람의 서버에 get reqest를 보낼때, ~~를 하겠다. 

 

+ response.on("data", function(data){~}); 는, 위 API를 통해 다른 사람의 서버에 보낸 get request에 대한 응답(response)이 왔을 때, 그 data에 관하여 ~를 하겠다.

Comments