디자인 패턴
- 상황에 따라 자주 쓰이는 설계 방법을 정리한 코딩 방법론
const indexRouter = require('./routes'); // index는 생략가능!
app.use('/', indexRouter); // localhost:PORT/ 경로를 기본으로
// ./routes/index.js 파일에 선언한 대로 동작!
const controller = require('../controller/Cmain.js');
...
// localhost:PORT/
router.get('/', controller.main); // GET /
router.get('/comments', controller.comments); // GET /comments
module exports = router;
404 Error 🚨
- 404 error는 클라이언트가 잘못된 주소로 접속했을 때 발생하는 error!
404 Error 라우팅 🚨
- 맨 마지막 라우트로 선언
- : 그 외 나머지 주소는 모두(all) 잘못된 요청임을 사용자에게 알려야 함
- 클라이언트가 올바르지 않은 주소로 요청 시 Error 페이지 렌더링
exports.main = (req, res) => {
res.render('index');
};
exports.comments = (res, req) => {
res.render('comments');
};
exports.commentInfos = () => {
return [
{
id: 1,
userId: 'helloworld',
date: '2022-10-11',
comment: '안녕!',
},
{
id: 2,
userId: 'world',
date: '2022-11-11',
comment: '안녕~~!',
},
];
};
const Comment = require('..model/Comment');
exports.main = (req, res) => {
res.render('index');
};
exports.comments = (req, res) => {
console.log(Comment.commentInfos());
res.render('comments', { commentInfos: Comment.commentInfos() });
};
리팩토링(Refactoring)
- (결과의 변경 없이) 코드를 재정비하는 것
- 장점
- 가독성 향상
- 유지보수성 향상
- 버그 수정, 기능 추가 X
- 로직(논리)개선