From 5437da386f5f881a411454a323e1295908a36a5d Mon Sep 17 00:00:00 2001 From: Aqoom01 Date: Fri, 14 Nov 2025 15:27:19 +0900 Subject: [PATCH 1/3] =?UTF-8?q?docs:=207=EC=A3=BC=EC=B0=A8=20=ED=82=A4?= =?UTF-8?q?=EC=9B=8C=EB=93=9C=20=EB=B0=8F=20=EB=AF=B8=EC=85=98=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- week07/keyword/Keyword.md | 0 week07/mission/Mission.md | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 week07/keyword/Keyword.md create mode 100644 week07/mission/Mission.md diff --git a/week07/keyword/Keyword.md b/week07/keyword/Keyword.md new file mode 100644 index 0000000..e69de29 diff --git a/week07/mission/Mission.md b/week07/mission/Mission.md new file mode 100644 index 0000000..e69de29 From 09630d47c9893e871bc321820ea8aeec52388ec5 Mon Sep 17 00:00:00 2001 From: Aqoom01 Date: Fri, 14 Nov 2025 15:28:38 +0900 Subject: [PATCH 2/3] =?UTF-8?q?docs:=20=EB=AC=B8=EC=84=9C=20=EB=82=B4?= =?UTF-8?q?=EC=9A=A9=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- week07/keyword/Keyword.md | 89 +++++++++++++++++++++++++++++++++++++++ week07/mission/Mission.md | 52 +++++++++++++++++++++++ 2 files changed, 141 insertions(+) diff --git a/week07/keyword/Keyword.md b/week07/keyword/Keyword.md index e69de29..ab77509 100644 --- a/week07/keyword/Keyword.md +++ b/week07/keyword/Keyword.md @@ -0,0 +1,89 @@ +- 미들웨어 + + ## **1) 정의** + + - Express에서 **요청(request) → 응답(response)** 과정 중간에 위치해 **기능을 수행하는 함수**. + - 모든 요청은 미들웨어 파이프라인을 따라 흐르며, 미들웨어는 요청/응답 객체를 수정하거나, 특정 조건에서 응답을 종료하거나, 다음 미들웨어(next)를 호출할 수 있음. + + ## **2) 특징** + + - `app.use()` 또는 라우터 단위로 적용 가능. + - 순서 기반 → **정의 순서가 매우 중요함**. + - 요청/응답 가공, 인증, 로깅, 에러 처리 등에 사용. + + ## **3) 실무에서 자주 쓰는 타입** + + | 종류 | 설명 | + | --- | --- | + | Application Middleware | `app.use()`로 전체 적용 | + | Router Middleware | 특정 router에만 적용 | + | Built-in Middleware | `express.json()`, `express.urlencoded()` | + | Third-party | `cors`, `morgan`, `helmet` | + | Error Handling Middleware | 에러 처리 전용(4개의 인자 필요) | +- HTTP 상태 코드 + + ## **1) 개요** + + 클라이언트 요청에 대해 서버가 어떤 상태인지 의미하는 3자리 숫자. + + ## **2) 범위별 특징** + + | 범위 | 의미 | 예시 | + | --- | --- | --- | + | **1xx** | 정보 | 100 Continue | + | **2xx** | 성공 | 200 OK, 201 Created | + | **3xx** | 리다이렉트 | 301 Moved Permanently | + | **4xx** | 요청 오류 | 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found | + | **5xx** | 서버 오류 | 500 Internal Server Error, 503 Service Unavailable | + + ## **3) 실무에서 자주 사용하는 코드 정리** + + ### ✔ 200 OK + + 정상 응답 + + → 조회 성공, 리스트 조회 등 + + ### ✔ 201 Created + + 리소스 생성 완료 + + → 회원가입, 글쓰기 성공 + + ### ✔ 400 Bad Request + + 클라이언트 요청이 잘못됨 + + → body 부족, 파라미터 이상 + + ### ✔ 401 Unauthorized + + 인증 필요 + + → JWT 토큰 없거나 유효하지 않음 + + ### ✔ 403 Forbidden + + 권한 부족 + + → 관리자만 가능한 요청인데 일반 유저가 요청 + + ### ✔ 404 Not Found + + URL 또는 리소스가 존재하지 않음 + + ### ✔ 500 Internal Server Error + + 서버 코드 내부 오류 + + → try/catch에서 잡히지 못한 에러 + +- 에러 핸들링(Error Handling) + + ## **1) 에러 핸들링이 중요한 이유** + + - 프로덕션 서비스는 실패를 무조건 고려해야 함. + - 에러가 나도 서버가 죽지 않아야 함. + - 클라이언트에 일관된 에러 구조를 전달해야 함. + + `TypeError: object is not iterable` 같은 내부 에러가 그대로 외부로 노출됨 → 매우 위험. \ No newline at end of file diff --git a/week07/mission/Mission.md b/week07/mission/Mission.md index e69de29..ee6f96f 100644 --- a/week07/mission/Mission.md +++ b/week07/mission/Mission.md @@ -0,0 +1,52 @@ +## 할 일 + +- [x] 표준 응답 규격: 기존에 이미 어느 정도 생각하고 있던 부분이라 헬퍼함수만 지정 필요 +- [x] 에러 핸들링: 실습 + Error 던지기 + +## 헬퍼 함수 지정 + +```jsx +//헬퍼 함수 지정 +app.use((req, res, next) => { + res.success = (data, statusCode = StatusCodes.OK) => { + return res.status(statusCode).json({ + success: true, + code: statusCode, + data, + }); + }; + + res.error = ({ + statusCode = 500, + errorCode = "unknown", + reason = null, + data = null, + } = {}) => { + return res.status(statusCode).json({ + success: false, + code: errorCode, + reason, + data, + }); + }; + + next(); +}); + +//에러 핸들링 +app.use((err, req, res, next) => { + if (res.headersSent) { + return next(err); + } + + res.status(err.statusCode || 500).error({ + errorCode: err.errorCode || "unknown", + reason: err.reason || err.message || null, + data: err.data || null, + }); +}); +``` + +## ERROR 던지기 + +![image.png](attachment:c159bdec-7c2c-42ea-95e2-bba4e0cd02ad:image.png) \ No newline at end of file From 1e939238656221dacc8c024ca345d48942299c5a Mon Sep 17 00:00:00 2001 From: rlaush <84065399+Aqoom01@users.noreply.github.com> Date: Fri, 14 Nov 2025 15:29:16 +0900 Subject: [PATCH 3/3] =?UTF-8?q?docs:=20=EA=B4=80=EB=A0=A8=20=EC=82=AC?= =?UTF-8?q?=EC=A7=84=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- week07/mission/Mission.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week07/mission/Mission.md b/week07/mission/Mission.md index ee6f96f..91a6e93 100644 --- a/week07/mission/Mission.md +++ b/week07/mission/Mission.md @@ -49,4 +49,4 @@ app.use((err, req, res, next) => { ## ERROR 던지기 -![image.png](attachment:c159bdec-7c2c-42ea-95e2-bba4e0cd02ad:image.png) \ No newline at end of file +image