Package Exports
- express-ts-api-starter
- express-ts-api-starter/dist/server.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (express-ts-api-starter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ποΈ EXPRESS-TS-API-STARTER
Opinionated Express + TypeScript starter with JWT authentication, MongoDB integration, comprehensive security middleware, input validation, error handling, and production-ready architectureβget your REST API running in minutes, not hours.
π Quick Demo
Create a new project in seconds:
npx express-ts-api-starter my-api
cd my-api
npm install
npm run devYour API is ready with authentication, validation, and error handling out of the box!
π Quick Start
Option 1: Using CLI (Recommended)
Create a new project with a single command:
npx express-ts-api-starter my-apiThis will:
- β Create a new project directory
- β Copy all template files and folder structure
- β Set up configuration files
- β
Create
.envfile from.env.example
Then:
cd my-api
npm install
npm run devOption 2: Manual Installation
# Clone the repository
git clone https://github.com/nikhilpktcr/express-ts-api-starter.git my-api
cd my-api
# Install dependencies
npm install
# Copy environment variables
cp .env.example .env
# Start development server (hot reload enabled)
npm run devThat's it! Your server is running at http://localhost:5000 with:
- β JWT authentication ready
- β MongoDB connection configured
- β Security middleware active
- β Request logging enabled
- β Error handling in place
β‘ Why Choose This Over Others?
vs. Bare Express Setup
| Feature | Bare Express | express-ts-api-starter |
|---|---|---|
| Setup Time | 2-4 hours | 2 minutes β‘ |
| TypeScript | Manual config | β Pre-configured |
| Authentication | Build from scratch | β JWT + bcryptjs ready |
| Error Handling | Manual try-catch | β Global middleware |
| Request Logging | Manual setup | β Morgan with request IDs |
| Input Validation | Manual validation | β express-validator integrated |
| Security | Manual headers | β Helmet + CORS configured |
| Database | Manual connection | β MongoDB/Mongoose ready |
| Testing | Manual Jest setup | β Jest configured with examples |
| Code Quality | No linting | β ESLint + Prettier ready |
| Graceful Shutdown | Not included | β Production-ready |
vs. Other Starters
| Feature | Other Starters | express-ts-api-starter |
|---|---|---|
| Architecture | Varies | β MVC with functional services (clean, testable) |
| Request Tracking | Rare | β Unique request IDs (debugging made easy) |
| Error Handling | Basic | β Comprehensive with graceful shutdown |
| Validation | Optional | β Built-in express-validator |
| Logging | Basic | β Morgan with request ID correlation |
| TypeScript | Sometimes | β 100% TypeScript with strict mode |
| Documentation | Minimal | β Well-documented with examples |
| Testing | Sometimes | β Jest with test examples included |
| CLI Tool | Sometimes | β Built-in CLI generator |
vs. NestJS
| Feature | NestJS | express-ts-api-starter |
|---|---|---|
| Setup Time | 10-15 minutes | β‘ 2 minutes |
| Learning Curve | High (new framework) | β Low (Express knowledge) |
| Bundle Size | ~200KB+ | β ~50KB (lightweight) |
| Flexibility | Framework-driven | β High (minimal abstraction) |
| Request Tracking | Manual setup | β Built-in request IDs |
| Security (Out of Box) | Manual config | β Pre-configured |
π See detailed NestJS comparison β
π― Key Strengths
π Authentication & Security
- JWT-based authentication with secure token generation
- bcryptjs password hashing (industry standard)
- Helmet.js for HTTP security headers
- CORS configured for cross-origin requests
- Rate limiting ready to prevent DDoS attacks
- Request ID tracking for security auditing
π Input Validation
- express-validator middleware pre-configured
- Type-safe validation with TypeScript
- Reusable validation rules in dedicated validators folder
- Automatic error responses for invalid inputs
π‘οΈ Error Handling
- Global error middleware catches all errors
- Standardized error responses across all endpoints
- Request ID included in error responses for debugging
- Graceful shutdown handles SIGTERM/SIGINT properly
- Uncaught exception handling prevents crashes
π Logging & Monitoring
- Morgan logging with custom format
- Request ID correlation for tracking requests across services
- Structured logging ready for production monitoring
- Error logging with stack traces
ποΈ Architecture
- MVC pattern with clear separation of concerns
- Functional service layer (easier to test than classes)
- Modular structure - each feature is self-contained
- Scalable design - grow from startup to enterprise
π¦ What's Included
Core Features
- β Express.js v5 - Latest framework version
- β TypeScript 5.8 - Full type safety with strict mode
- β MongoDB + Mongoose - Database integration ready
- β JWT Authentication - Secure token-based auth
- β bcryptjs - Password hashing
- β express-validator - Input validation
- β Helmet - Security headers
- β CORS - Cross-origin resource sharing
- β Morgan - HTTP request logging
- β Multer - File upload support
- β express-rate-limit - Rate limiting
- β Jest - Testing framework with examples
- β ESLint + Prettier - Code quality tools
Developer Experience
- β CLI Tool - Generate projects with one command
- β Hot reload - See changes instantly
- β TypeScript declarations - Full IntelliSense support
- β Pre-configured scripts - dev, build, test, lint
- β Example code - User module with CRUD operations
- β Test examples - Learn testing patterns
- β Well-documented - Clear code structure
π Project Structure
src/
βββ modules/ # Feature modules (MVC pattern)
β βββ users/
β βββ userController.ts # HTTP request handlers
β βββ userService.ts # Business logic
β βββ userMessage.ts # Constants/messages
β βββ tests/ # Unit tests
βββ routes/ # API route definitions
βββ middleware/ # Express middleware
β βββ auth.ts # JWT authentication
β βββ errorMiddleware.ts
β βββ validatorMiddleware.ts
β βββ requestIdMiddleware.ts
βββ config/ # Configuration files
β βββ dbConfig.ts # MongoDB connection
β βββ envConfig.ts # Environment variables
β βββ rateLimitConfig.ts
βββ models/ # Mongoose schemas
βββ validators/ # Input validation rules
βββ utils/ # Utility functions
βββ types/ # TypeScript type definitions
βββ app.ts # Express app setupπ§ Available Commands
# Development
npm run dev # Start dev server with hot reload
# Production
npm run build # Compile TypeScript to JavaScript
npm start # Start production server
# Testing
npm test # Run test suite
npm test -- --watch # Run tests in watch mode
npm test -- --coverage # Run with coverage report
# Code Quality
npm run lint # Check for linting errors
npm run lint:fix # Auto-fix linting errorsπ Configuration
Environment Variables
Create a .env file:
# Server
NODE_ENV=development
PORT=5000
BASIC_API_URL=/api/v1
# Database
DB_CONNECTION=mongodb://localhost:27017/
DB_NAME=testDB
# Authentication
JWT_SECRET=your-super-secret-jwt-key-change-in-productionSee .env.example for all available options.
π§ͺ Testing
# Run all tests
npm test
# Watch mode
npm test -- --watch
# Coverage report
npm test -- --coverageExample test included in src/modules/users/tests/userController.test.ts
π Standardized API Responses
All responses follow a consistent format:
Success:
{
"success": true,
"requestId": "550e8400-e29b-41d4-a716-446655440000",
"message": "Operation successful",
"response": { "data": "..." }
}Error:
{
"success": false,
"requestId": "550e8400-e29b-41d4-a716-446655440000",
"error": "Error message"
}π Security Features
- Helmet - Sets secure HTTP headers
- CORS - Configurable cross-origin requests
- JWT - Secure token-based authentication
- bcryptjs - Password hashing (10 rounds)
- Rate Limiting - DDoS protection ready
- Input Validation - Prevents injection attacks
- Request ID Tracking - Security audit trail
π¦ Production Ready
- β Graceful shutdown - Handles SIGTERM/SIGINT
- β Error handling - Global error middleware
- β Logging - Request tracking with unique IDs
- β Type safety - Full TypeScript coverage
- β Testing - Jest framework configured
- β Code quality - ESLint + Prettier
π― Perfect For
- π REST APIs - Full-featured API servers
- π§ Microservices - Scalable service architecture
- π’ Enterprise Apps - Production-ready foundation
- π Learning - Best practices and patterns
- β‘ MVPs - Quick prototype development
- π± Backend Services - Complex business logic
π§ Tech Stack
- Runtime: Node.js v20+
- Framework: Express.js v5
- Language: TypeScript 5.8
- Database: MongoDB with Mongoose
- Security: Helmet, CORS, JWT, bcryptjs
- Validation: express-validator
- Logging: Morgan
- Testing: Jest
- Linting: ESLint + Prettier
π€ Contributing
Contributions welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
π License
MIT License - see LICENSE file for details.
Free for personal and commercial use! β¨
π¬ Support
- Email: nikhil.pk.connect@gmail.com
- GitHub Issues: Report bugs
- GitHub: @nikhilpktcr
π Show Your Support
If this boilerplate helps your project:
- β Star the repository on GitHub
- π¦ Use the npm package in your projects
- π Report issues you encounter
- π‘ Suggest features and improvements
- π Share with other developers
π― Roadmap
- API Documentation (Swagger/OpenAPI)
- Health check endpoint
- Pagination utility
- Redis caching layer
- Docker & Docker Compose
- CI/CD pipeline examples
- More comprehensive test suite
Ready to build? Start your next API project in minutes:
npx express-ts-api-starter my-apiHappy coding! π