Next.js Tutorial (Beginner Friendly)
Welcome to the complete beginner-friendly Next.js Tutorial! This guide will help you learn Next.js step by step β from installation to building pages, APIs, and deploying your application.
π§© What is Next.js?
Next.js is a React framework for building fast, scalable, SEO-friendly web applications. It includes features like:
- File-based routing
- Server-side rendering (SSR)
- Static site generation (SSG)
- API routes
- Image optimization
- Built-in CSS & file support
π οΈ Step 1 β Install Next.js
You can create a Next.js app using:
1npx create-next-app@latest my-app
2cd my-app
3npm run dev
Now open your browser and go to:
π Step 2 β Project Structure (Next.js 13+ App Router)
1my-app/
2 ββ app/
3 β ββ page.js β Home page
4 β ββ layout.js β Layout file
5 β ββ api/ β API routes
6 ββ public/ β Static files
7 ββ package.json
8 ββ next.config.js
π Step 3 β Create Your First Page
Inside app/page.js:
1export default function Home() {
2 return (
3 Hello Next.js!
4 );
5}
This shows on the home route /.
π§ Step 4 β Create Routes (Pages)
Create a folder inside app:
1export default function About() {
2 return About Page;
3}
Visit: http://localhost:3000/about
π¨ Step 5 β Add Global CSS
In app/globals.css, you can write:
1body {
2 margin: 0;
3 font-family: sans-serif;
4}
Then import it in layout.js:
βοΈ Step 6 β Layout Component
app/layout.js runs on every page.
1export default function RootLayout({ children }) {
2 return (
3
4
5 My Navbar
6 {children}
7
8
9 );
10}
π§΅ Step 7 β Fetch Data (SSR / SSG)
Server-Side Rendered (default in app router)
1export default async function Home() {
2 const res = await fetch('https://jsonplaceholder.typicode.com/posts');
3 const posts = await res.json();
4
5 return (
6
7 {posts.map(post => (
8 {post.title}
9 ))}
10
11 );
12}
π Step 8 β Create API Routes
Next.js lets you create backend routes.
Create file:
1app/api/hello/route.js
2export function GET() {
3 return Response.json({ message: "Hello from API" });
4}
1http://localhost:3000/api/hello
πΌοΈ Step 9 β Use Image Component
1import Image from 'next/image';
2
3export default function Photo() {
4 return (
5
6 );
7}
π Step 10 β Deploy to Vercel
Deploy easily using:
Or connect your GitHub repository to Vercel.
π Summary
You learned:
- Installing Next.js
- Building pages & routes
- Using layouts
- Fetching data
- Creating APIs
- Deploying to Vercel
You can now start building a full-stack Next.js application.
If you want, I can continue this tutorial with:
- Authentication (NextAuth)
- Prisma + MongoDB
- Dynamic routes
- Middleware
- File uploads
- Admin dashboard
Just tell me! π