@@ -944,22 +944,327 @@ nodejs-backend-boilerplate/
944944
945945# # Available Scripts
946946
947- | Script | Description |
948- | --------| -------------|
949- | ` npm run dev` | Start development server with hot reload |
950- | ` npm start` | Start production server |
951- | ` npm test` | Run tests |
952- | ` npm run test:watch` | Run tests in watch mode |
953- | ` npm run lint` | Check for linting errors |
954- | ` npm run format` | Format code with Prettier |
955- | ` npm run docker:up` | Start Docker containers |
956- | ` npm run docker:down` | Stop Docker containers |
957- | ` npm run docker:build` | Rebuild Docker containers |
958- | ` npm run seed` | Seed database with sample data |
959- | ` npm run migrate:up` | Run database migrations |
960- | ` npm run migrate:down` | Rollback last migration |
961- | ` npm run migrate:create` | Create new migration |
962- | ` npm run prepare` | Set up Husky hooks |
947+ This boilerplate includes a comprehensive set of npm scripts to streamline your development workflow. Below is a detailed explanation of each script:
948+
949+ # ## Development Scripts
950+
951+ # ### `npm run dev`
952+ ** Description:** Starts the development server with automatic hot-reload using Nodemon.
953+
954+ ** When to use:** During active development when you want the server to automatically restart on file changes.
955+
956+ ** Example:**
957+ ` ` ` bash
958+ npm run dev
959+ ` ` `
960+
961+ ** Output:** Server starts on ` http://localhost:3000` with auto-reload enabled.
962+
963+ ---
964+
965+ # ### `npm start`
966+ ** Description:** Starts the production server without hot-reload.
967+
968+ ** When to use:** In production environments or when you want to test the production build locally.
969+
970+ ** Example:**
971+ ` ` ` bash
972+ NODE_ENV=production npm start
973+ ` ` `
974+
975+ ** Note:** Ensure all environment variables are set before running in production.
976+
977+ ---
978+
979+ # ## Testing Scripts
980+
981+ # ### `npm test`
982+ ** Description:** Runs all test suites with coverage reporting using Jest.
983+
984+ ** When to use:** Before committing code, in CI/CD pipelines, or when you want to verify all tests pass.
985+
986+ ** Example:**
987+ ` ` ` bash
988+ npm test
989+ ` ` `
990+
991+ ** What it does:**
992+ - Runs all ` * .test.js` and ` * .spec.js` files
993+ - Generates coverage reports in ` coverage/` directory
994+ - Requires 70% code coverage (configurable in ` jest.config.js` )
995+
996+ ** View coverage report:**
997+ ` ` ` bash
998+ open coverage/lcov-report/index.html # macOS
999+ xdg-open coverage/lcov-report/index.html # Linux
1000+ ` ` `
1001+
1002+ ---
1003+
1004+ # ### `npm run test:watch`
1005+ ** Description:** Runs tests in watch mode, re-running tests when files change.
1006+
1007+ ** When to use:** During test-driven development (TDD) or when actively writing tests.
1008+
1009+ ** Example:**
1010+ ` ` ` bash
1011+ npm run test:watch
1012+ ` ` `
1013+
1014+ ** Features:**
1015+ - Automatically re-runs tests on file changes
1016+ - Interactive mode with options to filter tests
1017+ - Press ` p` to filter by filename pattern
1018+ - Press ` t` to filter by test name pattern
1019+
1020+ ---
1021+
1022+ # ## Code Quality Scripts
1023+
1024+ # ### `npm run lint`
1025+ ** Description:** Checks and automatically fixes code style issues using ESLint.
1026+
1027+ ** When to use:** Before committing code or when you want to enforce coding standards.
1028+
1029+ ** Example:**
1030+ ` ` ` bash
1031+ npm run lint
1032+ ` ` `
1033+
1034+ ** What it checks:**
1035+ - Airbnb JavaScript style guide compliance
1036+ - Import/export statement correctness
1037+ - Potential bugs and code smells
1038+ - Best practices for Node.js and Express
1039+
1040+ ** Configuration:** See ` .eslintrc.json` for rules.
1041+
1042+ ---
1043+
1044+ # ### `npm run format`
1045+ ** Description:** Formats all code files according to Prettier configuration.
1046+
1047+ ** When to use:** Before committing code to ensure consistent formatting across the codebase.
1048+
1049+ ** Example:**
1050+ ` ` ` bash
1051+ npm run format
1052+ ` ` `
1053+
1054+ ** What it formats:**
1055+ - JavaScript/JSON files
1056+ - Markdown documentation
1057+ - YAML configuration files
1058+
1059+ ** Check without formatting:**
1060+ ` ` ` bash
1061+ npm run format -- --check
1062+ ` ` `
1063+
1064+ ** Configuration:** See ` .prettierrc` for formatting rules.
1065+
1066+ ---
1067+
1068+ # ## Docker Scripts
1069+
1070+ # ### `npm run docker:up`
1071+ ** Description:** Starts all Docker containers in detached mode (app, MongoDB, Redis).
1072+
1073+ ** When to use:** When you want to run the entire stack using Docker without installing MongoDB and Redis locally.
1074+
1075+ ** Example:**
1076+ ` ` ` bash
1077+ npm run docker:up
1078+ ` ` `
1079+
1080+ ** What it starts:**
1081+ - Application container on port 3000
1082+ - MongoDB container on port 27017
1083+ - Redis container on port 6379
1084+ - Mongo Express (DB admin UI) on port 8081
1085+
1086+ ** Access services:**
1087+ - API: http://localhost:3000
1088+ - Mongo Express: http://localhost:8081 (admin/pass)
1089+
1090+ ---
1091+
1092+ # ### `npm run docker:down`
1093+ ** Description:** Stops and removes all Docker containers.
1094+
1095+ ** When to use:** When you' re done with Docker development and want to free up resources.
1096+
1097+ **Example:**
1098+ ```bash
1099+ npm run docker:down
1100+ ```
1101+
1102+ **Remove volumes too:**
1103+ ```bash
1104+ npm run docker:down -- -v
1105+ ```
1106+
1107+ ---
1108+
1109+ #### `npm run docker:build`
1110+ **Description:** Rebuilds Docker images from scratch.
1111+
1112+ **When to use:** After changing Dockerfile, package.json dependencies, or when images are corrupted.
1113+
1114+ **Example:**
1115+ ```bash
1116+ npm run docker:build
1117+ ```
1118+
1119+ **Force rebuild without cache:**
1120+ ```bash
1121+ npm run docker:build -- --no-cache
1122+ ```
1123+
1124+ ---
1125+
1126+ ### Database Scripts
1127+
1128+ #### `npm run seed`
1129+ **Description:** Populates the database with sample data for development.
1130+
1131+ **When to use:** After initial setup or when you need fresh test data.
1132+
1133+ **Example:**
1134+ ```bash
1135+ npm run seed
1136+ ```
1137+
1138+ **What it creates:**
1139+ - Admin user (admin@example.com / Admin123!)
1140+ - Sample products with categories
1141+ - Sample orders
1142+
1143+ **Note:** Clears existing data before seeding. Use with caution in production.
1144+
1145+ ---
1146+
1147+ #### `npm run migrate:up`
1148+ **Description:** Runs pending database migrations.
1149+
1150+ **When to use:** After pulling new code with database schema changes or when deploying to production.
1151+
1152+ **Example:**
1153+ ```bash
1154+ npm run migrate:up
1155+ ```
1156+
1157+ **Configuration:** See `migrate-mongo-config.js` and `migrations/` directory.
1158+
1159+ ---
1160+
1161+ #### `npm run migrate:down`
1162+ **Description:** Rolls back the last applied migration.
1163+
1164+ **When to use:** When you need to undo the most recent database migration.
1165+
1166+ **Example:**
1167+ ```bash
1168+ npm run migrate:down
1169+ ```
1170+
1171+ **Warning:** This may result in data loss. Always backup your database first.
1172+
1173+ ---
1174+
1175+ #### `npm run migrate:create <name>`
1176+ **Description:** Creates a new migration file.
1177+
1178+ **When to use:** When you need to make database schema changes.
1179+
1180+ **Example:**
1181+ ```bash
1182+ npm run migrate:create add_user_roles
1183+ ```
1184+
1185+ **Output:** Creates a new file in `migrations/` directory with timestamp.
1186+
1187+ ---
1188+
1189+ ### Git Hooks Script
1190+
1191+ #### `npm run prepare`
1192+ **Description:** Installs Husky git hooks for pre-commit validation.
1193+
1194+ **When to use:** Automatically runs after `npm install`. Manually run if hooks aren' t working.
1195+
1196+ ** Example:**
1197+ ` ` ` bash
1198+ npm run prepare
1199+ ` ` `
1200+
1201+ ** What it sets up:**
1202+ - Pre-commit hook: Runs linting and formatting on staged files
1203+ - Commit-msg hook: Validates commit message format (Conventional Commits)
1204+
1205+ ** Bypass hooks (not recommended):**
1206+ ` ` ` bash
1207+ git commit --no-verify -m " message"
1208+ ` ` `
1209+
1210+ ---
1211+
1212+ # ## Quick Reference Table
1213+
1214+ | Script | Use Case | Required Services |
1215+ | --------| ----------| -------------------|
1216+ | ` npm run dev` | Local development | MongoDB, Redis |
1217+ | ` npm start` | Production run | MongoDB, Redis |
1218+ | ` npm test` | Testing & CI/CD | MongoDB, Redis |
1219+ | ` npm run test:watch` | TDD workflow | MongoDB, Redis |
1220+ | ` npm run lint` | Code quality check | None |
1221+ | ` npm run format` | Code formatting | None |
1222+ | ` npm run docker:up` | Docker development | Docker |
1223+ | ` npm run docker:down` | Stop Docker | Docker |
1224+ | ` npm run docker:build` | Rebuild images | Docker |
1225+ | ` npm run seed` | Generate test data | MongoDB |
1226+ | ` npm run migrate:up` | Apply migrations | MongoDB |
1227+ | ` npm run migrate:down` | Revert migration | MongoDB |
1228+ | ` npm run migrate:create` | Create migration | None |
1229+ | ` npm run prepare` | Setup git hooks | None |
1230+
1231+ ---
1232+
1233+ # ## Common Workflows
1234+
1235+ ** Starting development for the first time:**
1236+ ` ` ` bash
1237+ npm install
1238+ cp .env.example .env # Configure your environment
1239+ npm run migrate:up # Apply database migrations
1240+ npm run seed # Add sample data
1241+ npm run dev # Start developing
1242+ ` ` `
1243+
1244+ ** Before committing code:**
1245+ ` ` ` bash
1246+ npm run lint # Fix code style issues
1247+ npm run format # Format all files
1248+ npm test # Ensure tests pass
1249+ git add .
1250+ git commit -m " feat: your feature" # Husky hooks run automatically
1251+ ` ` `
1252+
1253+ ** Using Docker:**
1254+ ` ` ` bash
1255+ npm run docker:build # First time only
1256+ npm run docker:up # Start all services
1257+ # Develop...
1258+ npm run docker:down # When finished
1259+ ` ` `
1260+
1261+ ** Deploying to production:**
1262+ ` ` ` bash
1263+ npm run lint
1264+ npm test
1265+ npm run migrate:up # On production server
1266+ NODE_ENV=production npm start
1267+ ` ` `
9631268
9641269# # Troubleshooting
9651270
0 commit comments