Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🔵 App Router – Pages, Layouts और Navigation  ·  Lesson 09

Pages और File-based Routing

मुख्य idea: folders ही URLs हैं

App Router में route tables कभी नहीं लिखते। app/ के अंदर हर folder एक URL हिस्सा है, और folder तभी खुलने लायक page बनता है जब उसमें page.jsx file हो।

app/rootcourses/segmentweb-development/segmentpage.jsxpublic बनाती है/courses/web-developmentfinal URL
Folders + page.jsx = एक route

पहले routes बनाइए

JSX – तीन page files
// app/about/page.jsx
export default function AboutPage() {
  return (
    <main className="container">
      <h1>About EduSite</h1>
      <p>We teach coding in Hindi + English since 2018.</p>
    </main>
  )
}

// app/courses/page.jsx
export default function CoursesPage() {
  return <h1>All courses</h1>
}

// app/courses/web-development/page.jsx
export default function WebDevPage() {
  return <h1>Web Development course</h1>
}

Files save करके /about, /courses और /courses/web-development खोलिए — restart की ज़रूरत नहीं।

Nested routes

गहरे URLs के लिए folders के अंदर folders रखें। app/courses/page.jsx /courses का index है, और child folders उसके नीचे pages बनाते हैं।

/app/page.jsx/coursesapp/courses/page.jsx/courses/web-developmentapp/courses/web-development/page.jsx
Folders का हर level URL में एक हिस्सा जोड़ता है

Folder → URL नक्शा

📁 app/
  page.jsx                 # /
  📁 about/
    page.jsx               # /about
  📁 courses/
    page.jsx               # /courses
    📁 web-development/
      page.jsx             # /courses/web-development
  📁 blog/
    page.jsx               # /blog
  📁 contact/
    page.jsx               # /contact
    ContactForm.jsx        # NOT a route (no page.jsx name)
एक छोटी site और उससे बनने वाले URLs

Colocation: helpers को page के पास रखें

सिर्फ़ page.jsx (और APIs के लिए route.js) public है। Components, CSS modules और helpers को उसी folder में बेझिझक रख सकते हैं। EduSite में app/contact/ContactForm.jsx और app/contact/actions.js contact page के पास हैं, पर URLs नहीं हैं।

💡 ऐसा folder चाहिए जो कभी route न बने? नाम underscore से शुरू करें: app/_utils/। Route Groups lesson में यह फिर मिलेगा।

URL पढ़ना: searchParams

?level=beginner जैसी query strings page में searchParams prop बनकर आती हैं। Next.js 16 में यह Promise है, इसलिए page async होना चाहिए और await करना पड़ता है:

JSX – app/courses/page.jsx
// app/courses/page.jsx   →   /courses?level=beginner&sort=fee
export default async function CoursesPage({ searchParams }) {
  const { level = 'all', sort = 'popular' } = await searchParams

  return (
    <main>
      <h1>Courses</h1>
      <p>Showing level: {level} · sorted by: {sort}</p>
    </main>
  )
}
Courses Showing level: beginner · sorted by: fee
💡 searchParams पढ़ने से page हर request पर render होता है (dynamic), क्योंकि result URL पर depend करता है।

Pages Server Components हैं

Default रूप से page server पर चलता है। यह async हो सकता है और files, environment variables या database सीधे पढ़ सकता है — यह code visitor के browser तक कभी नहीं जाता।

JSX – app/blog/page.jsx
// app/blog/page.jsx — a page can be async and read files or a database directly
import { getAllPosts } from '@/lib/posts'

export default async function BlogPage() {
  const posts = getAllPosts()   // runs on the server, never sent to the browser
  return (
    <ul>
      {posts.map((p) => <li key={p.slug}>{p.title}</li>)}
    </ul>
  )
}

आम गलतियाँ

  • app/about.jsx → route नहीं। app/about/page.jsx बनाएँ।
  • Folder नाम में spaces या capitals (About Us) → /About%20Us जैसे बुरे URLs।
  • बिना await के searchParams.level पढ़ना → यह Promise है, इसलिए value undefined।
  • दो folders से एक ही URL बनना (जैसे route groups से) → "two parallel pages" build error।

सारांश

  • app/ के folders URL हिस्से बनते हैं; page.jsx उन्हें public बनाती है।
  • गहरे URLs के लिए folders nest करें; helper files pages के पास रखें।
  • searchParams (Promise) query-string values देता है।
  • Pages Server Components हैं और async हो सकते हैं।
✅ Practice

/courses/python-ai और /courses/android-kotlin pages बनाइए। /courses पर ?level= पढ़िए और मौजूद हो तो "Filter: beginner" दिखाइए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

क्या React Router जैसा router install करना पड़ेगा?
नहीं। Next.js app/ के अंदर का folder structure पढ़कर routes खुद बना देता है।
क्या page file का नाम index.js हो सकता है?
App Router में नहीं। File का नाम page.js (या .jsx/.tsx) होना चाहिए। URL folder के नाम से बनता है।
क्या URLs case-sensitive होते हैं?
हाँ। Hyphen वाले lowercase folder नाम रखें, जैसे app/about-us/page.js → /about-us। SEO के लिए भी यही सबसे अच्छा है।
searchParams को await क्यों करते हैं?
Next.js 15 और 16 में searchParams और params Promises हैं। Values पढ़ने से पहले await करना ज़रूरी है।
← Next.js Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicLayouts और Templates आपकी प्रगति: 9 / 37 (24%) · Lesson 10 / 37 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें