Jeongmin Seo

Connect AWS RDS and register environment variable by dotenv

...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
2 //모듈 2 //모듈
3 const express = require("express"); 3 const express = require("express");
4 const bodyParser = require("body-parser"); 4 const bodyParser = require("body-parser");
5 +//환경변수 (운영체제간 gap 없애고자)
6 +const dotenv = require("dotenv");
7 +dotenv.config();
5 const app = express(); 8 const app = express();
6 //라우팅 9 //라우팅
7 const home = require("./src/routes/home"); 10 const home = require("./src/routes/home");
......
1 "use strict"; 1 "use strict";
2 2
3 -const PORT = 3000;
4 -
5 const app = require("../app"); 3 const app = require("../app");
4 +const PORT = process.env.PORT || 3000;
6 5
7 app.listen(PORT, () => { 6 app.listen(PORT, () => {
8 console.log("서버 가동"); 7 console.log("서버 가동");
......
...@@ -36,6 +36,11 @@ ...@@ -36,6 +36,11 @@
36 "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", 36 "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
37 "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" 37 "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
38 }, 38 },
39 + "bignumber.js": {
40 + "version": "9.0.0",
41 + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
42 + "integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A=="
43 + },
39 "body-parser": { 44 "body-parser": {
40 "version": "1.20.0", 45 "version": "1.20.0",
41 "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.0.tgz", 46 "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.0.tgz",
...@@ -127,6 +132,11 @@ ...@@ -127,6 +132,11 @@
127 "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", 132 "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
128 "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" 133 "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ=="
129 }, 134 },
135 + "core-util-is": {
136 + "version": "1.0.3",
137 + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",
138 + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ=="
139 + },
130 "debug": { 140 "debug": {
131 "version": "2.6.9", 141 "version": "2.6.9",
132 "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", 142 "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
...@@ -145,6 +155,11 @@ ...@@ -145,6 +155,11 @@
145 "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", 155 "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz",
146 "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==" 156 "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg=="
147 }, 157 },
158 + "dotenv": {
159 + "version": "16.0.1",
160 + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.0.1.tgz",
161 + "integrity": "sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ=="
162 + },
148 "ee-first": { 163 "ee-first": {
149 "version": "1.1.1", 164 "version": "1.1.1",
150 "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", 165 "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
...@@ -316,6 +331,11 @@ ...@@ -316,6 +331,11 @@
316 "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", 331 "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
317 "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" 332 "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g=="
318 }, 333 },
334 + "isarray": {
335 + "version": "1.0.0",
336 + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
337 + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ=="
338 + },
319 "jake": { 339 "jake": {
320 "version": "10.8.5", 340 "version": "10.8.5",
321 "resolved": "https://registry.npmjs.org/jake/-/jake-10.8.5.tgz", 341 "resolved": "https://registry.npmjs.org/jake/-/jake-10.8.5.tgz",
...@@ -384,6 +404,24 @@ ...@@ -384,6 +404,24 @@
384 "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", 404 "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
385 "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" 405 "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
386 }, 406 },
407 + "mysql": {
408 + "version": "2.18.1",
409 + "resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz",
410 + "integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==",
411 + "requires": {
412 + "bignumber.js": "9.0.0",
413 + "readable-stream": "2.3.7",
414 + "safe-buffer": "5.1.2",
415 + "sqlstring": "2.3.1"
416 + },
417 + "dependencies": {
418 + "safe-buffer": {
419 + "version": "5.1.2",
420 + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
421 + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
422 + }
423 + }
424 + },
387 "negotiator": { 425 "negotiator": {
388 "version": "0.6.3", 426 "version": "0.6.3",
389 "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", 427 "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
...@@ -412,6 +450,11 @@ ...@@ -412,6 +450,11 @@
412 "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", 450 "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
413 "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" 451 "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w="
414 }, 452 },
453 + "process-nextick-args": {
454 + "version": "2.0.1",
455 + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
456 + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag=="
457 + },
415 "proxy-addr": { 458 "proxy-addr": {
416 "version": "2.0.7", 459 "version": "2.0.7",
417 "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", 460 "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
...@@ -445,6 +488,27 @@ ...@@ -445,6 +488,27 @@
445 "unpipe": "1.0.0" 488 "unpipe": "1.0.0"
446 } 489 }
447 }, 490 },
491 + "readable-stream": {
492 + "version": "2.3.7",
493 + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz",
494 + "integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==",
495 + "requires": {
496 + "core-util-is": "~1.0.0",
497 + "inherits": "~2.0.3",
498 + "isarray": "~1.0.0",
499 + "process-nextick-args": "~2.0.0",
500 + "safe-buffer": "~5.1.1",
501 + "string_decoder": "~1.1.1",
502 + "util-deprecate": "~1.0.1"
503 + },
504 + "dependencies": {
505 + "safe-buffer": {
506 + "version": "5.1.2",
507 + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
508 + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
509 + }
510 + }
511 + },
448 "safe-buffer": { 512 "safe-buffer": {
449 "version": "5.2.1", 513 "version": "5.2.1",
450 "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", 514 "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
...@@ -508,11 +572,31 @@ ...@@ -508,11 +572,31 @@
508 "object-inspect": "^1.9.0" 572 "object-inspect": "^1.9.0"
509 } 573 }
510 }, 574 },
575 + "sqlstring": {
576 + "version": "2.3.1",
577 + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz",
578 + "integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A="
579 + },
511 "statuses": { 580 "statuses": {
512 "version": "2.0.1", 581 "version": "2.0.1",
513 "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz", 582 "resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.1.tgz",
514 "integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ==" 583 "integrity": "sha512-RwNA9Z/7PrK06rYLIzFMlaF+l73iwpzsqRIFgbMLbTcLD6cOao82TaWefPXQvB2fOC4AjuYSEndS7N/mTCbkdQ=="
515 }, 584 },
585 + "string_decoder": {
586 + "version": "1.1.1",
587 + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
588 + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
589 + "requires": {
590 + "safe-buffer": "~5.1.0"
591 + },
592 + "dependencies": {
593 + "safe-buffer": {
594 + "version": "5.1.2",
595 + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
596 + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
597 + }
598 + }
599 + },
516 "supports-color": { 600 "supports-color": {
517 "version": "7.2.0", 601 "version": "7.2.0",
518 "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", 602 "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
...@@ -540,6 +624,11 @@ ...@@ -540,6 +624,11 @@
540 "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", 624 "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
541 "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" 625 "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw="
542 }, 626 },
627 + "util-deprecate": {
628 + "version": "1.0.2",
629 + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
630 + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
631 + },
543 "utils-merge": { 632 "utils-merge": {
544 "version": "1.0.1", 633 "version": "1.0.1",
545 "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", 634 "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
......
...@@ -7,8 +7,10 @@ ...@@ -7,8 +7,10 @@
7 }, 7 },
8 "dependencies": { 8 "dependencies": {
9 "body-parser": "^1.20.0", 9 "body-parser": "^1.20.0",
10 + "dotenv": "^16.0.1",
10 "ejs": "^3.1.8", 11 "ejs": "^3.1.8",
11 - "express": "^4.18.1" 12 + "express": "^4.18.1",
13 + "mysql": "^2.18.1"
12 }, 14 },
13 "devDependencies": {}, 15 "devDependencies": {},
14 "scripts": { 16 "scripts": {
......
1 +const mysql = require("mysql");
2 +
3 +const db = mysql.createConnection({
4 + host: process.env.DB_HOST,
5 + user: process.env.DB_USER,
6 + password: process.env.DB_PASSWORD,
7 + database: process.env.DB_DATABASE,
8 +});
9 +
10 +db.connect();
11 +
12 +module.exports = db;
...\ No newline at end of file ...\ No newline at end of file
1 -{
2 - "id": [
3 - "jeongmin",
4 - "jumi",
5 - "sss",
6 - "sjeongmin"
7 - ],
8 - "password": [
9 - "1234",
10 - "1234",
11 - "1111",
12 - "1111"
13 - ],
14 - "name": [
15 - "서정민",
16 - "양주미",
17 - "정민",
18 - "서정민"
19 - ]
20 -}
1 'use strict'; 1 'use strict';
2 - 2 +//for DB manipulate
3 const UserStorage = require("./UserStorage"); 3 const UserStorage = require("./UserStorage");
4 4
5 class User { 5 class User {
...@@ -9,6 +9,8 @@ class User { ...@@ -9,6 +9,8 @@ class User {
9 9
10 async login() { 10 async login() {
11 const client = this.body; 11 const client = this.body;
12 + try {
13 +
12 const {id, password} = await UserStorage.getUserInfo(client.id); 14 const {id, password} = await UserStorage.getUserInfo(client.id);
13 // console.log(id, password); 15 // console.log(id, password);
14 16
...@@ -19,6 +21,9 @@ class User { ...@@ -19,6 +21,9 @@ class User {
19 return { success : false, msg: "비밀번호가 틀렸습니다."}; 21 return { success : false, msg: "비밀번호가 틀렸습니다."};
20 } 22 }
21 return {success: false, msg: "존재하지 않는 아이디입니다."}; 23 return {success: false, msg: "존재하지 않는 아이디입니다."};
24 + } catch (err) {
25 + return {success: false, msg: err};
26 + }
22 } 27 }
23 28
24 async register() { 29 async register() {
......
1 'use strict'; 1 'use strict';
2 -//파일시스템 이용해서 파일 접근 및 DB 관리 2 +//for DB CRUD
3 -const fs = require("fs").promises; 3 +const db = require("../config/db");
4 4
5 -class UserStorage {
6 -
7 - static #getUserInfo(data, id) {
8 - const users = JSON.parse(data);
9 - const idx = users.id.indexOf(id);
10 - const userKeys = Object.keys(users); // [id, password, name]
11 - const userInfo = userKeys.reduce((newUser, info) => {
12 - newUser[info] = users[info][idx];
13 - return newUser;
14 - }, {});
15 - // console.log(userInfo);
16 - return userInfo;
17 - }
18 5
19 - static #getUsers(data, isAll, fields) { 6 +class UserStorage {
20 - const users = JSON.parse(data); 7 + // static getUsers(isAll, ...fields) {
21 - if (isAll) return users; 8 + // }
22 -
23 - const newUsers = fields.reduce((newUsers, field) => {
24 - if (users.hasOwnProperty(field)) {
25 - newUsers[field] = users[field];
26 - }
27 - return newUsers;
28 - }, {});
29 - return newUsers;
30 - }
31 -
32 - static getUsers(isAll, ...fields) {
33 - return fs
34 - .readFile("./src/databases/users.json")
35 - .then((data) => {
36 - return this.#getUsers(data, isAll, fields);
37 - })
38 - .catch((err) => console.error);
39 - }
40 9
41 static getUserInfo(id) { 10 static getUserInfo(id) {
42 - return fs 11 + return new Promise((resolve, reject) => {
43 - .readFile("./src/databases/users.json") 12 + const query = "SELECT * FROM users WHERE id = ?;";
44 - .then((data) => { 13 + db.query(query, [id], (err, data) => {
45 - return this.#getUserInfo(data, id); 14 + if (err) reject(`${err}`);
46 - }) 15 + // console.log(data[0]);
47 - .catch((err) => console.error); 16 + resolve(data[0]);
17 + });
18 + });
48 } 19 }
49 20
50 static async save(userInfo) { 21 static async save(userInfo) {
51 - const users = await this.getUsers(true); 22 + return new Promise((resolve, reject) => {
52 - //id가 없으면 회원가입 가능 23 + const query = "INSERT INTO users(id, name, password) VALUES(?, ?, ?);";
53 - if (users.id.includes(userInfo.id)) { 24 + db.query(
54 - throw "이미 존재하는 아이디입니다."; 25 + query,
26 + [userInfo.id, userInfo.name, userInfo.password],
27 + (err, data) => {
28 + if (err) reject(`${err}`);
29 + // console.log(data[0]);
30 + resolve({ success: true});
55 } 31 }
56 - users.id.push(userInfo.id); 32 + );
57 - users.name.push(userInfo.name); 33 + });
58 - users.password.push(userInfo.password);
59 - fs.writeFile("./src/databases/users.json", JSON.stringify(users));
60 - return { success: true};
61 } 34 }
62 } 35 }
63 36
......
1 +{
2 + "lockfileVersion": 1
3 +}