Stop 04 · REST API Design with ExpressTopics · 2/9

MVC Folder Structure

Part of the MERN Stack Roadmap

In short

A 50-line server.js is fine. A 1,500-line one is a nightmare. Splitting your Express app into routes, controllers, services and models means you always know where code lives.

Splitting code into routes, controllers, models and services keeps a growing Express app readable: routes map URLs, controllers handle requests and models talk to MongoDB.

The jaadu stands at a hotel reception desk, pointing a guest toward one of several neatly labelled doors along a corridor.

Think of it like this

A well-run hotel

The reception desk (routes) only directs guests to the right department. The department manager (controller) understands the request and replies. Specialists in the back (services) do the real work, and the records room (models) keeps the data. Nobody does everyone else's job.

Watch it happen

A typical Express project layout

src/
app.js
routes/
product.routes.js
controllers/
product.controller.js
services/
product.service.js
models/
product.model.js
middleware/

1/5app.js creates Express, adds middleware and mounts routers. server.js just imports it and calls listen.

The big ideas

What you need to know

Routes are thin

A route file is a list: router.get("/", controller.list). If you see logic there, move it.

Controllers speak HTTP

They read params, query and body, pick a status code and send JSON. That's all.

Services hold the logic

Business rules live in plain functions, so you can test them without starting a server.

Group by feature

As apps grow, some teams use features/products/ with routes, controller and model together. Both styles are fine if you're consistent.

See it in code

Walk through the code

js
1// src/routes/product.routes.js2import { Router } from "express";3import * as products from "../controllers/product.controller.js";4 5const router = Router();6router.get("/", products.list);7router.post("/", products.create);8export default router;9 10// src/controllers/product.controller.js11import * as productService from "../services/product.service.js";12 13export async function list(req, res) {14  const items = await productService.findAll(req.query);15  res.json(items);16}17 18export async function create(req, res) {19  const product = await productService.create(req.body);20  res.status(201).json(product);21}22 23// src/app.js24// app.use("/api/products", productRoutes);

1/4The router only wires URLs to controller functions.

Each layer does one job. The controller never builds database queries, and the route never sends responses.

Save yourself the pain

Common mistakes

Oops

Writing database queries directly in route files.

Do this instead

Routes call controllers, controllers call services, services use models. Each file stays short and testable.

Oops

Passing req and res into service functions.

Do this instead

Pass plain values like req.body or an id. Services should not depend on Express.

Your mission

Refactor a single-file API

~35 min

Quick check

Where should the rule "orders above Rs 999 get free delivery" live?

Go deeper

  1. 1Reorganise your todo API into routes, controllers and models folders.
  2. 2Keep controllers thin and move reusable business logic into services.
  3. 3Add a config folder for the database connection and environment settings.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge