Skip to main content

Documentation 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.

Chat App is packaged as a single Node.js process that serves both the REST API, WebSocket connections, and the compiled React frontend from one port. There is no separate frontend server in production — Express statically serves the Vite build output from frontend/dist and a catch-all route hands every non-API request to index.html so React Router can handle client-side navigation.

Prerequisites

Before you begin, make sure the following are available on your server or cloud platform:
  • Node.js 18 or later — the backend uses ES module syntax (import/export)
  • npm — bundled with Node.js; used to install dependencies and build the frontend
  • MongoDB — either a free MongoDB Atlas cluster or a self-hosted MongoDB instance reachable from your server
  • Git — to clone the repository

Deployment Steps

1

Clone the repository

Pull the source code onto your server and enter the project root:
git clone https://github.com/khushboodaryani/Chat-App.git && cd Chat-App
2

Install backend dependencies

From the repository root, install the Node.js dependencies that power the Express and Socket.io server:
npm install
3

Install frontend dependencies

Move into the frontend/ directory, install its packages, then return to the root:
cd frontend && npm install && cd ..
4

Build the React application

Vite compiles and bundles the React source into static files. The output is written to frontend/dist, which Express will serve in production:
cd frontend && npm run build && cd ..
5

Create the .env file

Create a .env file at the repository root (the same directory that contains /backend and /frontend). dotenv.config() is called inside backend/server.js, which resolves paths relative to the root:
MONGO_URI=mongodb+srv://user:password@cluster.mongodb.net/chat-app
JWT_SECRET=your-very-long-random-secret-here
PORT=4000
NODE_ENV=production
See the Environment Variables reference for a full description of each variable.
6

Start the server

Launch the Node.js process directly or via the npm script:
node backend/server.js
Alternatively, if you have the root-level npm scripts available:
npm run server
You should see Server Running on port 4000 in the console once the process connects to MongoDB successfully.
7

Verify the deployment

Open a browser or run a quick health check to confirm the app is reachable:
curl http://your-server-ip:4000
The app is now live at http://your-server-ip:4000. All API routes are served under /api/*, and every other request returns the compiled React app.

Keeping the Process Running with PM2

Node.js processes exit if they crash or if the terminal session closes. Use a process manager like PM2 to automatically restart the app and keep it alive across reboots:
npm install -g pm2
pm2 start backend/server.js --name chat-app
pm2 save          # persist the process list across reboots
pm2 startup       # generate and enable the system startup script
To view logs and monitor the process:
pm2 logs chat-app
pm2 status

Putting the App Behind a Reverse Proxy

Run nginx or Caddy in front of Node.js to terminate TLS, handle HTTP→HTTPS redirects, and optionally serve on port 80/443. When TLS is terminated at the proxy, ensure NODE_ENV=production is set in your .env file so that the JWT cookie is issued with the Secure flag — browsers will not send Secure cookies over plain HTTP.
A minimal nginx server block for this setup:
server {
    listen 443 ssl;
    server_name chat.example.com;

    # ... ssl_certificate / ssl_certificate_key directives ...

    location / {
        proxy_pass         http://127.0.0.1:4000;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection "upgrade";
        proxy_set_header   Host $host;
    }
}
The Upgrade and Connection headers are required to proxy Socket.io’s WebSocket connections correctly.

Security Reminders

Do not expose your MongoDB port (default 27017) directly to the internet. If you are using MongoDB Atlas, restrict network access to your server’s IP address in the Atlas Network Access panel. For self-hosted MongoDB, bind it to 127.0.0.1 or place it behind a VPN.
  • Store JWT_SECRET as a long, randomly generated string (32+ characters). You can generate one with openssl rand -hex 32.
  • Never commit your .env file to version control — add it to .gitignore.
  • Rotate JWT_SECRET if you suspect it has been exposed; all existing sessions will be invalidated.

Build docs developers (and LLMs) love