애플리케이션 : 특정 기능을 제공하는 프로그램
API : 둘 이상이 컴퓨터, 애플리케이션 간에 자료를 어떻게 주고받는 지를 지정
RESTful API : REST를 명심해서 개발한 API
-REST : HTTP 프로토콜을 활용해서 자료의 현재 상태를 주고 받는 것
디자인 아키텍처 : 코드를 읽기 쉽고 관리하기 편하도록 기능이나 역할에 따라 여러 파일로 나눈 뒤 연결해서 사용하는 것
MVC : 모델(Model), 뷰(Vue), 컨트롤러(Controller) 세 영역으로 나누어 애플리케이션을 구성
-모델 : 애플리케이션을 처리할 대상으로, 자료를 저장/검색/수정하는 함수
-뷰 : 처리 결과를 화면을 통해 시각적으로 보여줌
-컨트롤러 : 모델과 뷰 중간에 위치하면서 요청에 따라 모델이나 뷰를 수정하는 역할, 라우트 코드
express-async-handler : try ~ catch 문을 한 줄로 합친 패키지
//express-async-handler 설치
>> npm intstall express-async-handler
routes - <contactRoutes.js>, controllers - <contactControllers.js> : 컨트롤러 작성하기
//contactControllers.js
const asynchHandler = require("express-async-handler");
//Get all contact, /contact : 데이터 가져오기
const getAllContact = asynchHandler(async (req, res) => {
res.status(200).send("Contact Page");
});
//Post create contact, /contact : 데이터 추가하기
const postCreateContect = asynchHandler(async (req, res) => {
console.log(req.body);
const { name, email, phone } = req.body;
if (!name || !email || !phone) {
res.status(400).send("필수 값이 입력되지 않았습니다.");
}
res.status(201).send("Create Contact");
});
//Get ID contact, /contact/:id : 특정 ID 데이터 가져오기
const getContact = asynchHandler(async (req, res) => {
res.status(200).send(`View Contact for ID : ${req.params.id}`);
});
//Put ID contact, /contact/:id : 특정 ID 데이터 수정하기
const updateContact = asynchHandler(async (req, res) => {
res.status(200).send(`Update Contact for ID : ${req.params.id}`);
});
//Delete ID contact, /contact/:id : 특정 ID 데이터 삭제하기
const deleteContact = asynchHandler(async (req, res) => {
res.status(200).send(`Delete Contact for ID : ${req.params.id}`);
});
module.exports = {
getAllContact,
postCreateContect,
getContact,
updateContact,
deleteContact,
};
//contactRoutes.js
const express = require("express");
const router = express.Router();
const {
getAllContact,
postCreateContect,
getContact,
updateContact,
deleteContact,
} = require("../controllers/contactController");
router.route("/").get(getAllContact).post(postCreateContect);
router.route("/:id").get(getContact).put(updateContact).delete(deleteContact);
module.exports = router;
'Do it Node.js > EJS BackEnd' 카테고리의 다른 글
15. EJS (0) | 2024.03.13 |
---|---|
14. CRUD (0) | 2024.03.12 |
12. 몽고DB, 스키마 (0) | 2024.03.08 |
11. 미들웨어 (0) | 2024.03.06 |
10. Express (0) | 2024.03.03 |