권주희

make get airCondition API

- make get airCondition API according to latitude, longitude
......@@ -2,6 +2,7 @@
# dependencies
frontend/node_modules/*
backend/node_modules/*
/.pnp
.pnp.js
......@@ -21,3 +22,4 @@ frontend/node_modules/*
npm-debug.log*
yarn-debug.log*
yarn-error.log*
secrets.json
\ No newline at end of file
......
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");
var airConditionRouter = require("./routes/airCondition");
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
app.use(logger('dev'));
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, "public")));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/airCondition", airConditionRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
res.render("error");
});
module.exports = app;
......
This diff is collapsed. Click to expand it.
......@@ -6,6 +6,7 @@
"start": "node ./bin/www"
},
"dependencies": {
"axios": "^0.19.2",
"cookie-parser": "~1.4.3",
"debug": "~2.6.9",
"express": "~4.16.0",
......
var express = require("express");
var router = express.Router();
var axios = require("axios");
const openAPIKey = require("./secrets.json").openAPIKey;
const googleMapKey = require("./secrets.json").googleAPIKey;
axios.create({
// TODO : 웹을 AWS에 올릴때, 해당 baseURL이 달라져야할 수 있음
baseURL: "http://localhost:3000",
responseType: "json",
});
/* GET airCondition listing. */
router.get("/", async function (req, res, next) {
console.log("경도:", req.query.latitude);
console.log("경도:", req.query.longitude);
let airCondition = "";
let response = await getPosition(req.query.latitude, req.query.longitude)
.then((encodedStation) => getCondition(encodedStation))
.then((result) => {
airCondition = result;
});
res.send(airCondition);
});
const getPosition = (lat, lon) => {
return axios
.get(
"https://maps.googleapis.com/maps/api/geocode/json?latlng=" +
lat +
"," +
lon +
"&location_type=ROOFTOP&result_type=street_address&key=" +
googleMapKey +
"&language=ko"
)
.then(function (response) {
console.log("KEY : ", googleMapKey);
let stationName = "";
for (
let i = 0;
i < response["data"].results[0]["address_components"].length;
i++
) {
let temp =
response["data"].results[0]["address_components"][i]["long_name"];
if (temp[temp.length - 1] == "구") {
stationName = temp;
break;
}
}
console.log("STATION : ", stationName);
return (encodedStation = encodeURI(stationName));
})
.catch(function (error) {
console.log(error.response);
});
};
const getCondition = (encodedStation) => {
return axios
.get(
"http://openapi.airkorea.or.kr/openapi/services/rest/ArpltnInforInqireSvc/getMsrstnAcctoRltmMesureDnsty?serviceKey=" +
openAPIKey +
"&numOfRows=10&pageNo=1&stationName=" +
encodedStation +
"&dataTerm=DAILY&ver=1.3&_returnType=json"
)
.then(function (response) {
// console.log("RES :: ", response);
result = response["data"]["list"][0];
return result;
})
.catch(function (error) {
console.log(error.response);
});
};
module.exports = router;
var express = require('express');
var express = require("express");
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
router.get("/", function (req, res, next) {
res.render("index", { title: "Express" });
});
module.exports = router;
......
......@@ -3,3 +3,4 @@ extends layout
block content
h1= title
p Welcome to #{title}
p This is HowsTheWeather Backend
......