Next.js 13 버전부터 지원하는 App 라우팅을 사용해 보자.
app/
디렉토리 내부의 page.tsx
파일과 layout.tsx
파일을 사용하여 루트 디렉토리(/
)를 설정할 수 있다.export default function RootLayout({children,}: {children: React.ReactNode}) {
return (
<html lang="en">
<body>{children}</body>
{/* html, body 태그를 사용하여 레이아웃을 구성*/}
{/* children은 page.tsx 내부의 컴포넌트를 받아서 레이아웃을 구성한다. */}
</html>
)
}
export default function Page() {
return <p>Root Page</p>
}
npm run dev
로 실행 후 http://localhost:3000/↗ 에 접속하면 Root Page
가 출력된다.app/
의 하위 폴더를 생성하여 라우팅을 생성 할 수 있다.export default function ExamplePage() {
return <p>Example Page</p>
}
export default function SlugPage({ params }: { params: { slug: string } }) {
return <p>{params.slug} Page</p>
}
app/api/
디렉토리를 생성하여 사용할 수 있다.import { NextResponse } from "next/server";
export async function GET() {
return NextResponse.json({ message: "Hello World" });
}
npm run dev
로 실행 후 http://localhost:3000/api/hello↗ 에 접속하면 {"message":"Hello World"}
가 출력된다.