How nasiriubat/ems works
Purpose and runtime
This is a Node.js/Express employee-management API backed by MongoDB. It provides JWT-based registration/login plus role-controlled employee CRUD. The server entry point is app.js; runtime configuration is sourced from .env (templated by .env.example).
Application composition
app.js assembles the Express application: request parsing, route mounting, database startup, and Swagger API documentation. It is the primary HTTP/process boundary and exposes documentation at /api-docs.
Paths: app.js, swaggerOptions.js, .env.example
Database and domain model
Mongoose connects the application to MongoDB through the database configuration module. A single user model represents both administrators and employees, including credentials and role assignment; roles are Admin and Employee.
Paths: config/db.js, models/user.js
Authentication flow
Authentication routes accept registration and login requests. The authentication controller creates users with bcrypt-protected passwords and issues JWTs after successful login. JWT verification is handled through the project’s Passport/JWT dependency stack before protected resource access.
Paths: routes/auth.js, controllers/authController.js, models/user.js
Employee resource flow
Employee endpoints are routed to the user controller, which owns list, lookup, create, update, and delete operations against the user collection. The list endpoint supports pagination. Employee records are therefore a role-filtered/use-case-specific view of the shared user domain model rather than a separate employee entity.
Paths: routes/user.js, controllers/userController.js, models/user.js
Authorization boundary
Role middleware enforces the central policy boundary after authentication: administrators can manage employee records, while employees may access/update their own record where permitted. Controllers should remain responsible for resource-level ownership checks implied by /:id operations.
Paths: middleware/roleMiddleware.js, routes/user.js, controllers/userController.js
External services and operational interfaces
MongoDB is the persistent external service, accessed through Mongoose. JWT signing requires a configured secret; database URL and secret are environment-provided. Swagger is the API discovery interface for consumers rather than a separate application service.
Paths: config/db.js, .env.example, swaggerOptions.js
Components 12
Runtime
- Environment · Supplies database URL and JWT secret.
.env.example - Express App · Assembles middleware, routes, database startup, and API docs.
app.js - Swagger Docs · Published by the application at /api-docs.
swaggerOptions.js
HTTP API
- Auth Routes
routes/auth.js - Auth Controller · Registers users and issues JWTs on login.
controllers/authController.js - Passport JWT · Verifies JWTs before protected access.
- Employee Routes
routes/user.js - Role Middleware · Applies Admin and Employee access rules.
middleware/roleMiddleware.js - User Controller · Handles paginated employee CRUD and ownership checks.
controllers/userController.js
Data & Services
- Mongoose Connection · Connects the process to MongoDB.
config/db.js - User Model · Shared administrator and employee domain model.
models/user.js - MongoDB
Connections 16
- Environment → Express App: configures
- Environment → Mongoose Connection: database URL
- Environment → Auth Controller: JWT secret
- Express App → Swagger Docs: serves /api-docs
- Express App → Mongoose Connection: starts connection
- Express App → Auth Routes: mounts
- Express App → Employee Routes: mounts
- Auth Routes → Auth Controller: registration and login
- Auth Controller → User Model: creates and finds users
- Employee Routes → Passport JWT: protects requests
- Passport JWT → Role Middleware: authenticated identity
- Role Middleware → User Controller: authorized access
- Employee Routes → User Controller: employee CRUD
- User Controller → User Model: queries and mutates
- Mongoose Connection → MongoDB: connects
- User Model → MongoDB: persists users