This is the backend service for the Falai application, a real-time chat platform. It is responsible for managing users, authentication, and sending/receiving messages through a RESTful API and WebSockets.
- Backend: Node.js, Express.js
- Database: PostgreSQL
- ORM: Drizzle ORM
- Real-time Communication: Socket.IO
- Authentication: JSON Web Tokens (JWT)
- Containerization: Docker & Docker Compose
- Validation and Security:
express-rate-limitfor request rate limiting.
Before you begin, ensure you have the following software installed:
For local development without Docker, you will also need:
- Node.js (v20 or later recommended)
- PostgreSQL
Follow these instructions to get the project up and running.
git clone https://github.com/Mikaelnotfound/FALAI-backend.git
cd FALAI-backendCreate a .env file in the project root. You can copy the .env.example if it exists.
# Example of creating the .env file
touch .envFill in the .env file with the required variables:
PORT: The port on which the backend server will run (e.g., 3000).HOST: The host address the backend server will bind to (e.g.,0.0.0.0for Docker,127.0.0.1for localhost).POSTGRES_USER: Your desired username for the PostgreSQL database.POSTGRES_PASSWORD: A secure password for the PostgreSQL user.POSTGRES_DB: The name of the database.DATABASE_URL: The connection string for the database. It should follow the format:postgresql://<USER>:<PASSWORD>@<HOST>:<PORT>/<DB>JWT_SECRET: A secret key for signing JSON Web Tokens.JWT_EXPIRATION: The expiration time for JWTs (e.g.,1h,7d).
Example for Docker:
When using Docker, the DATABASE_URL host should be the database service name in docker-compose.yml, which is falai-db.
DATABASE_URL="postgresql://user:password@falai-db:5432/falai"
-
Build and Run the Containers:
npm run docker:start
This command uses
docker-compose.ymlto orchestrate the application and database services. -
Apply Database Migrations: Once the containers are running, push the Drizzle schema.
docker-compose exec backend npm run db:push
The application will be available at http://localhost:3000 (or the PORT you specified).
-
Install Dependencies:
npm install
-
Set up PostgreSQL: Ensure your PostgreSQL server is running and create a database with the credentials defined in your
.envfile. -
Apply Database Migrations:
npm run db:push
-
Start the Server:
- For development with auto-reloading:
npm run dev
- For production:
npm start
- For development with auto-reloading:
npm start: Starts the server in production mode.npm run dev: Starts the server in development mode with--watch.npm run docker:start: Starts Docker containers in detached mode.npm run docker:restart: Stops and recreates Docker containers.npm run docker:stop: Stops and removes containers and volumes.npm run docker:logs: Displays logs from the backend container.npm run db:push: Synchronizes the Drizzle schema with the database.npm run db:studio: Opens Drizzle Studio to visualize and manage the database.
All endpoints requiring authentication need a JWT token in the Authorization header.
POST /login: Authenticates a user and returns a JWT token.POST /register: Creates a new user.GET /users: (Authenticated) Returns a list of all users.GET /users/:id: (Authenticated) Returns a specific user by ID.DELETE /users/:id: (Authenticated) Deletes a user by ID.
GET /messages: (Authenticated) Returns all messages.GET /messages/:id: (Authenticated) Returns a specific message by ID.GET /messages/user/:userId: (Authenticated) Returns all messages from a specific user.POST /messages: (Authenticated) Creates and sends a new message.
GET /status: Health check endpoint. Returns "OK!".
The server uses Socket.IO for bidirectional communication.
- Connection: A client connects to the server.
chat message(emission): A client sends a message to the server.chat message(reception): The server retransmits the received message to all other connected clients.user connected: Emitted when a new user connects.user disconnected: Emitted when a user disconnects.
The project uses Drizzle ORM to interact with the PostgreSQL database. The table schemas (users, messages, etc.) are defined in src/database/schema.js.
To interactively visualize your database, run:
npm run db:studio-
With Docker:
npm run docker:stop
-
Locally: Press
Ctrl + Cin the terminal where the server is running.