A production-ready Twitter clone backend built with modern Node.js technologies, showcasing scalable architecture, security best practices, and comprehensive API design.
- API Base URL: (https://twitter-backend-3lau.onrender.com)
- π 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
- π 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
- 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
- Environment Management: dotenv
- Development Server: nodemon
- Version Control: Git & GitHub
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
- Node.js (v14 or higher)
- MongoDB (local or MongoDB Atlas)
- Git
-
Clone the repository
git clone https://github.com/Bharat1Rajput/twitter-backend.git cd twitter-backend -
Install dependencies
npm install
-
Environment Setup
cp .env.example .env
Update
.envwith your configuration:PORT=5000 MONGODB_URI=mongodb://localhost:27017/twitter-backend JWT_SECRET=your_super_secret_jwt_key_here NODE_ENV=development
-
Start MongoDB
# If using local MongoDB mongosh # Or ensure MongoDB Atlas connection is configured
-
Run the application
# Development mode with auto-restart npm run dev # Production mode npm start
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /api/auth/register |
Register new user | No |
| POST | /api/auth/login |
Login user | No |
| 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 |
| 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 |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| GET | /api/search/users?q=query |
Search users | Yes |
| GET | /api/search/tweets?q=query |
Search tweets | Yes |
| 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 |
- 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
- 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
- Total Lines of Code: ~2,000+
- API Endpoints: 15+
- Database Models: 2
- Middleware Functions: 3+
- Validation Schemas: 5+
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Bharat Rajput
- GitHub: @Bharat1Rajput
- LinkedIn: Bharat Singh
- Email: bharattsingh33@gmail.com
- 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