@codeenthusiast09/create-express-app 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +90 -0
  3. package/bin/cli.cjs +12 -0
  4. package/dist/generator.d.ts +59 -0
  5. package/dist/generator.d.ts.map +1 -0
  6. package/dist/generator.js +178 -0
  7. package/dist/generator.js.map +1 -0
  8. package/dist/index.d.ts +2 -0
  9. package/dist/index.d.ts.map +1 -0
  10. package/dist/index.js +113 -0
  11. package/dist/index.js.map +1 -0
  12. package/dist/installer.d.ts +37 -0
  13. package/dist/installer.d.ts.map +1 -0
  14. package/dist/installer.js +146 -0
  15. package/dist/installer.js.map +1 -0
  16. package/dist/prompts.d.ts +19 -0
  17. package/dist/prompts.d.ts.map +1 -0
  18. package/dist/prompts.js +67 -0
  19. package/dist/prompts.js.map +1 -0
  20. package/package.json +59 -0
  21. package/templates/boilerplate/.env.example +24 -0
  22. package/templates/boilerplate/.eslintrc.cjs +27 -0
  23. package/templates/boilerplate/.prettierrc +9 -0
  24. package/templates/boilerplate/DEPENDENCIES.md +43 -0
  25. package/templates/boilerplate/README.md +282 -0
  26. package/templates/boilerplate/docker/.dockerignore +46 -0
  27. package/templates/boilerplate/docker/Dockerfile +61 -0
  28. package/templates/boilerplate/docker/docker-compose.yml +68 -0
  29. package/templates/boilerplate/drizzle/drizzle.config.ts +13 -0
  30. package/templates/boilerplate/drizzle/schema.ts +22 -0
  31. package/templates/boilerplate/jest.config.cjs +24 -0
  32. package/templates/boilerplate/nodemon.json +11 -0
  33. package/templates/boilerplate/package.json +61 -0
  34. package/templates/boilerplate/prisma/schema.prisma +0 -0
  35. package/templates/boilerplate/scripts/generate-module.cjs +397 -0
  36. package/templates/boilerplate/src/common/middleware/error.middleware.ts +121 -0
  37. package/templates/boilerplate/src/common/middleware/validation.middleware.ts +50 -0
  38. package/templates/boilerplate/src/common/utils/http-logger.ts +24 -0
  39. package/templates/boilerplate/src/common/utils/logger.ts +34 -0
  40. package/templates/boilerplate/src/common/utils/response-helper.ts +140 -0
  41. package/templates/boilerplate/src/config/env.ts +24 -0
  42. package/templates/boilerplate/src/config/index.ts +92 -0
  43. package/templates/boilerplate/src/database/drizzle.connection.ts +50 -0
  44. package/templates/boilerplate/src/database/index.ts +20 -0
  45. package/templates/boilerplate/src/database/mongoose.connection.ts +56 -0
  46. package/templates/boilerplate/src/database/prisma.connection.ts +50 -0
  47. package/templates/boilerplate/src/modules/.gitkeep +0 -0
  48. package/templates/boilerplate/src/server.ts +121 -0
  49. package/templates/boilerplate/src/types/express.types.ts +29 -0
  50. package/templates/boilerplate/src/types/index.ts +5 -0
  51. package/templates/boilerplate/src/types/response.types.ts +54 -0
  52. package/templates/boilerplate/tsconfig.json +72 -0
