A robust RESTful API for a movie rental service built with TypeScript, Express.js, and MongoDB. This application provides comprehensive functionality for managing movies, customers, rentals, and user authentication with role-based access control.
- Movie Management: CRUD operations for movies with genre categorization
- Customer Management: Customer registration and profile management
- Rental System: Complete rental workflow with return processing
- User Authentication: JWT-based authentication with role-based authorization
- Admin Panel: Administrative functions for system management
- Data Validation: Comprehensive input validation using Joi
- Security: Password hashing, helmet protection, and secure headers
- Logging: Winston-based logging with MongoDB integration
- Testing: Comprehensive unit and integration tests with Jest
- Runtime: Node.js 20.10.0+
- Language: TypeScript
- Framework: Express.js
- Database: MongoDB with Mongoose ODM
- Authentication: JSON Web Tokens (JWT)
- Validation: Joi
- Testing: Jest with Supertest
- Security: bcrypt, helmet
- Logging: Winston with MongoDB transport
vidly/
├── config/ # Environment configurations
├── controllers/ # Route controllers
├── dtos/ # Data Transfer Objects
├── Middlewares/ # Custom middleware functions
├── models/ # Mongoose models
├── routes/ # API route definitions
├── start/ # Application startup modules
├── tests/ # Unit and integration tests
├── views/ # Pug templates
└── public/ # Static files
-
Clone the repository
git clone <repository-url> cd vidly
-
Install dependencies
npm install
-
Set up environment variables
# Set JWT private key export vidly_jwtPrivateKey="your-jwt-secret-key" # Set MongoDB connection string (optional, defaults to localhost) export vidly_db="mongodb://localhost/vidly"
-
Start MongoDB
# Make sure MongoDB is running on your system mongod
npm run start:devStarts the server with nodemon for automatic restarts on file changes.
npm run build
npm startCompiles TypeScript and runs the production build.
npm testRuns the complete test suite with Jest.
POST /api/auth- User login
POST /api/users- Register new userGET /api/users/me- Get current user profile
GET /api/genres- Get all genresGET /api/genres/:id- Get genre by IDPOST /api/genres- Create new genre (Auth required)PUT /api/genres/:id- Update genre (Auth required)DELETE /api/genres/:id- Delete genre (Admin required)
GET /api/movies- Get all moviesGET /api/movies/:id- Get movie by IDPOST /api/movies- Create new movie (Auth required)PUT /api/movies/:id- Update movie (Auth required)DELETE /api/movies/:id- Delete movie (Admin required)
GET /api/customers- Get all customersGET /api/customers/:id- Get customer by IDPOST /api/customers- Create new customer (Auth required)PUT /api/customers/:id- Update customer (Auth required)DELETE /api/customers/:id- Delete customer (Admin required)
GET /api/rentals- Get all rentalsPOST /api/rentals- Create new rental (Auth required)
POST /api/returns- Process movie return (Auth required)
The API uses JWT tokens for authentication. Include the token in the request header:
x-auth-token: your-jwt-token
- Regular User: Can create rentals, manage own profile
- Admin: Full access to all resources including delete operations
The project includes comprehensive testing:
- Unit Tests: Individual component testing
- Integration Tests: API endpoint testing
- Test Coverage: Controllers, models, and middleware
Run specific test suites:
# Integration tests only
npm test -- --testPathPattern=integration
# Unit tests only
npm test -- --testPathPattern=unitConfiguration files are located in the config/ directory:
default.json- Default configurationdevelopment.json- Development environmentproduction.json- Production environmenttest.json- Test environmentcustom-environment-variables.json- Environment variable mappings
- Password hashing with bcrypt
- JWT token-based authentication
- Input validation and sanitization
- Security headers with Helmet
- MongoDB injection protection
- Rate limiting and compression
Winston logger with multiple transports:
- Console logging for development
- File logging for production
- MongoDB logging for persistent storage
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
Hossein Rezaei
For more information or support, please refer to the API documentation or contact the development team.