Skip to content

Commit db1f8ef

Browse files
committed
refactor: Complete ES modules migration and optimize boilerplate
- Convert all routes to ES modules with comprehensive Swagger documentation - Convert all controllers, utilities, and helpers to ES modules - Update Dockerfile health check to use wget for ES module compatibility - Add detailed npm scripts documentation with usage examples and workflows - Create LICENSE file (MIT) - Create directory structure for logs and file uploads - Update CI/CD workflow with correct repository name - Optimize all files for production-ready boilerplate template
1 parent f062913 commit db1f8ef

30 files changed

Lines changed: 1847 additions & 181 deletions

.github/workflows/ci-cd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ jobs:
8484
- name: Build, tag, and push image to Amazon ECR
8585
env:
8686
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
87-
ECR_REPOSITORY: nodejs-backend-template
87+
ECR_REPOSITORY: nodejs-backend-boilerplate
8888
IMAGE_TAG: ${{ github.sha }}
8989
run: |
9090
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ COPY --from=deps /app/node_modules ./node_modules
4343
# Copy source code
4444
COPY . .
4545

46+
# Install wget for health checks
47+
RUN apk add --no-cache wget
48+
4649
# Create non-root user
4750
RUN addgroup -g 1001 -S nodejs && \
4851
adduser -S nodejs -u 1001 && \
@@ -56,7 +59,7 @@ EXPOSE 3000
5659

5760
# Health check
5861
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
59-
CMD node -e "require('http').get('http://localhost:3000/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"
62+
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
6063

6164
# Start production server
6265
CMD ["node", "src/index.js"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Sahinur Islam
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 321 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

public/uploads/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)