100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Node.js

Express Application Structure

Learn how to organize a growing Express project into routes, controllers, middleware, and config folders.

Express FundamentalsIntermediate10 min readJul 8, 2026
Analogies

Introduction

A single-file Express app works fine for small demos, but as an application grows to dozens of routes and features, keeping everything in one file becomes unmanageable. A well-structured Express project separates concerns into distinct folders: routes (URL-to-handler mapping), controllers (business logic), middleware (cross-cutting request processing), models (data access), and config (environment and app setup). This structure improves readability, testability, and team collaboration.

🏏

Cricket analogy: A single net-practice session works for a beginner, but a national team splits duties across batting coach, bowling coach, fielding coach, and analyst rather than one person doing everything.

Syntax

javascript
project/
  src/
    app.js              // Express app configuration
    server.js           // Entry point, calls app.listen()
    routes/
      userRoutes.js
      orderRoutes.js
    controllers/
      userController.js
      orderController.js
    middleware/
      errorHandler.js
      authMiddleware.js
    models/
      userModel.js
    config/
      db.js
  package.json

Explanation

app.js typically creates the Express instance, registers global middleware (like express.json() and logging), mounts routers with app.use('/api/users', userRoutes), and registers the final error-handling middleware. server.js imports the configured app and calls app.listen(), keeping startup logic separate from app configuration — this separation makes the app easier to test, since test frameworks can import app.js directly without starting a real server. Routers (built with express.Router()) map URLs to controller functions. Controllers contain the actual request-handling logic, often delegating data access to model files. This layered approach follows separation of concerns: routes decide 'what URL maps to what', controllers decide 'what to do', and models decide 'how to access data'.

🏏

Cricket analogy: app.js is like the team's official team sheet naming the playing eleven and formation, while server.js is the umpire's coin toss that actually starts the match using that pre-set lineup.

Example

javascript
// routes/userRoutes.js
const express = require('express');
const router = express.Router();
const userController = require('../controllers/userController');

router.get('/', userController.listUsers);
router.post('/', userController.createUser);

module.exports = router;

// app.js
const express = require('express');
const userRoutes = require('./routes/userRoutes');
const errorHandler = require('./middleware/errorHandler');

const app = express();
app.use(express.json());
app.use('/api/users', userRoutes);
app.use(errorHandler);

module.exports = app;

// server.js
const app = require('./app');
app.listen(3000, () => console.log('Server started on port 3000'));

Output

Running node server.js starts the server and logs 'Server started on port 3000'. A GET request to /api/users is routed through userRoutes.js to userController.listUsers, keeping the app.js file focused purely on wiring rather than business logic.

🏏

Cricket analogy: It's like the umpire calling 'play' (server starts, logs ready) and then a specific delivery to a specific batsman being routed through the bowler's run-up to the exact shot outcome, keeping the umpire's role separate from execution.

Key Takeaways

  • Separate app configuration (app.js) from server startup (server.js) for easier testing.
  • Use express.Router() to split routes into feature-specific files (e.g. userRoutes.js).
  • Keep business logic in controllers, not inline in route definitions.
  • Centralize error-handling and auth middleware in a dedicated middleware folder.
  • This layered structure scales better and is easier for teams to navigate than a single monolithic file.

Practice what you learned

Was this page helpful?

Topics covered

#NodeJs#NodeJsExpressStudyNotes#WebDevelopment#ExpressApplicationStructure#Express#Application#Structure#Syntax#StudyNotes#SkillVeris