@@ -0,0 +1,282 @@
1
+ # Express TypeScript Boilerplate
2
+
3
+ A production-ready Express.js boilerplate with TypeScript, featuring a clean architecture inspired by NestJS.
4
+
5
+ ## Features
6
+
7
+ - **Clean Architecture** - Modular structure ready for your features
8
+ - **Type Safety** - Strict TypeScript configuration
9
+ - **Validation** - Request validation with Zod
10
+ - **Logging** - Structured logging with Pino
11
+ - **Configuration** - Type-safe config with environment validation
12
+ - **Code Quality** - ESLint + Prettier configured
13
+ - **Docker Ready** - Coming soon
14
+ - **Testing Setup** - Jest configured
15
+
16
+ ## Prerequisites
17
+
18
+ - Node.js 20.x or higher
19
+ - npm
20
+ - Database: MongoDB OR PostgreSQL (depending on your choice)
21
+
22
+ ## 💾 Database Options
23
+
24
+ This boilerplate supports three database configurations:
25
+
26
+ 1. **MongoDB with Mongoose** (default)
27
+ 2. **PostgreSQL with Prisma**
28
+ 3. **PostgreSQL with Drizzle**
29
+
30
+ The active connection is set in `src/database/index.ts`. See `DEPENDENCIES.md` for setup instructions.
31
+
32
+ When using the CLI tool, it will automatically configure the database based on your selection.
33
+
34
+ ## Quick Start
35
+
36
+ ### 1. Install Dependencies
37
+
38
+ ```bash
39
+ npm install
40
+ ```
41
+
42
+ ### 2. Set Up Environment
43
+
44
+ ```bash
45
+ cp .env.example .env
46
+ ```
47
+
48
+ Edit `.env` with your values:
49
+
50
+ ```env
51
+ NODE_ENV=development
52
+ PORT=3000
53
+ DATABASE_URL=mongodb://localhost:27017/myapp
54
+ JWT_SECRET=your-super-secret-jwt-key-min-32-characters-for security
55
+ ```
56
+
57
+ ### 3. Start Development Server
58
+
59
+ ```bash
60
+ npm run dev
61
+ ```
62
+
63
+ Server will start at `http://localhost:3000`
64
+
65
+ ## 📁 Project Structure
66
+
67
+ ```
68
+ src/
69
+ ├── common/ # Shared utilities and middleware
70
+ │ ├── middleware/ # Express middleware (error handling, validation)
71
+ │ └── utils/ # Helper functions (logger, response formatter)
72
+ ├── config/ # Configuration with Zod validation
73
+ ├── database/ # Database connection
74
+ ├── modules/ # Your feature modules go here
75
+ │ └── .gitkeep
76
+ ├── types/ # TypeScript type definitions
77
+ └── server.ts # Application entry point
78
+ ```
79
+
80
+ ## 🗂️ How to Add a Module
81
+
82
+ ### Quick Way: Use the Module Generator
83
+
84
+ ```bash
85
+ npm run generate:module users
86
+ ```
87
+
88
+ This creates:
89
+
90
+ ```
91
+ src/modules/users/
92
+ ├── user.controller.ts # Route handlers
93
+ ├── user.service.ts # Business logic
94
+ ├── user.routes.ts # Route definitions
95
+ ├── user.validation.ts # Request validation schemas
96
+ ├── user.repository.interface.ts # Repository contract
97
+ └── user.repository.ts # Database operations
98
+ ```
99
+
100
+ Then:
101
+
102
+ 1. Implement database operations in `user.repository.ts`
103
+ 2. Update validation schemas in `user.validation.ts`
104
+ 3. Register routes in `server.ts`:
105
+ ```typescript
106
+ import userRoutes from './modules/users/user.routes';
107
+ app.use('/api/users', userRoutes);
108
+ ```
109
+
110
+ ### Manual Way: Create Files Yourself
111
+
112
+ Create a new folder in `src/modules/` with this structure:
113
+
114
+ ```
115
+ src/modules/users/
116
+ ├── user.controller.ts # Route handlers
117
+ ├── user.service.ts # Business logic
118
+ ├── user.repository.ts # Database operations
119
+ ├── user.routes.ts # Route definitions
120
+ ├── user.validation.ts # Request validation schemas
121
+ └── user.model.ts # Database model (Mongoose/Prisma)
122
+ ```
123
+
124
+ Then register routes in `server.ts`:
125
+
126
+ ```typescript
127
+ import userRoutes from './modules/users/user.routes';
128
+ app.use('/api/users', userRoutes);
129
+ ```
130
+
131
+ ## API Endpoints
132
+
133
+ ### Health Check
134
+
135
+ ```http
136
+ GET /health
137
+ ```
138
+
139
+ Returns server status and uptime.
140
+
141
+ ### Root
142
+
143
+ ```http
144
+ GET /
145
+ ```
146
+
147
+ Returns welcome message.
148
+
149
+ ## Testing
150
+
151
+ ```bash
152
+ # Run tests
153
+ npm test
154
+
155
+ # Watch mode
156
+ npm run test:watch
157
+
158
+ # Coverage
159
+ npm run test:coverage
160
+ ```
161
+
162
+ ## Building
163
+
164
+ ```bash
165
+ # Build for production
166
+ npm run build
167
+
168
+ # Start production server
169
+ npm start
170
+ ```
171
+
172
+ ## 🐳 Docker
173
+
174
+ ### Using Docker Compose (Recommended for Development)
175
+
176
+ ```bash
177
+ # Start all services (app + database)
178
+ docker-compose up
179
+
180
+ # Start in detached mode
181
+ docker-compose up -d
182
+
183
+ # View logs
184
+ docker-compose logs -f app
185
+
186
+ # Stop services
187
+ docker-compose down
188
+
189
+ # Remove volumes (clears database)
190
+ docker-compose down -v
191
+ ```
192
+
193
+ ### Manual Docker Build
194
+
195
+ ```bash
196
+ # Build image
197
+ docker build -t express-app .
198
+
199
+ # Run container
200
+ docker run -p 3000:3000 \
201
+ -e DATABASE_URL=mongodb://host:27017/myapp \
202
+ -e JWT_SECRET=your-secret-min-32-chars \
203
+ express-app
204
+ ```
205
+
206
+ ### Database Selection in Docker
207
+
208
+ By default, docker-compose uses MongoDB. To use PostgreSQL:
209
+
210
+ 1. Comment out MongoDB service
211
+ 2. Uncomment PostgreSQL service
212
+ 3. Uncomment PostgreSQL `DATABASE_URL` in app service
213
+ 4. Update depends_on to use postgres
214
+
215
+ ## Code Quality
216
+
217
+ ```bash
218
+ # Lint
219
+ npm run lint
220
+ npm run lint:fix
221
+
222
+ # Format
223
+ npm run format
224
+ npm run format:check
225
+
226
+ # Type check
227
+ npm run typecheck
228
+ ```
229
+
230
+ ## Path Aliases
231
+
232
+ Use clean imports with path aliases:
233
+
234
+ ```typescript
235
+ // ❌ Don't do this
236
+ import { logger } from '../../../common/utils/logger';
237
+
238
+ // ✅ Do this
239
+ import { logger } from '@/common/utils/logger';
240
+ ```
241
+
242
+ Available aliases:
243
+
244
+ - `@/modules/*`
245
+ - `@/common/*`
246
+ - `@/config/*`
247
+ - `@/database/*`
248
+ - `@/types/*`
249
+
250
+ ## Environment Variables
251
+
252
+ | Variable | Description | Required |
253
+ | -------------- | ----------------------------------------- | -------- |
254
+ | `NODE_ENV` | Environment (development/production/test) | No |
255
+ | `PORT` | Server port | No |
256
+ | `DATABASE_URL` | Database connection string | Yes |
257
+ | `JWT_SECRET` | JWT signing secret (min 32 chars) | Yes |
258
+ | `CORS_ORIGIN` | CORS allowed origins | No |
259
+ | `LOG_LEVEL` | Logging level (info/debug/error) | No |
260
+
261
+ ## Scripts
262
+
263
+ | Script | Description |
264
+ | ---------------- | ------------------------ |
265
+ | `npm run dev` | Start development server |
266
+ | `npm run build` | Build for production |
267
+ | `npm start` | Run production build |
268
+ | `npm test` | Run tests |
269
+ | `npm run lint` | Check code quality |
270
+ | `npm run format` | Format code |
271
+
272
+ ## Contributing
273
+
274
+ 1. Fork the repository
275
+ 2. Create your feature branch
276
+ 3. Commit your changes
277
+ 4. Push to the branch
278
+ 5. Open a Pull Request
279
+
280
+ ## License
281
+
282
+ MIT
@@ -0,0 +1,46 @@
1
+ # Dependencies
2
+ node_modules
3
+ npm-debug.log
4
+ package-lock.json
5
+
6
+ # Build output
7
+ dist
8
+ build
9
+
10
+ # Environment files
11
+ .env
12
+ .env.development
13
+ .env.production
14
+ .env.local
15
+ .env.*.local
16
+
17
+ # Git
18
+ .git
19
+ .gitignore
20
+
21
+ # IDE
22
+ .vscode
23
+ .idea
24
+ *.swp
25
+ *.swo
26
+
27
+ # Testing
28
+ coverage
29
+ .nyc_output
30
+
31
+ # Documentation
32
+ README.md
33
+ docs
34
+
35
+ # Docker files
36
+ Dockerfile
37
+ docker-compose.yml
38
+ .dockerignore
39
+
40
+ # CI/CD
41
+ .github
42
+ .gitlab-ci.yml
43
+
44
+ # Misc
45
+ .DS_Store
46
+ *.log
@@ -0,0 +1,61 @@
1
+ # Multi-stage build for smaller final image
2
+
3
+ # Stage 1: Build
4
+ FROM node:20-alpine AS builder
5
+
6
+ WORKDIR /app
7
+
8
+ # Copy package files first (for better layer caching)
9
+ COPY package*.json ./
10
+
11
+ # Install ALL dependencies (including devDependencies for build)
12
+ RUN npm ci
13
+
14
+ # Copy source code
15
+ COPY . .
16
+
17
+ # Build TypeScript and remove dev dependencies
18
+ RUN npm run build && \
19
+ npm prune --production
20
+
21
+ # Stage 2: Production
22
+ FROM node:20-alpine AS production
23
+
24
+ # Create non-root user for security
25
+ RUN addgroup -g 1001 -S nodejs && \
26
+ adduser -S nodejs -u 1001
27
+
28
+ WORKDIR /app
29
+
30
+ ENV NODE_ENV=production
31
+
32
+ # Copy package files
33
+ COPY package*.json ./
34
+
35
+ # Copy built app and production dependencies from builder
36
+ COPY --from=builder /app/dist ./dist
37
+ COPY --from=builder /app/node_modules ./node_modules
38
+
39
+ # Change ownership to non-root user
40
+ RUN chown -R nodejs:nodejs /app
41
+
42
+ # Switch to non-root user
43
+ USER nodejs
44
+
45
+ # Expose port
46
+ ARG PORT=3000
47
+ ENV PORT=${PORT}
48
+
49
+ EXPOSE ${PORT}
50
+
51
+ # Health check
52
+ HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
53
+ CMD node -e "require('http').get('http://localhost:' + (process.env.PORT || 3000) + '/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"
54
+
55
+ # Run the application
56
+ CMD ["node", "dist/server.js"]
57
+
58
+ # Labels
59
+ LABEL maintainer="your-email@example.com" \
60
+ description="Express TypeScript API" \
61
+ version="1.0.0"
@@ -0,0 +1,68 @@
1
+ version: '3.8'
2
+
3
+ services:
4
+ # MongoDB Service
5
+ mongodb:
6
+ image: mongo:7
7
+ container_name: express-mongodb
8
+ restart: unless-stopped
9
+ ports:
10
+ - '27017:27017'
11
+ environment:
12
+ MONGO_INITDB_ROOT_USERNAME: admin
13
+ MONGO_INITDB_ROOT_PASSWORD: password123
14
+ MONGO_INITDB_DATABASE: myapp
15
+ volumes:
16
+ - mongodb_data:/data/db
17
+ networks:
18
+ - app-network
19
+
20
+ # PostgreSQL Service (uncomment if using PostgreSQL)
21
+ # postgres:
22
+ # image: postgres:16-alpine
23
+ # container_name: express-postgres
24
+ # restart: unless-stopped
25
+ # ports:
26
+ # - '5432:5432'
27
+ # environment:
28
+ # POSTGRES_USER: postgres
29
+ # POSTGRES_PASSWORD: password123
30
+ # POSTGRES_DB: myapp
31
+ # volumes:
32
+ # - postgres_data:/var/lib/postgresql/data
33
+ # networks:
34
+ # - app-network
35
+
36
+ # Application Service
37
+ app:
38
+ build:
39
+ context: .
40
+ dockerfile: Dockerfile
41
+ target: production
42
+ container_name: express-app
43
+ restart: unless-stopped
44
+ ports:
45
+ - '3000:3000'
46
+ environment:
47
+ NODE_ENV: production
48
+ PORT: 3000
49
+ # MongoDB connection
50
+ DATABASE_URL: mongodb://admin:password123@mongodb:27017/myapp?authSource=admin
51
+ # PostgreSQL connection (uncomment if using PostgreSQL)
52
+ # DATABASE_URL: postgresql://postgres:password123@postgres:5432/myapp
53
+ JWT_SECRET: your-super-secret-jwt-key-change-this-in-production-min-32-chars
54
+ JWT_EXPIRES_IN: 7d
55
+ LOG_LEVEL: info
56
+ depends_on:
57
+ - mongodb
58
+ # - postgres # Uncomment if using PostgreSQL
59
+ networks:
60
+ - app-network
61
+
62
+ networks:
63
+ app-network:
64
+ driver: bridge
65
+
66
+ volumes:
67
+ mongodb_data:
68
+ # postgres_data: # Uncomment if using PostgreSQL
@@ -0,0 +1,13 @@
1
+ import type { Config } from 'drizzle-kit';
2
+ import * as dotenv from 'dotenv';
3
+
4
+ dotenv.config({ path: '.env', quiet: true });
5
+
6
+ export default {
7
+ schema: './drizzle/schema.ts',
8
+ out: './drizzle/migrations',
9
+ driver: 'pg',
10
+ dbCredentials: {
11
+ connectionString: process.env.DATABASE_URL || '',
12
+ },
13
+ } satisfies Config;
@@ -0,0 +1,22 @@
1
+ import { pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core';
2
+
3
+ /**
4
+ * Drizzle Schema
5
+ *
6
+ * Define your database tables here.
7
+ * This is an example User table (commented out).
8
+ * Uncomment and modify as needed.
9
+ */
10
+
11
+ // Example User table
12
+ // export const users = pgTable('users', {
13
+ // id: uuid('id').defaultRandom().primaryKey(),
14
+ // email: text('email').notNull().unique(),
15
+ // name: text('name').notNull(),
16
+ // createdAt: timestamp('created_at').defaultNow().notNull(),
17
+ // updatedAt: timestamp('updated_at').defaultNow().notNull(),
18
+ // });
19
+
20
+ // Export all tables
21
+ // export type User = typeof users.$inferSelect;
22
+ // export type NewUser = typeof users.$inferInsert;
@@ -0,0 +1,24 @@
1
+ module.exports = {
2
+ preset: 'ts-jest',
3
+ testEnvironment: 'node',
4
+ roots: ['<rootDir>/src'],
5
+ testMatch: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],
6
+ transform: {
7
+ '^.+\\.ts$': 'ts-jest',
8
+ },
9
+ collectCoverageFrom: [
10
+ 'src/**/*.ts',
11
+ '!src/**/*.d.ts',
12
+ '!src/**/*.interface.ts',
13
+ '!src/**/*.type.ts',
14
+ '!src/server.ts',
15
+ ],
16
+ coverageDirectory: 'coverage',
17
+ moduleNameMapper: {
18
+ '^@/modules/(.*)$': '<rootDir>/src/modules/$1',
19
+ '^@/common/(.*)$': '<rootDir>/src/common/$1',
20
+ '^@/config/(.*)$': '<rootDir>/src/config/$1',
21
+ '^@/database/(.*)$': '<rootDir>/src/database/$1',
22
+ '^@/types/(.*)$': '<rootDir>/src/types/$1',
23
+ },
24
+ };
@@ -0,0 +1,11 @@
1
+ {
2
+ "watch": [
3
+ "src"
4
+ ],
5
+ "ext": "ts,json",
6
+ "ignore": [
7
+ "src/**/*.spec.ts",
8
+ "src/**/*.test.ts"
9
+ ],
10
+ "exec": "ts-node -r tsconfig-paths/register src/server.ts"
11
+ }
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "express-app",
3
+ "version": "1.0.0",
4
+ "description": "Express TypeScript API",
5
+ "main": "dist/server.js",
6
+ "scripts": {
7
+ "dev": "nodemon",
8
+ "build": "tsc && tsc-alias",
9
+ "start": "node dist/server.js",
10
+ "start:prod": "NODE_ENV=production node dist/server.js",
11
+ "test": "jest",
12
+ "test:watch": "jest --watch",
13
+ "test:coverage": "jest --coverage",
14
+ "lint": "eslint . --ext .ts",
15
+ "lint:fix": "eslint . --ext .ts --fix",
16
+ "format": "prettier --write \"src/**/*.ts\"",
17
+ "format:check": "prettier --check \"src/**/*.ts\"",
18
+ "typecheck": "tsc --noEmit",
19
+ "generate:module": "node scripts/generate-module.cjs"
20
+ },
21
+ "keywords": [
22
+ "express",
23
+ "typescript",
24
+ "nodejs",
25
+ "api"
26
+ ],
27
+ "author": "",
28
+ "license": "MIT",
29
+ "dependencies": {
30
+ "bcryptjs": "^2.4.3",
31
+ "cors": "^2.8.5",
32
+ "dotenv": "^16.4.5",
33
+ "express": "^4.18.2",
34
+ "jsonwebtoken": "^9.0.2",
35
+ "pino": "^8.19.0",
36
+ "pino-http": "^9.0.0",
37
+ "pino-pretty": "^10.3.1",
38
+ "zod": "^3.22.4"
39
+ },
40
+ "devDependencies": {
41
+ "@types/bcryptjs": "^2.4.6",
42
+ "@types/cors": "^2.8.17",
43
+ "@types/express": "^4.17.21",
44
+ "@types/jest": "^29.5.12",
45
+ "@types/jsonwebtoken": "^9.0.5",
46
+ "@types/node": "^20.11.19",
47
+ "@types/supertest": "^6.0.2",
48
+ "@typescript-eslint/eslint-plugin": "^6.20.0",
49
+ "@typescript-eslint/parser": "^6.20.0",
50
+ "eslint": "^8.56.0",
51
+ "jest": "^29.7.0",
52
+ "nodemon": "^3.0.3",
53
+ "prettier": "^3.2.5",
54
+ "supertest": "^6.3.4",
55
+ "ts-jest": "^29.1.2",
56
+ "ts-node": "^10.9.2",
57
+ "tsc-alias": "^1.8.16",
58
+ "tsconfig-paths": "^4.2.0",
59
+ "typescript": "^5.3.3"
60
+ }
61
+ }
File without changes