<vetted />
Node.js
Mid-Level
Question 3 of 6

How do you structure a Node.js project as it grows?

Quick Answer

Organize by feature/domain rather than type, separate business logic from framework code, and use clear boundaries between layers.

Detailed Answer8 paragraphs

Good project structure scales with your application and helps developers find and organize code intuitively.

Feature-based organization groups related code together: /features/users/ contains user routes, controllers, services, and models. This beats organizing by type (/controllers/, /models/) because features are self-contained and easier to navigate.

Layer separation keeps concerns clean. Routes/controllers handle HTTP, services contain business logic, repositories handle data access. Business logic shouldn't know about HTTP or specific databases—this makes testing easier and allows swapping infrastructure.

Common structure for medium apps: /src/features/[feature]/{routes, controller, service, repository}, /src/common (shared utilities, middleware), /src/config (environment, constants), /src/infrastructure (database, external services).

Use barrel exports (index.ts) to expose clean public APIs from each module. Internal implementation details stay hidden.

Dependency injection (even simple constructor injection) decouples modules. Pass dependencies in rather than importing them directly. This enables testing with mocks and makes relationships explicit.

Monorepo consideration: for larger projects, separate packages for shared code, multiple services, etc. Tools like Turborepo or Nx manage builds and dependencies across packages.

Start simple and refactor as you learn what your app actually needs. Perfect structure upfront often guesses wrong. Let patterns emerge from real code.

Key Takeaway

Organize by feature/domain rather than type, separate business logic from framework code, and use clear boundaries between layers.

Ace your interview

Ready to Land Your Dream Job?

Join our network of elite AI-native engineers.