Commit caff31ae authored by KangMin An's avatar KangMin An
Browse files

Local Code 제공을 위한 코드 추가 및 수정.

parent 81e95b46
...@@ -10,6 +10,7 @@ import globalRouter from "./routers/globalRouter"; ...@@ -10,6 +10,7 @@ import globalRouter from "./routers/globalRouter";
import dataRouter from "./routers/dataRouter"; import dataRouter from "./routers/dataRouter";
import { localmiddleware } from "./middlewares"; import { localmiddleware } from "./middlewares";
import locCodeRouter from "./routers/locCodeRouter";
const app = express(); const app = express();
...@@ -28,5 +29,6 @@ app.use(localmiddleware); ...@@ -28,5 +29,6 @@ app.use(localmiddleware);
// router 사용 // router 사용
app.use(routes.home, globalRouter); app.use(routes.home, globalRouter);
app.use(routes.data, dataRouter); app.use(routes.data, dataRouter);
app.use(routes.locCode, locCodeRouter);
export default app; export default app;
import db from "../db";
const DB_QUERY_ERROR_MSG = "DB Query Error.";
const DB_CONNECTION_ERROR_MSG = "DB Connection Error.";
const SERVER_ERROR_MSG = "The server encountered an error.";
const QUERY_SUCCESS_MSG = "Query Success.";
const STATUS_OK_CODE = 200;
const STATUS_SERVER_ERROR_CODE = 500;
export const getDo = async (req, res) => {
const query = "SELECT CODE, DONAME FROM LOCDO";
db((connErr, connection) => {
if (connErr) {
console.log(DB_CONNECTION_ERROR_MSG);
res.status(STATUS_SERVER_ERROR_CODE).json({ error: SERVER_ERROR_MSG });
} else {
connection.query(query, (queryErr, result) => {
if (queryErr) {
console.log(DB_QUERY_ERROR_MSG);
res
.status(STATUS_SERVER_ERROR_CODE)
.json({ error: SERVER_ERROR_MSG });
} else {
console.log(QUERY_SUCCESS_MSG);
res.status(STATUS_OK_CODE).json({ info: result });
}
});
connection.release();
}
});
};
export const getSGG = (req, res) => {
const {
params: { id },
} = req;
const query = `SELECT CODE, SGGNAME FROM LOCSIGUNGU WHERE DOCODE = ${id}`;
db((connErr, connection) => {
if (connErr) {
console.log(DB_CONNECTION_ERROR_MSG);
res.status(STATUS_SERVER_ERROR_CODE).json({ error: SERVER_ERROR_MSG });
} else {
connection.query(query, (queryErr, result) => {
if (queryErr) {
console.log(DB_QUERY_ERROR_MSG);
res
.status(STATUS_SERVER_ERROR_CODE)
.json({ error: SERVER_ERROR_MSG });
} else {
console.log(QUERY_SUCCESS_MSG);
res.status(STATUS_OK_CODE).json({ info: result });
}
});
connection.release();
}
});
};
export const getEMD = (req, res) => {
const {
params: { id },
} = req;
const query = `SELECT CODE, EMDNAME FROM LOCINFO WHERE SGGCODE = ${id}`;
db((connErr, connection) => {
if (connErr) {
console.log(DB_CONNECTION_ERROR_MSG);
res.status(STATUS_SERVER_ERROR_CODE).json({ error: SERVER_ERROR_MSG });
} else {
connection.query(query, (queryErr, result) => {
if (queryErr) {
console.log(DB_QUERY_ERROR_MSG);
res
.status(STATUS_SERVER_ERROR_CODE)
.json({ error: SERVER_ERROR_MSG });
} else {
console.log(QUERY_SUCCESS_MSG);
res.status(STATUS_OK_CODE).json({ info: result });
}
});
connection.release();
}
});
};
import mysql from "mysql";
import dotenv from "dotenv";
dotenv.config();
// MySQL Config
const db_config = {
host: process.env.MYSQL_HOST || "localhost",
user: process.env.MYSQL_USER || "root",
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE || "EUE",
connectionLimit: 5,
};
// Creation of MySQL Pool
const pool = mysql.createPool(db_config);
// Pool을 이용시, Connection을 생성 후 반환하여 사용
const connection = (callback) => {
console.log("db_connection()");
pool.getConnection(async (err, connection) => {
console.log("db_connection_poolConnection()");
await callback(err, connection);
});
};
export default connection;
import express from "express";
import routes from "../routes";
import { getDo, getEMD, getSGG } from "../controllers/locCodeController";
const locCodeRouter = express.Router();
locCodeRouter.get(routes.do, getDo);
locCodeRouter.get(routes.sigungu + routes.Detail(), getSGG);
locCodeRouter.get(routes.eupmyeondong + routes.Detail(), getEMD);
export default locCodeRouter;
...@@ -12,7 +12,7 @@ const DATA_OUTSIDE = "/outside"; ...@@ -12,7 +12,7 @@ const DATA_OUTSIDE = "/outside";
// # Local Code Data // # Local Code Data
const LOCCODE = "/loccode"; const LOCCODE = "/loccode";
const DO = "/do"; const DO = "/do";
const SIGUNGU = "/sigungu"; const SIGUNGU = "/si-gun-gu";
const EUPMYEONDONG = "/eup-myeon-dong"; const EUPMYEONDONG = "/eup-myeon-dong";
// # Auth // # Auth
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment