A robust Node.js backend server for the Web Chat App, providing real-time messaging, user authentication, and data persistence with MongoDB.
🔴 PRIVACY WARNING: This server handles user data for a public chat application where all user names are visible to all users. Ensure your frontend warns users NOT to use real names or personal identifying information.
This backend serves a React frontend application. You can find the frontend repository here:
- Frontend Repository: https://github.com/Nugi29/web-chatapp-client.git
- Live Frontend: https://web-chatapp-client.vercel.app/
- ✅ RESTful API with Express.js
- ✅ Real-time messaging with Socket.io
- ✅ JWT-based authentication
- ✅ MongoDB database with Mongoose
- ✅ Password hashing with bcryptjs
- ✅ Image upload with Cloudinary
- ✅ CORS enabled for cross-origin requests
- ✅ Online/Offline user status tracking
- ✅ Message persistence and history
- Node.js - Runtime environment
- Express.js - Web framework
- Socket.io - Real-time communication
- MongoDB - Database
- Mongoose - MongoDB ODM
- JWT - Authentication tokens
- bcryptjs - Password hashing
- Cloudinary - Image storage
- CORS - Cross-Origin Resource Sharing
- Clone this repository
- Install dependencies:
npm install
- Create a
.env
file in the root directory:
PORT=5000
NODE_ENV=production
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret_key
CLOUDINARY_CLOUD_NAME=your_cloudinary_cloud_name
CLOUDINARY_API_KEY=your_cloudinary_api_key
CLOUDINARY_API_SECRET=your_cloudinary_api_secret
- Start the development server:
npm run server
For production:
npm start
npm start
- Start production servernpm run server
- Start development server with nodemon
server/
├── controllers/ # Route handlers
│ ├── messageController.js
│ └── userController.js
├── middleware/ # Custom middleware
│ └── auth.js
├── models/ # Database models
│ ├── message.js
│ └── User.js
├── routes/ # API routes
│ ├── messageRoutes.js
│ └── userRoutes.js
├── lib/ # Utility functions
│ ├── cloudinary.js
│ ├── db.js
│ └── utils.js
├── vercel.json # Vercel deployment config
└── server.js # Main server file
- Password Hashing: All passwords are hashed using bcryptjs
- JWT Authentication: Secure token-based authentication
- Protected Routes: Middleware to protect sensitive endpoints
- CORS Configuration: Proper cross-origin resource sharing setup
- Environment Variables: Sensitive data stored in environment variables
- This server handles a public chat application
- All user data (except passwords) may be visible to other users
- Implement proper data validation on frontend
- Warn users against using real names or personal information
- Consider implementing additional privacy features if needed
The server connects to MongoDB using Mongoose. Make sure your MongoDB instance is running and accessible with the provided MONGO_URI
.
Profile pictures are handled through Cloudinary. Configure your Cloudinary credentials in the environment variables.
The server implements comprehensive error handling:
- Database connection errors
- Authentication errors
- Validation errors
- File upload errors
- Socket connection errors
- Start MongoDB service
- Set up environment variables
- Install dependencies:
npm install
- Run development server:
npm run server
- The server will run on
http://localhost:5000
by default