Node.js Project Structure
A scalable and maintainable folder structure for Node.js applications
File Structure
- src
- index.js
- app.js
- controllers
- userController.js
- authController.js
- postController.js
- routes
- index.js
- users.js
- auth.js
- posts.js
- models
- User.js
- Post.js
- Comment.js
- middleware
- auth.js
- validation.js
- errorHandler.js
- rateLimiter.js
- services
- userService.js
- emailService.js
- authService.js
- utils
- database.js
- helpers.js
- logger.js
- validation.js
- config
- database.js
- env.js
- cors.js
- tests
- unit
- integration
- setup.js
- docs
- api.md
- .env
- .env.example
- package.json
- .gitignore
- README.md
Directory Structure Explanation
src/services/
Contains service layer that handles complex business logic separate from controllers.
services/userService.js
Service handling user-related business logic and data operations.
services/emailService.js
Service for handling email sending and email-related operations.
middleware/errorHandler.js
Global error handling middleware for catching and processing errors.
middleware/rateLimiter.js
Rate limiting middleware to prevent abuse and control API usage.
utils/logger.js
Logging utility for tracking application events and debugging.
tests/
Contains unit and integration tests for the application.
docs/
Contains API documentation and other project documentation.