Backend Mean
Purpose
Implement Node.js + Express + MongoDB backends for MEAN stack applications. Translate database schemas and API contracts from architects into working server code: Mongoose models, Express routes, controllers, middleware, authentication, validation, and error handling.
Key Patterns
Project Structure
- server/config/ — DB connection, env validation, CORS config
- server/models/ — Mongoose model files
- server/routes/ — Express router files with index.js mount
- server/controllers/ — Business logic handlers
- server/middleware/ — auth.js, validate.js, errorHandler.js, asyncHandler.js
- server/utils/ — ApiError.js, ApiResponse.js, validators/
Model Pattern
- Mongoose schema with fields, types, validators, required, enum, match
- timestamps: true on every schema
- toJSON transform removing __v and converting _id to id
- Pre-save middleware for password hashing (bcrypt, 12 rounds)
- Instance methods: comparePassword for auth
- select: false on password field
Controller Pattern
- Every handler wrapped with asyncHandler (no try/catch needed)
- ApiError class for throwing errors with status codes
- All list endpoints: pagination with page, limit, total, pages
- Standard response: { success: true, data: {...} }
- Promise.all for parallel queries (count + find)
Auth Pattern
- JWT signed with { id: user._id }, expiry 7d
- protect middleware: extract token from Authorization header, verify, attach user to req
- authorize middleware: check req.user.role against allowed roles
- Refresh token: separate longer-lived token (30d)
Error Handler Pattern
- Global error middleware as last Express middleware
- Handles: Mongoose ValidationError (422), CastError (400), duplicate key 11000 (409)
- Stack trace only in development mode
Conventions
- async/await everywhere with asyncHandler wrapper
- Custom ApiError class — throw instead of res.status().json()
- Environment variables in .env — validate on startup
- Passwords: bcryptjs with 12 salt rounds, select: false, never return in responses
- Use express-rate-limit on auth routes
- Use helmet for security headers, cors for cross-origin, morgan for logging
- NEVER return stack traces in production
- NEVER hardcode secrets
Knowledge Strategy
- Patterns to capture: Working controller patterns, middleware chains, auth implementations
- Examples to collect: Complete server setup files, model definitions, route files
- Update permission: Agents may freely add/update files in
references/. Changes to SKILL.md or scripts/ require user approval.
1---2name: backend-mean3description: Node.js + Express + MongoDB backend development for MEAN stack apps. Use when implementing server code: models, routes, controllers, middleware, config, and database connections.4---56# Backend Mean78## Purpose910Implement Node.js + Express + MongoDB backends for MEAN stack applications. Translate database schemas and API contracts from architects into working server code: Mongoose models, Express routes, controllers, middleware, authentication, validation, and error handling.1112## Key Patterns1314### Project Structure15- server/config/ — DB connection, env validation, CORS config16- server/models/ — Mongoose model files17- server/routes/ — Express router files with index.js mount18- server/controllers/ — Business logic handlers19- server/middleware/ — auth.js, validate.js, errorHandler.js, asyncHandler.js20- server/utils/ — ApiError.js, ApiResponse.js, validators/2122### Model Pattern23- Mongoose schema with fields, types, validators, required, enum, match24- timestamps: true on every schema25- toJSON transform removing __v and converting _id to id26- Pre-save middleware for password hashing (bcrypt, 12 rounds)27- Instance methods: comparePassword for auth28- select: false on password field2930### Controller Pattern31- Every handler wrapped with asyncHandler (no try/catch needed)32- ApiError class for throwing errors with status codes33- All list endpoints: pagination with page, limit, total, pages34- Standard response: { success: true, data: {...} }35- Promise.all for parallel queries (count + find)3637### Auth Pattern38- JWT signed with { id: user._id }, expiry 7d39- protect middleware: extract token from Authorization header, verify, attach user to req40- authorize middleware: check req.user.role against allowed roles41- Refresh token: separate longer-lived token (30d)4243### Error Handler Pattern44- Global error middleware as last Express middleware45- Handles: Mongoose ValidationError (422), CastError (400), duplicate key 11000 (409)46- Stack trace only in development mode4748## Conventions4950- async/await everywhere with asyncHandler wrapper51- Custom ApiError class — throw instead of res.status().json()52- Environment variables in .env — validate on startup53- Passwords: bcryptjs with 12 salt rounds, select: false, never return in responses54- Use express-rate-limit on auth routes55- Use helmet for security headers, cors for cross-origin, morgan for logging56- NEVER return stack traces in production57- NEVER hardcode secrets5859## Knowledge Strategy6061- **Patterns to capture:** Working controller patterns, middleware chains, auth implementations62- **Examples to collect:** Complete server setup files, model definitions, route files63- **Update permission:** Agents may freely add/update files in `references/`. Changes to `SKILL.md` or `scripts/` require user approval.