Ojimin

Update mysql module and Create rest api

1 +host=dataserver.chilwi8knzrl.us-east-1.rds.amazonaws.com
2 +user=admin
3 +password=apple0606!
4 +port=3306
5 +database=csvdata
...@@ -2,5 +2,3 @@ node_modules/ ...@@ -2,5 +2,3 @@ node_modules/
2 2
3 package-lock.json 3 package-lock.json
4 4
5 -#database
6 -database.json
...\ No newline at end of file ...\ No newline at end of file
......
1 +const express =require('express');
2 +const mysql = require('mysql');
3 +const app = express();
4 +const dotenv = require('dotenv').config(); //dotenv를 사용하기 위해서 dotenv 라이브러리를 불러온 뒤, config() 메소드를 호출
5 +const mysqlConObj = require('./config/mysql'); //config의 mysql파일을 가져와 mysqlConnection 객체 사용
6 +const db = mysqlConObj.init();
7 +
8 +mysqlConObj.open(db); //db 연결
9 +
10 +
11 +
12 +
1 +const mysql = require('mysql'); // mysql 드라이버 불러오기
2 +
3 +const mysqlConnection = {
4 + init: function() { //DB와 연결하는 객체 생성
5 + return mysql.createConnection({
6 + host: process.env.host,
7 + user: process.env.user,
8 + password: process.env.password,
9 + port:process.env.port,
10 + database:process.env.database
11 + });
12 + },
13 + open: function(con) { //생성된 객체를 DB와 연결
14 + con.connect(err => {
15 + if(err){
16 + console.log("MySQL 연결 실패 : ",err);
17 + } else{
18 + console.log("MySQL 연결 성공");
19 + }
20 + });
21 + },
22 + close: function(con){ //DB와 연결을 종료
23 + con.end(err => {
24 + if(err){
25 + console.log("MySQL 종료 실패 : ", err);
26 + } else {
27 + console.log("MySQL Terminated...");
28 + }
29 + })
30 + }
31 +}
32 +
33 +module.exports = mysqlConnection; //생성한 mysqlConnection 객체를 모듈화하여 외부 파일에서 불러와 사용할 수 있도록 export함
...\ No newline at end of file ...\ No newline at end of file
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
9 "author": "", 9 "author": "",
10 "license": "ISC", 10 "license": "ISC",
11 "dependencies": { 11 "dependencies": {
12 + "dotenv": "^10.0.0",
12 "express": "^4.17.1", 13 "express": "^4.17.1",
13 "mysql": "^2.18.1", 14 "mysql": "^2.18.1",
14 "path": "^0.12.7", 15 "path": "^0.12.7",
......
...@@ -4,38 +4,18 @@ const path = require("path"); ...@@ -4,38 +4,18 @@ const path = require("path");
4 const HTTPS = require("https"); 4 const HTTPS = require("https");
5 5
6 const app = express(); 6 const app = express();
7 -const domain = "2020105619.oss2021.tk"; 7 +const domain = "2020105635.oss2021.tk";
8 const sslport = 8080; 8 const sslport = 8080;
9 9
10 -const data = fs.readFileSync('database.json');
11 -const conf = JSON.parse(data);
12 -const mysql = require('mysql');
13 -
14 app.use(express.static(path.join(__dirname, "kakao"))); 10 app.use(express.static(path.join(__dirname, "kakao")));
15 app.use(express.static(path.join(__dirname, "public"))); 11 app.use(express.static(path.join(__dirname, "public")));
16 12
17 -const connection = mysql.createConnection({
18 - host:conf.host,
19 - user:conf.user,
20 - password:conf.password,
21 - port:conf.port,
22 - database:conf.database
23 -});
24 -
25 -connection.connect();
26 -
27 app.get("/", function (req, res) { 13 app.get("/", function (req, res) {
28 res.sendFile(path.join(__dirname + "/main.html")); 14 res.sendFile(path.join(__dirname + "/main.html"));
29 }); 15 });
30 16
31 app.get("/geolocation", function (req, res) { 17 app.get("/geolocation", function (req, res) {
32 res.sendFile(path.join(__dirname + "/kakao/kakaomap.html")); 18 res.sendFile(path.join(__dirname + "/kakao/kakaomap.html"));
33 - connection.query('SELECT * FROM csvdata.csvdata', function (error, results, fields) {
34 - if (error) {
35 - throw(error);
36 - }
37 - res.send(results);
38 - });
39 }); 19 });
40 20
41 try { 21 try {
......