권주희

Merge branch 'feature/air_condition' into 'develop'

make get airCondition API

- make get airCondition API according to latitude, longitude

See merge request !2
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
2 2
3 # dependencies 3 # dependencies
4 frontend/node_modules/* 4 frontend/node_modules/*
5 +backend/node_modules/*
5 /.pnp 6 /.pnp
6 .pnp.js 7 .pnp.js
7 8
...@@ -21,3 +22,4 @@ frontend/node_modules/* ...@@ -21,3 +22,4 @@ frontend/node_modules/*
21 npm-debug.log* 22 npm-debug.log*
22 yarn-debug.log* 23 yarn-debug.log*
23 yarn-error.log* 24 yarn-error.log*
25 +secrets.json
...\ No newline at end of file ...\ No newline at end of file
......
1 -var createError = require('http-errors'); 1 +var createError = require("http-errors");
2 -var express = require('express'); 2 +var express = require("express");
3 -var path = require('path'); 3 +var path = require("path");
4 -var cookieParser = require('cookie-parser'); 4 +var cookieParser = require("cookie-parser");
5 -var logger = require('morgan'); 5 +var logger = require("morgan");
6 6
7 -var indexRouter = require('./routes/index'); 7 +var indexRouter = require("./routes/index");
8 -var usersRouter = require('./routes/users'); 8 +var usersRouter = require("./routes/users");
9 +var airConditionRouter = require("./routes/airCondition");
9 10
10 var app = express(); 11 var app = express();
11 12
12 // view engine setup 13 // view engine setup
13 -app.set('views', path.join(__dirname, 'views')); 14 +app.set("views", path.join(__dirname, "views"));
14 -app.set('view engine', 'pug'); 15 +app.set("view engine", "pug");
15 16
16 -app.use(logger('dev')); 17 +app.use(logger("dev"));
17 app.use(express.json()); 18 app.use(express.json());
18 app.use(express.urlencoded({ extended: false })); 19 app.use(express.urlencoded({ extended: false }));
19 app.use(cookieParser()); 20 app.use(cookieParser());
20 -app.use(express.static(path.join(__dirname, 'public'))); 21 +app.use(express.static(path.join(__dirname, "public")));
21 22
22 -app.use('/', indexRouter); 23 +app.use("/", indexRouter);
23 -app.use('/users', usersRouter); 24 +app.use("/users", usersRouter);
25 +app.use("/airCondition", airConditionRouter);
24 26
25 // catch 404 and forward to error handler 27 // catch 404 and forward to error handler
26 -app.use(function(req, res, next) { 28 +app.use(function (req, res, next) {
27 next(createError(404)); 29 next(createError(404));
28 }); 30 });
29 31
30 // error handler 32 // error handler
31 -app.use(function(err, req, res, next) { 33 +app.use(function (err, req, res, next) {
32 // set locals, only providing error in development 34 // set locals, only providing error in development
33 res.locals.message = err.message; 35 res.locals.message = err.message;
34 - res.locals.error = req.app.get('env') === 'development' ? err : {}; 36 + res.locals.error = req.app.get("env") === "development" ? err : {};
35 37
36 // render the error page 38 // render the error page
37 res.status(err.status || 500); 39 res.status(err.status || 500);
38 - res.render('error'); 40 + res.render("error");
39 }); 41 });
40 42
41 module.exports = app; 43 module.exports = app;
......
This diff is collapsed. Click to expand it.
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
6 "start": "node ./bin/www" 6 "start": "node ./bin/www"
7 }, 7 },
8 "dependencies": { 8 "dependencies": {
9 + "axios": "^0.19.2",
9 "cookie-parser": "~1.4.3", 10 "cookie-parser": "~1.4.3",
10 "debug": "~2.6.9", 11 "debug": "~2.6.9",
11 "express": "~4.16.0", 12 "express": "~4.16.0",
......
1 +var express = require("express");
2 +var router = express.Router();
3 +var axios = require("axios");
4 +
5 +const openAPIKey = require("./secrets.json").openAPIKey;
6 +const googleMapKey = require("./secrets.json").googleAPIKey;
7 +
8 +axios.create({
9 + // TODO : 웹을 AWS에 올릴때, 해당 baseURL이 달라져야할 수 있음
10 + baseURL: "http://localhost:3000",
11 + responseType: "json",
12 +});
13 +
14 +/* GET airCondition listing. */
15 +router.get("/", async function (req, res, next) {
16 + console.log("경도:", req.query.latitude);
17 + console.log("경도:", req.query.longitude);
18 + let airCondition = "";
19 + let response = await getPosition(req.query.latitude, req.query.longitude)
20 + .then((encodedStation) => getCondition(encodedStation))
21 + .then((result) => {
22 + airCondition = result;
23 + });
24 +
25 + res.send(airCondition);
26 +});
27 +
28 +const getPosition = (lat, lon) => {
29 + return axios
30 + .get(
31 + "https://maps.googleapis.com/maps/api/geocode/json?latlng=" +
32 + lat +
33 + "," +
34 + lon +
35 + "&location_type=ROOFTOP&result_type=street_address&key=" +
36 + googleMapKey +
37 + "&language=ko"
38 + )
39 + .then(function (response) {
40 + console.log("KEY : ", googleMapKey);
41 + let stationName = "";
42 + for (
43 + let i = 0;
44 + i < response["data"].results[0]["address_components"].length;
45 + i++
46 + ) {
47 + let temp =
48 + response["data"].results[0]["address_components"][i]["long_name"];
49 + if (temp[temp.length - 1] == "구") {
50 + stationName = temp;
51 + break;
52 + }
53 + }
54 + console.log("STATION : ", stationName);
55 + return (encodedStation = encodeURI(stationName));
56 + })
57 + .catch(function (error) {
58 + console.log(error.response);
59 + });
60 +};
61 +
62 +const getCondition = (encodedStation) => {
63 + return axios
64 + .get(
65 + "http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getMsrstnAcctoRltmMesureDnsty?serviceKey=" +
66 + openAPIKey +
67 + "&numOfRows=10&pageNo=1&stationName=" +
68 + encodedStation +
69 + "&dataTerm=DAILY&ver=1.3&_returnType=json"
70 + )
71 + .then(function (response) {
72 + // console.log("RES :: ", response);
73 + result = response["data"]["list"][0];
74 + return result;
75 + })
76 + .catch(function (error) {
77 + console.log(error.response);
78 + });
79 +};
80 +
81 +module.exports = router;
1 -var express = require('express'); 1 +var express = require("express");
2 var router = express.Router(); 2 var router = express.Router();
3 3
4 /* GET home page. */ 4 /* GET home page. */
5 -router.get('/', function(req, res, next) { 5 +router.get("/", function (req, res, next) {
6 - res.render('index', { title: 'Express' }); 6 + res.render("index", { title: "Express" });
7 }); 7 });
8 8
9 module.exports = router; 9 module.exports = router;
......
...@@ -3,3 +3,4 @@ extends layout ...@@ -3,3 +3,4 @@ extends layout
3 block content 3 block content
4 h1= title 4 h1= title
5 p Welcome to #{title} 5 p Welcome to #{title}
6 + p This is HowsTheWeather Backend
......