How abjabja/quickstart-mern-back-end works
Project type and purpose
• This is a backend-only boilerplate for a MERN stack application (MongoDB, Express, Node.js).
• Its goal is to provide a quickstart template with modular user authentication (separating auth_user vs. app_user data), JWT-based sessions, validation, and a clean folder structure.
High-level architecture and components
• Client (frontend or API consumer)
• Sends HTTP requests to the Express server (e.g. register, login, hello world test).
• Express Server (server.js)
• Entry point. Loads environment variables, connects to MongoDB, wires up middleware and routes, and starts listening on PORT.
• Middleware Layer
• authMiddleware.js – parses and verifies JWT, attaches user context.
• error-handler.js – catches thrown errors and formats responses.
• not-found.js – handles unmatched routes.
• Routing Layer (routes/…)
• authRoutes.js – /api/v1/auth/* endpoints.
• helloWorldRouter.js – /api/v1/hello test endpoint.
• Controller Layer (controllers/…)
• authController.js – orchestrates register/login flows (calls authService).
• helloWorldController.js – simple GET handler.
• Service Layer (services/authService.js)
• Contains core business logic for registration, login, password hashing (bcryptjs), JWT issuing (jsonwebtoken), and validation (validator).
• Data Access Layer (model/User.js + db/connect.js)
• connect.js sets up Mongoose connection to MongoDB URI.
• User.js defines a Mongoose schema with two sub-schemas:
– auth_user: username, email, phone, hashed password
– app_user: freeform, customizable per application
• Error Definitions (errors/…)
• Custom Error classes (BadRequest, NotFound, Unauthenticated, etc.) to standardize error throwing and handling.
Interactions and data flow
1. Client → HTTP Request → Express (server.js)
2. Request → Middleware chain:
• Authentication (if endpoint requires JWT)
• Validation (e.g., input format checks)
• Error handler catch-all
3. Middleware → Router dispatch → Controller
4. Controller calls Service → Service uses Model to query/update DB
5. Service returns domain object or throws custom error
6. Controller → sends HTTP response → Client
Architectural patterns and principles
• Layered (Onion) Architecture: clear separation of concerns (Routing, Controllers, Services, Models)
• Modular User Model: two sub-schemas in one User model for auth vs. app data
• Middleware Pipeline: centralized error handling, JWT parsing
• Dependency Injection (via imports): controllers import services; services import models
• Client / API Consumer
• Express Server
• Middleware Layer
• Router Layer
• Controller Layer
• Service Layer
• Data Access Layer (Mongoose)
• MongoDB Database
• External Dependencies (bcryptjs, jsonwebtoken, validator)
c. Patterns:
• Modular user schema sub-components
d. Technologies & libraries:
• Express.js, Mongoose, bcryptjs, jsonwebtoken, validator, Node.js