Skip to content

Latest commit

 

History

History
137 lines (103 loc) · 3.23 KB

README.md

File metadata and controls

137 lines (103 loc) · 3.23 KB

Node.js TypeScript API Template

A production-ready Node.js API template built with TypeScript, Express, and MongoDB. This template includes user management, authentication, error handling, and testing setup out of the box.

Features

  • 🚀 TypeScript - Type safety and modern JavaScript features
  • 🔐 Authentication & Authorization - JWT-based auth system
  • 👥 User Management - Complete user CRUD operations
  • 🗃️ MongoDB Integration - Using Mongoose ODM
  • 🐳 Docker Support - Containerization ready
  • Testing - Jest setup with example tests
  • 🔄 Middleware - Error handling and auth middleware
  • 📝 Input Validation - Request validation utilities
  • 🔧 Environment Configuration - Proper env management
  • 📊 API Documentation - Clear API structure

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB
  • Docker (optional)

Getting Started

  1. Clone the repository:
git clone <your-repo-url>
cd <project-directory>
  1. Install dependencies:
npm install
  1. Set up environment variables:
cp .env.example .env

Edit the .env file with your configuration.

  1. Start the development server:
npm run dev

Or using Docker:

docker-compose up

Project Structure

src/
├── __tests__/        # Test files
├── controllers/      # Route controllers
├── middlewares/      # Custom middlewares
├── models/          # Database models
├── routes/          # API routes
├── services/        # Business logic
├── utils/           # Utility functions
├── app.ts           # Express app setup
└── server.ts        # Server entry point

Available Scripts

  • npm run dev - Start development server
  • npm run build - Build for production
  • npm start - Start production server
  • npm test - Run tests
  • npm run test:watch - Run tests in watch mode
  • npm run lint - Run ESLint
  • npm run format - Format code with Prettier

API Endpoints

Authentication

  • POST /api/auth/register - Register a new user
  • POST /api/auth/login - Login user
  • POST /api/auth/logout - Logout user

Users

  • GET /api/users - Get all users (admin only)
  • GET /api/users/:id - Get user by ID
  • PUT /api/users/:id - Update user
  • DELETE /api/users/:id - Delete user

Environment Variables

PORT=3000
MONGODB_URI=mongodb://localhost:27017/your-database
JWT_SECRET=your-jwt-secret
NODE_ENV=development

Testing

The template includes a comprehensive test suite using Jest. Run tests with:

npm test

Docker

The template includes Docker configuration for easy containerization:

# Build and run containers
docker-compose up

# Run in background
docker-compose up -d

# Stop containers
docker-compose down

Contributing

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

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For support, email [[email protected]] or open an issue in the repository.