Next.js Folders Are Routes
A folder with a page.tsx file becomes a web address. The folder name is the address.
The books page file
The file lives in app/books. Its heading names the page.
export default function Books() {
return <h1>Books on the shelf</h1>;
}The about page file
The file lives in app/about. Its heading names the page.
export default function About() {
return <h1>About My Bookshelf</h1>;
}Both pages answer
The check asks each page for its status. The answer is HTTP 200, which means it worked.
curl -s -o /dev/null -w "HTTP %{http_code}\n" http://localhost:3000/books
curl -s -o /dev/null -w "HTTP %{http_code}\n" http://localhost:3000/aboutExample
const routes = [["/books", "Books on the shelf"], ["/about", "About My Bookshelf"]];
routes.forEach(([path, heading]) => console.log(path + " → " + heading + " (200)"));Key points
- Route: a web address that opens one page
- app/books/page.tsx opens at /books. The folder name books is the address.
- app/about/page.tsx opens at /about. The folder name about is the address.
- A folder without page.tsx opens nothing. The page file is what makes the route.
- A nested folder makes a nested address. A page in app/books/new/page.tsx would open at /books/new.
- No config file lists the routes. The folders are the list.
In this example, the list holds the two new routes and their headings. The /books route shows "Books on the shelf", and the /about route shows "About My Bookshelf". Each folder name became its address. Both pages answer 200, so both routes work.
Note
The home page is the odd one out. It lives in app/page.tsx, with no folder of its own.
Try it Yourself
Edit the code, then press Run.
Running is simulated · TypeScript Playground
Exercise
Which file opens at /books?