Next.js에서는 express에서와 같이 middleware를 사용가능하다.
만약 전역 middleware를 사용하고 싶다면 page폴더에 middleware.ts파일을 작성하면된다.
// middleware.ts
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// 아래의 경로에 해당하는경우 /about-2로 강제로 이동시킨다.
export function middleware(request: NextRequest) {
return NextResponse.redirect(new URL('/about-2', request.url))
}
// 특정 경로에서만 사용하고싶은경우
export const config = {
matcher: '/about/:path*', 혹은
// matcher:['/about/:path*', '/dashboard/:path*'],
}