get Current Location's Air Information Using API
- get Current Location's Air Information Using API - install axios - allow the CORS error to Backend app
Showing
7 changed files
with
48 additions
and
22 deletions
| 1 | var createError = require("http-errors"); | 1 | var createError = require("http-errors"); |
| 2 | var express = require("express"); | 2 | var express = require("express"); |
| 3 | +var cors = require("cors"); | ||
| 3 | var path = require("path"); | 4 | var path = require("path"); |
| 4 | var cookieParser = require("cookie-parser"); | 5 | var cookieParser = require("cookie-parser"); |
| 5 | var logger = require("morgan"); | 6 | var logger = require("morgan"); |
| ... | @@ -9,6 +10,7 @@ var usersRouter = require("./routes/users"); | ... | @@ -9,6 +10,7 @@ var usersRouter = require("./routes/users"); |
| 9 | var airConditionRouter = require("./routes/airCondition"); | 10 | var airConditionRouter = require("./routes/airCondition"); |
| 10 | 11 | ||
| 11 | var app = express(); | 12 | var app = express(); |
| 13 | +app.use(cors()); | ||
| 12 | 14 | ||
| 13 | // view engine setup | 15 | // view engine setup |
| 14 | app.set("views", path.join(__dirname, "views")); | 16 | app.set("views", path.join(__dirname, "views")); | ... | ... |
| ... | @@ -4,16 +4,16 @@ | ... | @@ -4,16 +4,16 @@ |
| 4 | * Module dependencies. | 4 | * Module dependencies. |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| 7 | -var app = require('../app'); | 7 | +var app = require("../app"); |
| 8 | -var debug = require('debug')('backend:server'); | 8 | +var debug = require("debug")("backend:server"); |
| 9 | -var http = require('http'); | 9 | +var http = require("http"); |
| 10 | 10 | ||
| 11 | /** | 11 | /** |
| 12 | * Get port from environment and store in Express. | 12 | * Get port from environment and store in Express. |
| 13 | */ | 13 | */ |
| 14 | 14 | ||
| 15 | -var port = normalizePort(process.env.PORT || '3000'); | 15 | +var port = normalizePort(process.env.PORT || "3001"); |
| 16 | -app.set('port', port); | 16 | +app.set("port", port); |
| 17 | 17 | ||
| 18 | /** | 18 | /** |
| 19 | * Create HTTP server. | 19 | * Create HTTP server. |
| ... | @@ -26,8 +26,8 @@ var server = http.createServer(app); | ... | @@ -26,8 +26,8 @@ var server = http.createServer(app); |
| 26 | */ | 26 | */ |
| 27 | 27 | ||
| 28 | server.listen(port); | 28 | server.listen(port); |
| 29 | -server.on('error', onError); | 29 | +server.on("error", onError); |
| 30 | -server.on('listening', onListening); | 30 | +server.on("listening", onListening); |
| 31 | 31 | ||
| 32 | /** | 32 | /** |
| 33 | * Normalize a port into a number, string, or false. | 33 | * Normalize a port into a number, string, or false. |
| ... | @@ -54,22 +54,20 @@ function normalizePort(val) { | ... | @@ -54,22 +54,20 @@ function normalizePort(val) { |
| 54 | */ | 54 | */ |
| 55 | 55 | ||
| 56 | function onError(error) { | 56 | function onError(error) { |
| 57 | - if (error.syscall !== 'listen') { | 57 | + if (error.syscall !== "listen") { |
| 58 | throw error; | 58 | throw error; |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | - var bind = typeof port === 'string' | 61 | + var bind = typeof port === "string" ? "Pipe " + port : "Port " + port; |
| 62 | - ? 'Pipe ' + port | ||
| 63 | - : 'Port ' + port; | ||
| 64 | 62 | ||
| 65 | // handle specific listen errors with friendly messages | 63 | // handle specific listen errors with friendly messages |
| 66 | switch (error.code) { | 64 | switch (error.code) { |
| 67 | - case 'EACCES': | 65 | + case "EACCES": |
| 68 | - console.error(bind + ' requires elevated privileges'); | 66 | + console.error(bind + " requires elevated privileges"); |
| 69 | process.exit(1); | 67 | process.exit(1); |
| 70 | break; | 68 | break; |
| 71 | - case 'EADDRINUSE': | 69 | + case "EADDRINUSE": |
| 72 | - console.error(bind + ' is already in use'); | 70 | + console.error(bind + " is already in use"); |
| 73 | process.exit(1); | 71 | process.exit(1); |
| 74 | break; | 72 | break; |
| 75 | default: | 73 | default: |
| ... | @@ -83,8 +81,6 @@ function onError(error) { | ... | @@ -83,8 +81,6 @@ function onError(error) { |
| 83 | 81 | ||
| 84 | function onListening() { | 82 | function onListening() { |
| 85 | var addr = server.address(); | 83 | var addr = server.address(); |
| 86 | - var bind = typeof addr === 'string' | 84 | + var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port; |
| 87 | - ? 'pipe ' + addr | 85 | + debug("Listening on " + bind); |
| 88 | - : 'port ' + addr.port; | ||
| 89 | - debug('Listening on ' + bind); | ||
| 90 | } | 86 | } | ... | ... |
| ... | @@ -228,6 +228,15 @@ | ... | @@ -228,6 +228,15 @@ |
| 228 | "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.11.tgz", | 228 | "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.6.11.tgz", |
| 229 | "integrity": "sha512-5wjnpaT/3dV+XB4borEsnAYQchn00XSgTAWKDkEqv+K8KevjbzmofK6hfJ9TZIlpj2N0xQpazy7PiRQiWHqzWg==" | 229 | "integrity": "sha512-5wjnpaT/3dV+XB4borEsnAYQchn00XSgTAWKDkEqv+K8KevjbzmofK6hfJ9TZIlpj2N0xQpazy7PiRQiWHqzWg==" |
| 230 | }, | 230 | }, |
| 231 | + "cors": { | ||
| 232 | + "version": "2.8.5", | ||
| 233 | + "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", | ||
| 234 | + "integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==", | ||
| 235 | + "requires": { | ||
| 236 | + "object-assign": "^4", | ||
| 237 | + "vary": "^1" | ||
| 238 | + } | ||
| 239 | + }, | ||
| 231 | "debug": { | 240 | "debug": { |
| 232 | "version": "2.6.9", | 241 | "version": "2.6.9", |
| 233 | "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", | 242 | "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", | ... | ... |
| ... | @@ -8,6 +8,7 @@ | ... | @@ -8,6 +8,7 @@ |
| 8 | "dependencies": { | 8 | "dependencies": { |
| 9 | "axios": "^0.19.2", | 9 | "axios": "^0.19.2", |
| 10 | "cookie-parser": "~1.4.3", | 10 | "cookie-parser": "~1.4.3", |
| 11 | + "cors": "^2.8.5", | ||
| 11 | "debug": "~2.6.9", | 12 | "debug": "~2.6.9", |
| 12 | "express": "~4.16.0", | 13 | "express": "~4.16.0", |
| 13 | "http-errors": "~1.6.2", | 14 | "http-errors": "~1.6.2", | ... | ... |
| ... | @@ -7,7 +7,7 @@ const googleMapKey = require("./secrets.json").googleAPIKey; | ... | @@ -7,7 +7,7 @@ const googleMapKey = require("./secrets.json").googleAPIKey; |
| 7 | 7 | ||
| 8 | axios.create({ | 8 | axios.create({ |
| 9 | // TODO : 웹을 AWS에 올릴때, 해당 baseURL이 달라져야할 수 있음 | 9 | // TODO : 웹을 AWS에 올릴때, 해당 baseURL이 달라져야할 수 있음 |
| 10 | - baseURL: "http://localhost:3000", | 10 | + baseURL: "http://localhost:3001", |
| 11 | responseType: "json", | 11 | responseType: "json", |
| 12 | }); | 12 | }); |
| 13 | 13 | ... | ... |
| ... | @@ -6,7 +6,6 @@ import "./home.css"; | ... | @@ -6,7 +6,6 @@ import "./home.css"; |
| 6 | import { Map, Marker, MarkerClusterer, Polyline } from "react-kakao-maps"; | 6 | import { Map, Marker, MarkerClusterer, Polyline } from "react-kakao-maps"; |
| 7 | import "bootstrap/dist/css/bootstrap.min.css"; | 7 | import "bootstrap/dist/css/bootstrap.min.css"; |
| 8 | import API from "./API"; | 8 | import API from "./API"; |
| 9 | - | ||
| 10 | /* global kakao */ | 9 | /* global kakao */ |
| 11 | 10 | ||
| 12 | export default class Home extends Component { | 11 | export default class Home extends Component { |
| ... | @@ -255,6 +254,25 @@ export default class Home extends Component { | ... | @@ -255,6 +254,25 @@ export default class Home extends Component { |
| 255 | let position = marker.getPosition(); | 254 | let position = marker.getPosition(); |
| 256 | console.log(marker.getPosition()); | 255 | console.log(marker.getPosition()); |
| 257 | console.log(position["Ga"]); | 256 | console.log(position["Ga"]); |
| 257 | + API.get("/airCondition", { | ||
| 258 | + params: { | ||
| 259 | + latitude: position["Ha"], | ||
| 260 | + longitude: position["Ga"], | ||
| 261 | + }, | ||
| 262 | + }) | ||
| 263 | + .then((response) => { | ||
| 264 | + console.log("Success"); | ||
| 265 | + console.log(response.data); | ||
| 266 | + this.setState({ | ||
| 267 | + curAirCondition: response.data, | ||
| 268 | + }); | ||
| 269 | + }) | ||
| 270 | + .catch(function (error) { | ||
| 271 | + console.log(error); | ||
| 272 | + }) | ||
| 273 | + .finally(function () { | ||
| 274 | + // always executed | ||
| 275 | + }); | ||
| 258 | }; | 276 | }; |
| 259 | let setDepart = document.createElement("Button"); | 277 | let setDepart = document.createElement("Button"); |
| 260 | setDepart.innerHTML = "출발지로 설정하기"; | 278 | setDepart.innerHTML = "출발지로 설정하기"; | ... | ... |
-
Please register or login to post a comment