[이지수A] Sprint11#48
Merged
loquemedalagana merged 1 commit intocodeit-sprint-fullstack:mainfrom Jun 21, 2025
Hidden character warning
The head ref may contain hidden characters: "express-\uc774\uc9c0\uc218A"
Merged
Conversation
|
|
||
| app.use( | ||
| cors({ | ||
| origin: "http://localhost:3001", // 프론트엔드 주소 |
Contributor
There was a problem hiding this comment.
요 부분도 환경변수로 바꿔주세요!
| res: Response, | ||
| next: NextFunction | ||
| ) => Promise<any>; | ||
|
|
Contributor
There was a problem hiding this comment.
generic 으로 바꿔 보시는거 어때요?
✅ Generic 버전 예시 (T를 반환 타입으로 지정)
import { Request, Response, NextFunction } from "express";
type AsyncFunction<T = any> = (
req: Request,
res: Response,
next: NextFunction
) => Promise<T>;
export const asyncHandler =
<T = any>(fn: AsyncFunction<T>) =>
(req: Request, res: Response, next: NextFunction): void => {
Promise.resolve(fn(req, res, next)).catch(next);
};
🧠 설명
T는fn이 무엇을 반환하는지 타입을 명시하기 위한 제네릭입니다.- 기본값을
any로 설정해둬서 명시하지 않아도 동작은 하되, 필요한 경우 특정 반환 타입을 명시할 수 있습니다.
예를 들어:
const getUser = asyncHandler<{ name: string }>(async (req, res) => {
const user = await fetchUser();
res.json(user);
});
✨ 이점
- API 응답 타입을 명확히 하고자 할 때 도움이 됩니다.
- TypeScript 환경에서 컨트롤러 리턴값을 추론하거나 추적할 때 용이합니다.
- 필요시
Request나Response타입도 제네릭으로 확장 가능하게 커스터마이즈할 수 있습니다.
| async function findByProductId( | ||
| productId: number, | ||
| options: CommentRepositoryPaginationOptions = {} | ||
| ): Promise<CommentWithWriter[]> { |
Contributor
There was a problem hiding this comment.
요 부분이 generic 적용 예시인거 익혀주세요!
| ); | ||
| }, | ||
| }); | ||
|
|
Contributor
There was a problem hiding this comment.
✅참고사항: storage.destination, filename은 반드시 콜백 내에서 동기적으로 처리해야 합니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
요구사항
공통 요구사항
백엔드 요구사항
주요 변경사항
스크린샷
멘토에게