JSPM

express-ts-api-starter

1.3.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 188
  • Score
    100M100P100Q69594F
  • License MIT

Production-ready Express.js + TypeScript boilerplate with MVC architecture, JWT auth, MongoDB, security, validation, and testingβ€”build scalable REST APIs fast

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

npm version npm downloads License: MIT TypeScript Express.js Node.js

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 dev

Your API is ready with authentication, validation, and error handling out of the box!


πŸš€ Quick Start

Create a new project with a single command:

npx express-ts-api-starter my-api

This will:

  • βœ… Create a new project directory
  • βœ… Copy all template files and folder structure
  • βœ… Set up configuration files
  • βœ… Create .env file from .env.example

Then:

cd my-api
npm install
npm run dev

Option 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 dev

That'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-production

See .env.example for all available options.


πŸ§ͺ Testing

# Run all tests
npm test

# Watch mode
npm test -- --watch

# Coverage report
npm test -- --coverage

Example 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:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

MIT License - see LICENSE file for details.

Free for personal and commercial use! ✨


πŸ’¬ Support


🌟 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-api

Happy coding! πŸŽ‰