This is the backend API and realtime WebSocket server for Hubbie Chat, a full-stack MERN real-time chat application. It is built using Node.js, Express, MongoDB/Mongoose, and Socket.IO.
- REST API: Handlers for user authentication, contact search, chat room creation, and message fetching.
- Cookie-based JWT Auth: Synchronous token validation and secure cookie sessions.
- Realtime WebSockets: Powered by Socket.IO with connection-level authentication and strict room participation checks.
- Email Verification & Password Reset: Uses Nodemailer (SMTP) for sending verification OTPs (cryptographically secure) and password reset tokens (TTL indexed).
- Robust Error Handling & Database Resilience: Automatic cleanup of stale reset tokens and graceful connection teardown on process exit signals (
SIGINT,SIGTERM).
- Node.js: v18.x or higher recommended.
- MongoDB: A running MongoDB instance (local or Atlas cluster).
- SMTP Account: A mail account (e.g., Gmail App Password) to send verification and password reset emails.
-
Navigate to the Backend directory:
cd backend -
Install dependencies:
npm install
-
Configure Environment Variables: Create a
.envfile in the root of thebackend/directory and configure the following variables:# Database Configuration MONGO_URI=your_mongodb_production_uri DB_DEV_URI=your_mongodb_development_uri # Server Configuration PORT=4000 CLIENT_URL=https://hubbie-chat.onrender.com NODE_VERSION=18.15.0 # SMTP Configuration (Nodemailer) MAIL_USERNAME=lastbenchscholar@gmail.com MAIL_PASSWORD=your_email_app_password # Security / JWT SECRET=your_jwt_signing_secret_key # SSO / External Service Configuration SSO_API_KEY=your_sso_api_key SSO_SERVER_URL=http://localhost:5000 # JWT Verification details JWT_ISSUER=LBS-AUTH-SERVER JWT_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----\nyour-public-key-here\n-----END PUBLIC KEY-----"
-
Development Mode (with Nodemon): Runs the server with auto-reload enabled when source files change.
npm run dev
-
Production Mode: Runs the server with standard node engine.
npm start
For structural details, models, and endpoints, please refer to the project_context.md file.