개발자 되는 법.../TIL...

3-4. Auth + MiddleWare

개발_자 2024. 10. 25. 03:53

MiddleWare

미들웨어는 요청이 완료되기 전에 코드를 실행할 수 있는 기능 제공. 들어오는 요청을 기반으로 응답을 재작성, 리다이렉트, 요청 또는 응답 헤더 수정, 혹은 직접 응답 가능

사용을 위해서는 app디렉토리와 동일한 위치, 프로젝트 루트에 middleware.ts파일 생성

import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
  return NextResponse.redirect(new URL('/home', request.url));
}

export const config = {
  matcher: '/about/:path*',
};