
모듈식 프로그래밍은 대규모 프로그램을 독립적으로 작동 가능한 작은 단위인 모듈로 분할하는 프로그래밍 방식이다. 각 모듈은 특정 기능을 담당하며, 모듈 간 상호 작용을 통해 전체 프로그램의 기능을 구현한다. 모듈은 재사용이 가능하며, 하나의 모듈을 변경해도 다른 모듈에 미치는 영향을 최소화할 수 있다.
계층화 아키텍처는 소프트웨어를 여러 레벨의 계층으로 분리하는 것이다. 각 계층은 특정한 역할과 책임을 지니며, 계층 간 의존성은 엄격하게 관리된다. 이러한 구조는 시스템의 복잡성을 관리하고, 각 부분이 서로 간섭하지 않도록 하는 데에 목적이 있다.
라우터는 클라이언트의 요청을 적절한 컨트롤러나 핸들러로 연결하는 역할을 한다. URL 경로 및 HTTP 메소드(GET, POST 등)를 기반으로 이를 수행한다.
컨트롤러는 클라이언트의 요청을 처리하고 적절한 서비스 또는 비즈니스 로직을 호출한다. 응답 데이터를 구성하여 클라이언트에게 반환한다.
서비스 계층은 비즈니스 로직을 구현한다. 데이터 유효성 검사, 계산, 데이터 변환 등의 작업을 수행한다.
리포지토리는 데이터베이스와의 상호 작용을 담당한다. 데이터 조회, 저장, 업데이트, 삭제 등의 작업을 캡슐화한다.
엔티티는 데이터베이스 테이블을 객체화한 것이다. 하나의 엔티티는 일반적으로 하나의 데이터베이스 테이블과 대응한다.
- /node_modules
- /src
- /controllers
- boardController.js
- /routes
- boardRoutes.js
- /services
- boardService.js
- /repositories
- boardRepository.js
- /models
- board.js
- app.js
- package.json
Board Router (boardRoutes.js)
const express = require('express');
const router = express.Router();
const boardController = require('../controllers/boardController');
router.get('/', boardController.getAllBoards);
router.post('/', boardController.createBoard);
// 추가 경로 정의...
module.exports = router;
**Board Controller
(boardController.js)**
const boardService = require('../services/boardService');
exports.getAllBoards = async (req, res) => {
const boards = await boardService.getAllBoards();
res.json(boards);
};
exports.createBoard = async (req, res) => {
const newBoard = await boardService.createBoard(req.body);
res.json(newBoard);
};
// 추가 컨트롤러 함수...
Board Service (boardService.js)
const boardRepository = require('../repositories/boardRepository');
exports.getAllBoards = async () => {
return await boardRepository.findAll();
};
exports.createBoard = async (boardData) => {
return await boardRepository.create(boardData);
};
// 추가 서비스 함수...
Board Repository (boardRepository.js)
const Board = require('../models/board');
exports.findAll = async () => {
// 데이터베이스 조회 로직
};
exports.create = async (boardData) => {
// 데이터베이스 생성 로직
};
// 추가 리포지토리 함수...
Board Model (board.js)
class Board {
constructor(id, title, content, author) {
this.id = id;
this.title = title;
this.content = content;
this.author = author;
}
// 추가 모델 메서드...
}
module.exports = Board;
Express App (app.js)
const express = require('express');
const app = express();
const boardRoutes = require('./routes/boardRoutes');
app.use(express.json());
app.use('/board', boardRoutes);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});