What Is Full Stack Next.js?
By the end of this lesson you will be able to explain how a single Next.js project can be the front end, the back end and the database layer of an app at the same time. You will know the five places your code can run, trace one request from a click to a database row, and decide when one Next.js codebase is a good fit and when a separate API is better. You will also meet EventHub, the meetup listing app we build in this course.
What you should know first: React components and props (React Introduction), basic TypeScript (TypeScript Introduction) and what a SQL table is (PostgreSQL Introduction). If Next.js itself is new to you, read Next.js Introduction and HTML Integration with Next.js first. This course does not re-teach those tools. It shows how they fit together in one real app.
One codebase, three jobs
A full stack app has three layers. The front end is what people see and click in the browser. The back end is code on a server that checks requests, applies business rules and keeps secrets. The database stores the data permanently.
In MERN or PERN, the front end and back end are two projects: a React app talks over HTTP to an Express API, as in What Is the PERN Stack?. Next.js joins the first two layers. One project holds your pages and your server code, and some components can read the database directly.
Think of a dhaba on the Delhi to Jaipur highway. Kitchen and counter are under one roof, so a waiter walks ten steps to the cook. A restaurant that orders from a separate cloud kitchen needs phone calls and order slips, but that kitchen can also serve other customers. Next.js is the dhaba; MERN and PERN are the cloud kitchen.
The five places your code runs
In a full stack Next.js app every file has a home. Learning these homes is the most important skill in this course, because they decide what is safe and what is public.
| Piece | How you mark it | Runs on | Typical job in EventHub |
|---|---|---|---|
| Server Component | the default, no directive | server only | Read events from the database and render HTML |
| Client Component | "use client" at the top of the file | server for the first HTML, then the browser | A filter box or share button that reacts to clicks |
| Server Action | "use server" on an async function | server, called from a form or button | Save an RSVP, create a new event |
| Route Handler | a route.ts file inside app/ | server | A JSON API at /api/events for a mobile app or a webhook |
| Proxy | a proxy.ts file at the project root | server, before matching routes | A quick redirect for logged-out users |
A Server Component's code never reaches the browser; only its output is sent. A Client Component has state and event handlers, and its JavaScript runs in the browser. The rules are in Server & Client Components. Here is the kind of Server Component you will write in this course:
// app/events/[slug]/page.tsx: a Server Component
import { getEventBySlug } from "@/lib/events";
export default async function EventPage({ params }: PageProps<"/events/[slug]">) {
const { slug } = await params;
const event = await getEventBySlug(slug); // runs on the server, next to the database
return <h1>{event?.title}</h1>;
}Below the five pieces sits server-only code in a lib folder, such as getEventBySlug. It uses Prisma, a database toolkit that turns a schema file into a typed client, set up in Database with Prisma & PostgreSQL.
One request, start to finish
Here is what happens when Ananya in Rohtak opens a meetup page and taps "RSVP":
Browser Next.js server PostgreSQL
------- -------------- ----------
1. GET /events/rohtak-react-meetup
----> 2. app/events/[slug]/page.tsx runs
3. calls getEventBySlug() in lib/events.ts
Prisma sends one parameterised query ----> 4. finds the row
5. React renders HTML <---- returns the event
6. HTML arrives, page shows <---- (streamed as it is ready)
7. Client Components hydrate
(the RSVP button becomes clickable)
8. Tap "RSVP" ----> 9. Server Action rsvpToEvent() runs
checks login and input, writes ----> 10. INSERT rsvp row
11. Updated count shown <---- 12. revalidates the pageNotice three things. The browser never talks to PostgreSQL. The database password is used only in steps 3 and 9, on the server. And hydration (step 7) means React in the browser attaches click handlers to HTML that the server already sent, so the page is readable before the JavaScript finishes loading.
Same-origin, so less plumbing
Because the pages and the server code come from the same address, many problems of a two-project stack disappear:
| Concern | React + separate API (MERN, PERN) | Full stack Next.js |
|---|---|---|
| Projects to build and deploy | two | one |
| CORS setup | needed, with an allow-list of front-end origins | not needed for your own pages |
| Sharing TypeScript types | copy them or build a shared package | import the same file |
| Login cookie | must work across two origins | one origin, an HttpOnly cookie just works |
| Fetching data for a page | useEffect or a query library, then a loading spinner | await in a Server Component, HTML arrives filled |
CORS (Cross-Origin Resource Sharing) is the browser rule that blocks a page on one origin from reading responses from another origin unless the server allows it. An HttpOnly cookie is a cookie that JavaScript in the page cannot read, which protects the login session from stolen-script attacks.
A separate API is still better when mobile apps and partners all need the same stable API, or for long-running jobs and WebSocket chat. Even then, many teams keep Next.js for the website and add Route Handlers for the few endpoints others need.
The security line runs through every file
In one codebase it is easy to forget which side of the line you are on. Remember these rules from day one:
- Everything a Client Component imports can end up in the browser, readable in DevTools.
- Environment variables are server-only unless named
NEXT_PUBLIC_.... Never give a secret that prefix. See Environment Variables. - Server Actions and Route Handlers are public endpoints. Check who the user is and validate input on the server.
Meet EventHub
EventHub lists tech meetups in Indian cities, such as a React meetup in Rohtak, and lets people RSVP.
| Route | What it shows | Lesson that builds it |
|---|---|---|
/ | Home page with the next few meetups | App Router Structure |
/events | All upcoming meetups, filterable by city | App Router Structure |
/events/[slug] | One meetup with venue, time and RSVP count | App Router Structure |
/events/new | Form for organisers to add a meetup | Server Actions & Forms |
/api/events | JSON list for other apps | Route Handlers (API Routes) |
/login, /dashboard | Sign in and see your RSVPs | Authentication with Auth.js |
The data model has three tables: User (members and organisers), Event (a meetup with a title, city, venue, start time, capacity and an optional ticket price in paise) and Rsvp (one row per person per event). Storing money as whole paise (₹1 = 100 paise) avoids rounding errors that decimals cause in JavaScript.
Try it: what the browser actually runs
In EventHub, the server reads the event and passes plain data to a small Client Component. This example plays both roles in the browser so you can see the split. EventDetails stands for a Server Component: it only formats data. RsvpButton is the Client Component with state:
import { useState } from "react";
import { createRoot } from "react-dom/client";
// Plain data, as the server would send it after reading PostgreSQL
const event = {
slug: "rohtak-react-meetup",
title: "Rohtak React Meetup",
city: "Rohtak",
startsAt: "2026-12-05T11:00:00.000Z",
goingCount: 41,
capacity: 60,
};
const when = new Intl.DateTimeFormat("en-IN", {
dateStyle: "medium",
timeStyle: "short",
timeZone: "Asia/Kolkata",
});
// Stands in for a Server Component: no state, no handlers
function EventDetails({ event, children }) {
return (
<article>
<h2>{event.title}</h2>
<p>{event.city}, {when.format(new Date(event.startsAt))}</p>
{children}
</article>
);
}
// A Client Component: needs state and a click handler
function RsvpButton({ initialCount, capacity }) {
const [going, setGoing] = useState(false);
const count = initialCount + (going ? 1 : 0);
return (
<button onClick={() => setGoing(!going)}>
{going ? "Cancel RSVP" : "RSVP"} ({count} of {capacity} going)
</button>
);
}
console.log("Props sent to the browser:", JSON.stringify({ initialCount: event.goingCount, capacity: event.capacity }));
createRoot(document.getElementById("root")).render(
<EventDetails event={event}>
<RsvpButton initialCount={event.goingCount} capacity={event.capacity} />
</EventDetails>
);Rohtak React Meetup
Rohtak, 5 Dec 2026, 4:30 pm
RSVP (41 of 60 going)
Console:
Props sent to the browser: {"initialCount":41,"capacity":60}The start time is stored in UTC (11:00Z) and shown in Indian time (4:30 pm), as EventHub will do everywhere. Clicking the button changes only the count; in the real app it will call a Server Action. Only the two numbers in the console line cross into the browser.
Common mistakes
- Treating a Server Action as private. Anyone can call it with their own request. Check the session and validate input inside it.
- Putting
"use client"at the top of a page "to make things work". The whole subtree then ships to the browser and loses direct database access. Push the directive down to the small interactive part. - Reading the database from a Client Component. It cannot work, and trying usually means a secret is heading for the browser.
- Building a separate Express API by habit. For your own pages, Server Components and Server Actions remove the extra hop. Add Route Handlers only for real outside clients.
- Storing rupees as floating-point numbers.
0.1 + 0.2is not0.3in JavaScript. Store paise as integers.
Interview questions
1. What makes Next.js a full stack framework rather than only a front-end framework?
It runs code on the server as well as in the browser. Server Components can read a database while rendering, Server Actions handle form submissions and data changes, and Route Handlers serve HTTP endpoints. All of these live in the same project as the UI and deploy together.
2. When would you still build a separate back-end API next to Next.js?
When several different clients, such as mobile apps or partner systems, need the same stable API, or when work is long-running or needs persistent connections, such as video processing or WebSocket chat. Team structure can also be a reason. Even then, Route Handlers in Next.js can cover a small number of public endpoints.
3. Is a Server Action safe because it is not exposed as a visible API route?
No. A Server Action is reachable with a direct POST request, not only through your own form, so anyone can call it with arguments they choose. The action must check authentication and authorisation and validate its arguments on the server, exactly like any API endpoint.
Key points
- Full stack Next.js puts the UI, server logic and database access in one project and one deployment.
- Server Components, Client Components, Server Actions, Route Handlers and Proxy each run in a known place; learn which is which.
- The browser never talks to the database. Secrets stay in server-only code and non-
NEXT_PUBLIC_environment variables. - Server Actions and Route Handlers are public endpoints: authenticate and validate inside them.
- Choose a separate API when many different clients or long-running, real-time work need it.
- EventHub has users, events and RSVPs, stores times in UTC and money in paise.
Exercise
Meera is planning a "Kochi PostgreSQL Evening" feature list for EventHub. For each item, say which piece should do it (Server Component, Client Component, Server Action, Route Handler or Proxy) and give one reason:
- Show the list of upcoming events in Kochi.
- A "Show only free events" checkbox that filters the list instantly.
- Save a new RSVP.
- Let a partner college's Android app fetch events as JSON.
- Send logged-out visitors from
/dashboardto/loginquickly.
Show solution
- Server Component. It can query the database while rendering and send finished HTML with no extra JavaScript.
- Client Component. It needs state and an
onChangehandler, which only run in the browser. The server can pass it the full list as props. - Server Action. It changes data, runs on the server and can be called straight from a form. It must check the session and validate the event id.
- Route Handler at
app/api/events/route.ts. An outside app needs a stable HTTP URL that returns JSON. - Proxy. It runs before the route and can redirect early. The dashboard page must still check the session itself, because Proxy is only a fast first check.
Quick Quiz
1. Where does the code of a Server Component run?
Server Components never ship their code to the browser, which is why they can use secrets and query the database.
2. Which environment variable could be read by anyone who opens your site?
Only variables with the NEXT_PUBLIC_ prefix are inlined into browser JavaScript.
3. A Server Action is only used by your own RSVP form. What must it still do?
A Server Action can be called with a direct POST request, so it needs the same checks as any API.
4. Why does EventHub store a ticket price of ₹149 as 14900?
Integers are exact; decimal fractions such as 0.1 are not exact in JavaScript numbers.
Learn Next.js Full Stack with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
