Production-grade REST API built with Node.js, TypeScript, Express, MongoDB, and Redis.
- TypeScript - Type-safe development
- Express.js - Fast, unopinionated web framework
- MongoDB - NoSQL database with Mongoose ODM
- Redis - In-memory caching and session management
- Docker - Containerized deployment
- JWT Authentication - Secure token-based auth
- Rate Limiting - Redis-based distributed rate limiting
- Security - Helmet, CORS, input validation
- Logging - Morgan for HTTP request logging
- Node.js >= 18.0.0
- Docker & Docker Compose
- npm >= 9.0.0
git clone <your-repo-url>
cd node-rest-api-typescriptnpm installcp .env.example .envEdit .env with your configuration:
# Application
NODE_ENV=development
PORT=3000
# Database
MONGODB_URI=mongodb://admin:password123@mongodb:27017/myapp?authSource=admin
MONGO_USERNAME=admin
MONGO_PASSWORD=password123
# Redis
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=redis_secret_password
# JWT
JWT_SECRET=your-super-secret-jwt-key-change-in-production
JWT_EXPIRE=7d# Start all services (MongoDB, Redis, App, Admin UIs)
npm run docker:dev
# Or manually
docker-compose -f docker-compose.dev.yml up -d
# View logs
npm run docker:logsServices available:
- API: http://localhost:3000
- MongoDB: mongodb://localhost:27017
- Redis: localhost:6379
- Mongo Express: http://localhost:8082
- Redis Commander: http://localhost:8081
# Build production image
npm run docker:build
# Start production services
npm run docker:prod
# Stop services
npm run docker:down# Using Homebrew (macOS)
brew install mongodb-community redis
brew services start mongodb-community
brew services start redis
# Or use Docker for just MongoDB and Redis
docker run -d -p 27017:27017 --name mongodb mongo:7.0
docker run -d -p 6379:6379 --name redis redis:7-alpineMONGODB_URI=mongodb://localhost:27017/myapp
REDIS_HOST=localhost# Development mode with hot reload
npm run dev
# Build for production
npm run build
# Start production server
npm startsrc/
βββ config/
β βββ db.ts # MongoDB connection
β βββ redis.ts # Redis client & utilities
βββ middleware/
β βββ auth.middleware.ts
β βββ cache.middleware.ts
β βββ error.middleware.ts
β βββ redisRateLimiter.middleware.ts
βββ models/
β βββ user.model.ts
βββ routes/
β βββ auth.routes.ts
β βββ user.routes.ts
βββ services/
β βββ cache.service.ts # Redis cache service
βββ types/
β βββ express.d.ts
βββ server.ts # Application entry point
import { cacheService } from './services/cache.service';
// Cache user data
await cacheService.cacheUser(userId, userData, 3600);
// Get cached user
const user = await cacheService.getCachedUser(userId);
// Cache query results
await cacheService.cacheQuery('allUsers', users);import { redisRateLimiter } from './middleware/redisRateLimiter.middleware';
// Custom rate limiter
app.use('/api/expensive', redisRateLimiter({
windowMs: 60000, // 1 minute
max: 10, // 10 requests per minute
}));// Create session
await cacheService.createSession(sessionId, userId, 86400);
// Get session
const session = await cacheService.getSession(sessionId);
// Delete session
await cacheService.deleteSession(sessionId);POST /api/auth/register- Register new userPOST /api/auth/login- Login userPOST /api/auth/logout- Logout user
GET /api/users- Get all users (cached)GET /api/users/:id- Get user by IDPUT /api/users/:id- Update userDELETE /api/users/:id- Delete user
GET /health- Server health status
Access Redis GUI at http://localhost:8081
- View all keys
- Monitor memory usage
- Execute Redis commands
Access MongoDB GUI at http://localhost:8082
- Browse collections
- Run queries
- Manage documents
npm testnpm run dev # Development mode with hot reload
npm run build # Build TypeScript to JavaScript
npm start # Start production server
npm run lint # Lint code
npm run lint:fix # Fix linting errors
npm run format # Format code with Prettier
# Docker scripts
npm run docker:dev # Start development environment
npm run docker:prod # Start production environment
npm run docker:down # Stop all containers
npm run docker:logs # View container logs
npm run docker:build # Build Docker images- Build the image:
docker build -t your-api:latest .- Push to registry:
docker tag your-api:latest your-registry/your-api:latest
docker push your-registry/your-api:latest- Deploy to server:
docker-compose -f docker-compose.yml up -dEnsure these are set in production:
- Generate strong
JWT_SECRET - Use secure database passwords
- Set
NODE_ENV=production - Configure proper
CORS_ORIGIN
- β Helmet.js for security headers
- β CORS configuration
- β Rate limiting with Redis
- β JWT token authentication
- β Password hashing with Argon2
- β Input validation with Joi
- β Token blacklisting for logout
- β Graceful shutdown handling
- Node.js - Runtime environment
- TypeScript - Type safety
- Express - Web framework
- MongoDB - Database
- Mongoose - ODM
- Redis - Caching & sessions
- Docker - Containerization
- JWT - Authentication
- Argon2 - Password hashing
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
MIT
Your Name
Made with β€οΈ and TypeScript