Skip to content

Bharat1Rajput/twitter-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Twitter Backend 🐦

A production-ready Twitter clone backend built with modern Node.js technologies, showcasing scalable architecture, security best practices, and comprehensive API design.

πŸš€ Live Demo

Core Functionality

  • πŸ” User Authentication - JWT-based secure authentication
  • πŸ‘€ User Management - Profile creation, updates, and user discovery
  • 🐦 Tweet System - Create, read, delete tweets with 280 character limit
  • πŸ–Ό Media Uploads
    • Upload images with tweets (multiple images per tweet)
    • Upload profile picture and cover image for users
  • ❀️ Social Interactions - Like/unlike tweets with real-time counts
  • πŸ‘₯ Follow System - Follow/unfollow users with relationship management
  • πŸ” Search Functionality - Search users and tweets
  • πŸ“± Timeline Feed - Personalized timeline based on following
  • πŸ’¬ Direct Messaging (DM) - Real-time chat between users with sender/receiver mapping

Technical Features

  • πŸ“Š RESTful API Design - Clean, intuitive endpoints
  • πŸ›‘οΈ Security First - Input validation, rate limiting, CORS protection
  • πŸ“ Comprehensive Validation - Request validation with detailed error messages
  • 🚦 Error Handling - Consistent error responses across all endpoints
  • ⚑ Real-Time Features - WebSocket integration (Socket.IO) for live messaging
  • πŸ—‚ Static Media Serving - Publicly accessible media URLs for uploaded images

πŸ›  Tech Stack

Backend

  • Runtime: Node.js
  • Framework: Express.js
  • Database: MongoDB with Mongoose ODM
  • Authentication: JWT (JSON Web Tokens)
  • Validation: express-validator
  • File Uploads: Multer (disk storage) + Express static file serving
  • Security: bcryptjs, helmet, cors, express-rate-limit

Development Tools

  • Environment Management: dotenv
  • Development Server: nodemon
  • Version Control: Git & GitHub

πŸ— Architecture

twitter-backend/
β”œβ”€β”€ config/                 # Configuration (DB, multer, etc.)
β”‚   β”œβ”€β”€ database.js
β”‚   
β”œβ”€β”€ controllers/            # Business logic layer
β”‚   β”œβ”€β”€ authController.js
β”‚   β”œβ”€β”€ tweetController.js  # Tweet creation with image upload support
β”‚   β”œβ”€β”€ userController.js   # Profile update with profile/cover image  upload
β”‚   └── searchController.js
β”‚   └── dmController.js     # Direct Messaging controller
β”œβ”€β”€ middleware/             # Custom middleware
β”‚   β”œβ”€β”€ auth.js             # Authentication middleware
β”‚   └── validation.js       # Validation middleware (express-validator)
β”‚   └── rateLimiter.js
|   └── multerUpload.js     # Multer storage, fileFilter & limits
β”œβ”€β”€ models/                 # Database models
β”‚   β”œβ”€β”€ User.js             # profilePicture, coverPicture fields
β”‚   └── Tweet.js            # images[] field for tweet media
β”‚   └── Message.js          # DM model
β”œβ”€β”€ routes/                 # API routes
β”‚   β”œβ”€β”€ auth.js
β”‚   β”œβ”€β”€ tweets.js           # POST /api/tweets with multipart/form-data
β”‚   β”œβ”€β”€ users.js            # PUT/PATCH /api/users/profile with images
β”‚   └── search.js
β”‚   └── message.js          # Direct Messaging routes
β”œβ”€β”€ upload/                 # Local folder for uploaded images (profile, cover, tweets)
β”œβ”€β”€ validators/             # Input validation schemas
β”‚   β”œβ”€β”€ userValidation.js
β”‚   └── tweetValidation.js
β”‚   └── dmValidation.js
β”œβ”€β”€ .env                    # Environment variables
β”œβ”€β”€ .gitignore
β”œβ”€β”€ package.json
β”œβ”€β”€ socket.js
└── server.js               # Application entry point

