티스토리 뷰
폴더 생성
레이어드 아키텍처에 따라 용도별로 폴더를 분류했다.
이번 모임모임 프로젝트에는 크게 user 관련, 모임(club) 관련 2가지로 나눌 수 있다.

- controllers/ : 라우팅 관리
- libs/ : 라이브러리
- middlewares/ : 미들웨어
- services/ : 실제 데이터베이스 다루는 부분
- server.js : 메인 파일
1. server.js
const express = require("express")
const app = express()
const cors = require("cors")
const bodyParser = require("body-parser")
const clubController = require("./controllers/clubController")
const userController = require("./controllers/userController")
app.use(cors())
app.use(bodyParser.json())
app.use("/clubs", clubController)
app.use("/users", userController)
app.listen(80, () => {
console.log("start server in 80")
})
1) express 사용 및 서버 포트 열기
const express = require("express")
const app = express()
app.listen(80, () => {
console.log("start server in 80")
})
2) cors 에러 해결 위한 라이브러리
const cors = require("cors")
app.use(cors())
3) body 데이터를 파싱해주는 미들웨어로 body-parser 사용
const bodyParser = require("body-parser")
app.use(bodyParser.json())
4) 컨트롤러로 라우팅
const clubController = require("./controllers/clubController")
const userController = require("./controllers/userController")
app.use("/clubs", clubController)
app.use("/users", userController)
2. libs/db.js
const mysql = require("mysql2/promise");
const pool = mysql.createPool({
host: "localhost",
user: "",
password: "",
database: "",
connectionLimit: 10,
});
exports.getConnection = async () => {
return await pool.getConnection();
};
1) 동기적인 처리를 위해 mysql2/promise 사용
const mysql = require("mysql2/promise");
2) db connection pool 생성
const pool = mysql.createPool({
host: "localhost",
user: "",
password: "",
database: "",
connectionLimit: 10,
});
- connection pool
미리 커넥션 객체를 pool에 만들어두고 필요할 때마다 꺼내 쓰고, 다시 반환하는 방식.
매번 커넥션 객체를 생성하고 제거하는 과정을 하게 되면, 사용자가 많아질수록 성능이 저하될 수 있음.
pool을 사용함으로써 그런 부분을 개선. - conenctionLimit
pool에 보관할 수 있는 connection 객체 수의 최댓값으로 나는 10으로 지정했다. default값이 10이다.
3) 다른 파일에서 쓸 수 있도록 connection pool 모듈화
exports.getConnection = async () => {
return await pool.getConnection();
};'프로젝트 > 모임모임' 카테고리의 다른 글
| [모임 플랫폼 서비스 개발] 백엔드(Node.js) - 모임 불러오기 (0) | 2022.05.26 |
|---|---|
| [모임 플랫폼 서비스 개발] 백엔드(Node.js) - 사용자 권한(Authorization) 관련 미들웨어 구현 (0) | 2022.05.26 |
| [모임 플랫폼 서비스 개발] 백엔드(Node.js) - 로그인 기능 구현 (0) | 2022.05.26 |
| [모임 플랫폼 서비스 개발] 백엔드(Node.js) - 회원가입 기능 구현 (0) | 2022.05.26 |
| [모임 플랫폼 서비스 개발] Node.js & React - 시작 (0) | 2022.05.26 |
댓글
