A Node.js-based backend for a real-time chat application, built using Express, MongoDB, and Socket.io. This API enables user registration/login, secure messaging, and real-time communication with persistent storage.
- 🔐 User Authentication with JWT and hashed passwords using bcrypt
- 📩 Real-Time Messaging using Socket.io
- 🧾 Persistent Chat History with MongoDB
- 📡 REST APIs for users and messages
- 🌐 CORS-enabled for frontend integration
- 🧪 Structured error handling and scalable design
- Backend: Node.js, Express
- Database: MongoDB (via Mongoose)
- Authentication: JWT, bcrypt
- WebSocket: Socket.io
- Environment Variables: dotenv
- Frontend Compatibility: CORS enabled for
http://localhost:3000
├── models/
│ ├── User.js
│ └── Messages.js
├── routes/
│ └── auth.js
├── .env
├── server.js
├── package.json
Create a .env file in the root:
PORT=5001
MONGO_URI=your_mongodb_connection_string
JWT_SECRET=your_jwt_secret
-
Clone the repository
git clone https://github.com/your-username/chat-backend.git cd chat-backend -
Install dependencies
npm install
-
Start the server
npm start
POST /auth/register– Register userPOST /auth/login– Login user
GET /users?currentUser=username– Fetch all users except current
GET /messages?sender=A&receiver=B– Fetch conversation between two usersSocket: send_message– Send real-time messageSocket: receive_message– Receive messages
connection– Connect new usersend_message– Emit message to receiverreceive_message– Broadcast new messagedisconnect– Handle disconnection