dataController.js 995 Bytes
Newer Older
1
import routes from "../routes";
2
import fetch from "node-fetch";
3
4

export const getDataInput = (req, res) => {
5
  console.log(req.query);
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
  if (req.query.type === "Out") {
    // 외부 데이터 수집기
    const {
      query: { id, type, lat, lng },
    } = req;
    // OpenWeatherAPI로 부터 지역의 날씨 정보획득
    fetch(
      `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lng}&appid=${process.env.OPENWEATHERMAP_API_KEY}`
    )
      .then((response) => response.json())
      .then((json) => {
        const temp = json.main.temp;
        const humi = json.main.humidity;
        const press = json.main.pressure;
        const wind_speed = json.wind.speed;
        console.log(id, type, lat, lng, temp, humi, press, wind_speed);
      });
  } else {
    // 내부 데이터 수집기 동작
    const {
      query: { id, type, temp, humi, lights },
    } = req;
    console.log(id, type, temp, humi, lights);
  }

31
  res.status(200).send("<p>OK</p>");
KangMin An's avatar
KangMin An committed
32
};