What Is the MERN Stack?
By the end of this lesson you will be able to explain what each part of the MERN stack does, follow one request from a button click to the database and back, and say when MERN is a good choice for a project. You will also meet TaskBoard, the app you will build across this course.
What you should know first: basic HTML, CSS and JavaScript, including Async/Await, Fetch API and JSON. You do not need to know React, Node.js or MongoDB yet. Whenever this course needs a basic idea from one of them, it links to the full lesson in the React, Node.js or MongoDB course, and spends its own time on how the parts fit together.
Four parts, one language
A stack is a set of tools that work together to make one app. MERN takes its name from its four parts:
| Letter | Tool | Its job in the app | Where it runs | Version in this course |
|---|---|---|---|---|
| M | MongoDB | Stores the data as documents | Database server | 9.0 |
| E | Express | Receives HTTP requests and sends responses | Node.js on the server | 5.3 |
| R | React | Draws the screens the user sees and clicks | The user's browser | 19.3 |
| N | Node.js | Runs JavaScript outside the browser | Server, and your laptop for tools | 26 LTS |
Think of a coaching centre in Rohtak. Students talk to the reception desk: that is React, the part people see. The receptionist passes each request to the office clerk, who checks it against the rules and decides what to do: that is Express. The clerk works inside the office building with its electricity and staff, which is Node.js. The register cupboard at the back keeps every record: that is MongoDB.
Two ideas make MERN popular.
- One language everywhere. React, Express and Node.js all use JavaScript, and MongoDB is queried with JavaScript objects. A student who knows JavaScript well can work on every part.
- JSON all the way through. JSON (JavaScript Object Notation) is a text format for data, such as
{"title": "Print worksheets"}. React sends JSON, Express reads and returns JSON, and MongoDB stores documents that look almost the same. Very little converting is needed between layers.
The client and the server
A MERN app has two programs that run in different places.
The client (also called the front end) is the React app. Its code is downloaded and runs inside each user's browser, on their phone or laptop.
The server (also called the back end or the API) is the Express app running on Node.js. It runs on a computer you control. Only the server talks to MongoDB.
An API (Application Programming Interface) is a list of requests a program accepts. Ours is a REST API: each URL names a thing, such as /api/tasks, and the HTTP method says what to do with it (GET to read, POST to create). You will design it properly in Build a REST API with Express 5.
Why not let React talk to MongoDB directly? Because everything in the browser is visible and editable by the user. If the browser held the database password, any student could open DevTools, copy it and delete every record. So the rule is simple: the browser is never trusted. The server keeps the secrets, checks every request and decides what data each user may see.
Follow one request through the stack
Here is what happens when Meera, a teacher, opens TaskBoard and sees her tasks.
Step 1: React asks for data. When the task list component appears, it calls the API:
const res = await fetch("/api/tasks?status=todo");
const tasks = await res.json();Step 2: Express matches a route. The server has a function for GET /api/tasks. Express finds it and calls it with the request.
tasksRouter.get("/", listTasks);Step 3: Mongoose asks MongoDB. Mongoose is a library that lets Node.js code read and write MongoDB through models. The function asks for tasks with the right status, sorted by due date:
const tasks = await Task.find({ status: "todo" }).sort({ dueDate: 1 });Step 4: Express sends JSON back. res.json(tasks) turns the documents into JSON text and sends it with status 200 OK:
[
{
"id": "6afa8c70c0a73c0001de6e3d",
"title": "Print Class 10 maths worksheets",
"status": "todo",
"dueDate": "2026-11-20T00:00:00.000Z",
"assignee": { "id": "6afa8c70c0a73c0001de6e3b", "name": "Priya Sharma" }
}
]Step 5: React draws the list. React stores the array in state and renders one row for each task. The user sees the screen update without a full page reload.
That round trip, browser to Express to MongoDB and back, is the heartbeat of every MERN app. Most bugs you will meet in this course happen at one of the joins: a wrong URL, a missing field in the JSON, or a date that arrives as text.
You can try Step 5 right now. The example below loads the JSON above from a data: URL (data written inside the URL itself) instead of a real server, then renders it the way the TaskBoard client will. Notice that dueDate arrives as a string, so the component turns it into a Date before formatting it for India.
import { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
const sample = [
{ id: "t1", title: "Print Class 10 maths worksheets", status: "todo",
dueDate: "2026-11-20T00:00:00.000Z", assignee: { id: "u1", name: "Priya Sharma" } },
{ id: "t2", title: "Call parents about Sunday test", status: "doing",
dueDate: "2026-11-18T00:00:00.000Z", assignee: { id: "u2", name: "Kabir Malik" } },
{ id: "t3", title: "Update physics batch timings", status: "done",
dueDate: "2026-11-12T00:00:00.000Z", assignee: null },
];
// Pretend server: the JSON lives inside the URL.
const API_URL = "data:application/json," + encodeURIComponent(JSON.stringify(sample));
const dateFormat = new Intl.DateTimeFormat("en-IN", { day: "numeric", month: "short", timeZone: "UTC" });
function TaskList() {
const [tasks, setTasks] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
fetch(API_URL)
.then((res) => res.json())
.then((data) => setTasks(data))
.finally(() => setLoading(false));
}, []);
if (loading) return <p>Loading tasks...</p>;
return (
<ul>
{tasks.map((task) => (
<li key={task.id}>
<strong>{task.title}</strong> ({task.status}), due{" "}
{dateFormat.format(new Date(task.dueDate))}, {task.assignee ? task.assignee.name : "unassigned"}
</li>
))}
</ul>
);
}
createRoot(document.getElementById("root")).render(<TaskList />);The list shows "due 20 Nov", "due 18 Nov" and "due 12 Nov", and the third task says "unassigned" because its assignee is null. Change one status in sample and run it again to see the row change.
The app you will build: TaskBoard
TaskBoard is a task tracker for the staff of a coaching centre in Rohtak. It has users (teachers and an admin) and tasks. Each task has a title, a status (todo, doing or done), a due date and an assignee. Every non-project lesson adds one piece:
| Course section | What TaskBoard gains |
|---|---|
| Getting Started | Tools installed, one project folder with client/ and server/ |
| Back End | Express API, MongoDB models, full CRUD, error handling, login with JWT |
| Front End | React screens with Vite, data fetching, routing, forms, protected pages |
| Production | File uploads, tests, a security checklist, deployment |
The last three lessons are separate, bigger projects: a team task manager, a blog with comments and a mini online store.
When MERN fits, and when it does not
MERN is a good choice when:
- your team already knows JavaScript well;
- the data is shaped like documents, such as a task with its details, or a product with varied specifications;
- the app is interactive, with many screens that update without reloading;
- you want to build an API that a mobile app can also use later.
Think twice when:
- the data is highly relational, with many tables joined in every query and strict money rules, such as a fees ledger. A SQL database such as PostgreSQL may fit better; see the PERN stack course.
- search engines must read every page at first load, as on a news site. Plain React draws pages in the browser. A framework such as Next.js renders HTML on the server; see Full Stack Next.js.
- the site is mostly static content, such as a brochure for a Kochi bakery. A simple static site is cheaper and faster.
The neighbouring stacks swap one letter. MEAN uses Angular instead of React, MEVN uses Vue, and PERN uses PostgreSQL instead of MongoDB. What you learn here about APIs, environment variables, CORS and login carries over to all of them.
Common mistakes
- Putting secrets in React code. API keys and database passwords in the client are visible to everyone. Keep them on the server.
- Trusting the browser's checks. A form that blocks an empty title can be bypassed with one
curlcommand. The server must check again. - Thinking MERN means MongoDB for everything. Choose the database for the shape of the data, not for the name of the stack.
- Mixing up the two programs.
fetchruns in the browser;Task.find()runs on the server. Code for one cannot simply be copied to the other.
Interview questions
1. What does each letter of MERN stand for, and where does each part run?
MongoDB is the document database, running on a database server. Express is the web framework that handles HTTP requests, and it runs on Node.js on the server. React builds the user interface and runs in the browser. Node.js is the JavaScript runtime for the server and for development tools.
2. Why can a React app not connect to MongoDB directly?
All client code is downloaded to the user's browser, so any database password in it would be public. The browser is also not trusted, so the server must check permissions and validate input before touching data. Express sits in between as the only program that holds the credentials.
3. What is the advantage of using JSON across the whole stack?
React, Express and MongoDB all work naturally with JavaScript objects, and JSON is their text form. Data moves between layers with little or no conversion. Dates are the main exception, because JSON has no date type and sends them as strings.
4. When would you choose a SQL database instead of MongoDB?
When the data is strongly relational with many joins, or needs strict multi-table rules, such as accounting entries. MongoDB has transactions and $lookup, but a relational design is often simpler for that kind of data. Many teams use a PERN stack in those cases.
Key points
- MERN is MongoDB (data), Express (API), React (UI in the browser) and Node.js (the server runtime).
- The client runs in the browser and is never trusted; only the server holds secrets and talks to the database.
- A request goes React
fetchto an Express route to a Mongoose query and comes back as JSON. - JSON flows through every layer, but dates arrive as strings and must be converted.
- MERN fits interactive apps with document-shaped data; strongly relational data may suit PERN better.
- Throughout this course you build TaskBoard, a task tracker for a Rohtak coaching centre.
Exercise
Ananya runs a tuition centre in Pune and wants an app where parents see their child's test marks. Write a short plan with three parts: (1) what React, Express, MongoDB and Node.js will each do in her app, (2) the steps of one request when a parent opens the "Marks" screen, and (3) one piece of data that must never be sent to the browser, with the reason.
Show solution
- React shows the login and "Marks" screens in the parent's browser. Express on Node.js receives requests such as
GET /api/students/:id/marks, checks that the logged-in parent belongs to that student, and returns JSON. MongoDB stores students, parents and marks. - The Marks screen calls
fetch("/api/students/s42/marks"), Express matches the route and checks the login, Mongoose runsMark.find({ student: "s42" }), Express sends the marks as JSON, and React renders a table. - The database connection string, because it holds the database password. Other children's marks must also never be sent, so the server filters by the parent's own child before replying.
// Express: only this parent's child, never everyone's marks
const marks = await Mark.find({ student: req.params.id }).sort({ date: -1 });
res.json(marks);Quick Quiz
1. In a MERN app, which part runs in the user's browser?
React code is downloaded and runs in the browser; the other parts run on servers you control.
2. Why must the server validate input even if the React form already checks it?
Tools such as curl or DevTools can send any request directly to the API, skipping the browser checks.
3. What does res.json(tasks) do in Express?
res.json() serialises the value and sends it with a JSON content type.
4. Which project is the weakest fit for MERN?
Strongly relational data with strict rules is often easier in a SQL database such as PostgreSQL.
Learn MERN 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
