# App Router File-System Routing

> How folders and files magically turn into URLs in your browser.

Subject: Next.js | Lesson 2 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/app-router-routing
Previous: [Server vs Client Components](https://codewithanimation.in/nextjs/server-vs-client-components.md) | Next: [Route Segments & Nested Routing](https://codewithanimation.in/nextjs/route-segments-nested.md)

## Folders are Routes

Next.js 13 introduced the App Router, a radically simplified way to handle navigation. Instead of writing complex routing configuration code, you simply create folders.

Each folder inside the `app` directory represents a route segment that maps to a URL path. For example, a folder named `dashboard` maps to the `/dashboard` URL.

## Why use the file system?

File-system routing is incredibly intuitive. By looking at a project's folder structure, any developer can instantly understand the architecture of the website without having to decipher a massive, centralized `routes.ts` file.

It also enables powerful features like automatic layout nesting, granular error boundaries, and colocation of related files.

## The 'page' convention

Creating a folder does not automatically make that route accessible to users. A route segment is only publicly accessible if it contains a `page.js` (or `.tsx`) file.

This `page` file acts as the leaf UI for that specific path. If you navigate to `/dashboard/settings`, the Next.js router looks for `app/dashboard/settings/page.tsx` and renders it.

## Common mistake: The 404 capitalization trap

Developers coming from standard React are used to naming their component files with capital letters, like `Dashboard.tsx` or `Settings.tsx`.

In the App Router, special routing files must be strictly lowercase. Naming a file `Page.tsx` or `index.tsx` instead of exactly `page.tsx` will cause Next.js to ignore it, resulting in a frustrating 404 Not Found error.

## Tip: Safe colocation

Because Next.js strictly looks for specific filenames like `page.js` or `layout.js`, you are free to colocate your other files directly inside the route folders.

You can safely put `button.tsx`, `utils.ts`, or `styles.css` right next to your `page.tsx` file. The router will completely ignore them, keeping your project cleanly organized by feature rather than by file type.

## Try it: Visualizing the Router

Scroll down to see how a nested folder structure translates into a final URL path.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/app-router-routing

## Check yourself

### Question 1

In the Next.js App Router, what dictates the URL path?

- A. A central routes.js configuration file
- B. The folder structure inside the app directory
- C. The names of the React components exported
- D. The React Router DOM package

Correct answer: B

Next.js uses file-system based routing. Folders inside the `app` directory define the route segments that make up the URL.

- Why not A: Next.js avoids centralized configuration files for routing.
- Why not C: Component names don't matter, only the folder names matter.
- Why not D: Next.js has its own built-in router; you don't use React Router.

### Question 2

What must a folder contain to be publicly accessible as a route?

- A. An index.js file
- B. A layout.js file
- C. A Component.tsx file
- D. A page.js (or .tsx) file

Correct answer: D

A folder defines the route path, but the `page.js` file defines the actual UI that is rendered when a user visits that path.

- Why not A: index.js was the convention in the older Pages Router, not the App Router.
- Why not B: Layouts are optional.
- Why not C: The file must match the specific Next.js convention.

### Question 3

What happens if you place a file named 'helper.ts' inside the app/dashboard folder?

- A. It creates a route at /dashboard/helper
- B. It crashes the Next.js build
- C. It is ignored by the router and can be safely imported by your page
- D. It overrides the dashboard page

Correct answer: C

Next.js only acts on specific special files (like page, layout, error). Any other file is ignored by the router, allowing for safe colocation of components, tests, and styles.

- Why not A: Only folders create route paths.
- Why not B: Next.js encourages colocating files.
- Why not D: Only special files like page.js affect the router.

## Remember this

- Folders inside the `app` directory map directly to URL route segments.
- A route is only accessible if it contains a `page.js` or `page.tsx` file.
- Routing files must be lowercase exactly as specified by Next.js.
- You can safely colocate non-routing files (like components and styles) inside route folders.
