How does file-based routing work in Next.js?
In the App Router, each folder inside app/ is a URL segment, and a route becomes publicly reachable only when that folder contains a page.tsx (UI) or route.ts (API). Brackets, parentheses and other folder-name conventions add dynamic and organizational behavior.
Basic mapping
app/page.tsxserves/.app/about/page.tsxserves/about.app/blog/[slug]/page.tsxserves/blog/anything.app/api/users/route.tsserves/api/usersfor HTTP methods.
A folder without page or route is not a route, so you can safely colocate components, tests and utilities next to it.
Folder conventions
[id]dynamic segment,[...slug]catch-all,[[...slug]]optional catch-all.(group)route group: organizes files or shares a layout without adding to the URL._folderprivate folder: excluded from routing entirely.@slotparallel route slot rendered by the parent layout.(.)photointercepting route, for example opening a modal over the current page.
Special files in a segment
layout, page, loading, error, not-found, template, default and route. They nest: a page renders inside every parent layout, wrapped by the nearest loading and error boundaries.
Pages Router comparison
In pages/, every file is a route (pages/about.tsx serves /about), so you cannot colocate non-route files there without them becoming pages.
Type safety
Run next typegen (or just next dev/next build) to generate global helpers such as PageProps<'/blog/[slug]'> and LayoutProps. Setting typedRoutes: true makes <Link href> type-checked against real routes.
Code example
1// app/2// ├── page.tsx -> /3// ├── (marketing)/ -> group, not in URL4// │ ├── layout.tsx shared marketing layout5// │ └── pricing/page.tsx -> /pricing6// ├── blog/7// │ ├── page.tsx -> /blog8// │ ├── _components/PostCard.tsx private, never a route9// │ └── [slug]/page.tsx -> /blog/:slug10// ├── docs/[[...path]]/page.tsx -> /docs, /docs/a, /docs/a/b11// └── api/health/route.ts -> GET /api/health1213// app/blog/[slug]/page.tsx14export default async function Post({ params }: PageProps<"/blog/[slug]">) {15 const { slug } = await params; // params is a Promise since Next.js 1516 return <h1>Post: {slug}</h1>;17}1819// app/api/health/route.ts20export function GET() {21 return Response.json({ ok: true });22}
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.