-
Notifications
You must be signed in to change notification settings - Fork 22
[이지수A] Sprint11 #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[이지수A] Sprint11 #48
The head ref may contain hidden characters: "express-\uC774\uC9C0\uC218A"
Conversation
|
|
||
| app.use( | ||
| cors({ | ||
| origin: "http://localhost:3001", // 프론트엔드 주소 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요 부분도 환경변수로 바꿔주세요!
| res: Response, | ||
| next: NextFunction | ||
| ) => Promise<any>; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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[]> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요 부분이 generic 적용 예시인거 익혀주세요!
| ); | ||
| }, | ||
| }); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅참고사항: storage.destination, filename은 반드시 콜백 내에서 동기적으로 처리해야 합니다.
요구사항
공통 요구사항
백엔드 요구사항
주요 변경사항
스크린샷
멘토에게