Chat App’s backend is a Node.js process that runs two communication layers inside the same HTTP server. Express handles stateless REST requests for authentication, messaging, and user discovery. Socket.io shares the same underlyingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/khushboodaryani/Chat-App/llms.txt
Use this file to discover all available pages before exploring further.
http.Server instance, so WebSocket upgrade handshakes arrive on the same port without any proxy or separate service. A JWT-based protectRoute middleware guards every private endpoint, and in production the compiled React application is served as static files so the entire product ships from a single origin.
Server Bootstrap
server.js is the application entry point. It wires together middleware, routes, static file serving, and the database connection in a deterministic order.
app and server are imported from socket/socket.js, not created in server.js. This ensures the Socket.io instance is attached to the HTTP server before any route or middleware is registered.Route Structure
| Prefix | Router file | Protected |
|---|---|---|
/api/auth | routes/auth.routes.js | No — handles signup, login, and logout |
/api/messages | routes/message.routes.js | Yes — protectRoute middleware applied |
/api/users | routes/user.routes.js | Yes — protectRoute middleware applied |
JWT Middleware (protectRoute)
protectRoute is an Express middleware that runs before every handler in the messages and users routers.
- Reads
req.cookies.jwt(set as anhttpOnlycookie by the login/signup handlers). - Verifies the token against
process.env.JWT_SECRET. - Looks up the user in MongoDB using the
userIdclaim, explicitly excluding thepasswordfield with.select('-password'). - Attaches the result to
req.userand callsnext().
401 Unauthorized and the handler never executes.
Static File Serving and the Catch-All Route
express.static serves the Vite build artefacts (JS bundles, CSS, images) directly from the filesystem. The wildcard GET * catch-all sits after all API routes and returns index.html for any path that was not matched earlier. This is what allows React Router to manage client-side URLs like /login or /signup — the browser always receives the SPA shell, and routing happens in JavaScript.
Socket.io Server
The Socket.io server is created inbackend/socket/socket.js and exported alongside app and server.
- Shared HTTP server — because
iowraps the sameserverthat Express runs on, WebSocket upgrades happen on the same port as REST calls. No reverse-proxy rules are needed to routews://traffic separately. userSocketMap— an in-memory object that maps each logged-inuserIdto their current socket ID. Message controllers callgetReceiverSocketId(receiverId)to find the target socket and emitnewMessagedirectly to that connection.- Online-user broadcast — every time a socket connects or disconnects,
io.emit("getOnlineUsers", ...)broadcasts the updated list to all clients. The frontend’sSocketContextlistens for this event and updates theonlineUsersarray.
Exported Symbols
socket.js is the authoritative source for three exports used across the backend:
| Export | Type | Used by |
|---|---|---|
app | Express application | server.js — middleware and route registration |
server | http.Server | server.js — server.listen(PORT, ...) |
io | Socket.io Server | Message controller — io.to(socketId).emit(...) |
getReceiverSocketId | Function | Message controller — resolves a userId to a live socket ID |