πŸš€ Installation

Prerequisites

  • Node.js (v14 or higher)
  • MongoDB (local or MongoDB Atlas)
  • Git

Setup Instructions

  1. Clone the repository

    git clone https://github.com/Bharat1Rajput/twitter-backend.git
    cd twitter-backend
  2. Install dependencies

    npm install
  3. Environment Setup

    cp .env.example .env

    Update .env with your configuration:

    PORT=5000
    MONGODB_URI=mongodb://localhost:27017/twitter-backend
    JWT_SECRET=your_super_secret_jwt_key_here
    NODE_ENV=development
  4. Start MongoDB

    # If using local MongoDB
    mongosh
    
    # Or ensure MongoDB Atlas connection is configured
  5. Run the application

    # Development mode with auto-restart
    npm run dev
    
    # Production mode
    npm start

πŸ“š API Documentation

Authentication Endpoints

Method Endpoint Description Auth Required
POST /api/auth/register Register new user No
POST /api/auth/login Login user No

User Endpoints

Method Endpoint Description Auth Required
GET /api/users/profile Get current user profile Yes
PUT /api/users/profile Update user profile Yes
GET /api/users/:username Get user by username No
POST /api/users/:userId/follow Follow/unfollow user Yes
GET /api/users/:userId/followers Get user followers No
GET /api/users/:userId/following Get user following No

Tweet Endpoints

Method Endpoint Description Auth Required
POST /api/tweets Create new tweet Yes
GET /api/tweets Get public timeline No
GET /api/tweets/timeline Get personalized timeline Yes
POST /api/tweets/:tweetId/like Like/unlike tweet Yes
DELETE /api/tweets/:tweetId Delete tweet Yes
GET /api/tweets/user/:userId Get user's tweets yes
GET /api/tweets/user//:tweetId/analytics Get tweet analytics yes

Search Endpoints

Method Endpoint Description Auth Required
GET /api/search/users?q=query Search users Yes
GET /api/search/tweets?q=query Search tweets Yes

Search Endpoints

Method Endpoint Description Auth Required
POST /api/message/ send new msg Yes
GET /api/message/:userId get conversion Yes
PUT /api/message/:messageId/read mark as Read Yes

πŸ”’ Security Features

  • Authentication: JWT-based stateless authentication
  • Password Security: bcrypt hashing with salt rounds
  • Input Validation: Comprehensive validation using express-validator
  • CORS Protection: Cross-origin resource sharing configuration
  • Rate Limiting: Limit API requests to prevent brute-force attacks (express-rate-limit)
  • Security Headers: Helmet.js for setting various HTTP headers
  • Data Sanitization: Input sanitization to prevent XSS attacks
  • Authorization: Route-level authentication middleware

πŸ“ˆ Future Enhancements

  • Caching Layer - Redis implementation for improved performance
  • Email Service - Email verification and notifications
  • Push Notifications - Mobile push notification system
  • Analytics - Tweet engagement and user activity analytics
  • Admin Panel - Administrative dashboard for user management
  • API Versioning - Versioned API endpoints for backward compatibility

πŸ“Š Project Statistics

  • Total Lines of Code: ~2,000+
  • API Endpoints: 15+
  • Database Models: 2
  • Middleware Functions: 3+
  • Validation Schemas: 5+

🀝 Contributing

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

πŸ“ License

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

πŸ‘¨β€πŸ’» Author

Bharat Rajput

πŸ™ Acknowledgments

  • Express.js community for excellent documentation
  • MongoDB team for the robust database solution
  • All contributors who helped improve this project

⭐ Star this repository if it helped you learn backend development!

Built with ❀️ by Bharat Rajput

About

A full-featured Twitter clone backend built with Node.js, Express.js, and MongoDB showcasing modern backend development practices and scalable architecture.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors