A simple CRUD API with Redis caching, RabbitMQ Queuing, Message broadcasting using Websocket and Server Sent Events
- Node.js
- Express
- PostgreSQL
- Prisma
- Docker
- Redis
- RabbitMQ
- Websocket
- Node.js
- Docker
-
Clone the respository
git clone https://github.com/RCOM363/health-record-api cd health-record-api -
Environment variable setup
Create an
.envfile in root directory & configure the following variablesPORT=3000 CORS_ORIGIN=* DATABASE_URL=<postgreSQL_DB_url> REDIS_URL="redis://redis:6379" // docker url RABBITMQ_URL="amqp://guest:guest@rabbitmq:5672" // docker url TOKEN_SECRET=<secret>
-
Setup Prisma Client
npx prisma generate npx prisma migrate dev npx prisma db push
-
Run the server
docker-compose up health-record-api
NOTE: You will get connection error with RabbitMQ but it will connect after few tries
- URL :
/api/v1/auth/login - Method :
POST - Body :
{ "email": "john@gmail.com", "password": "pass#123" } curlcommand :curl -X POST http://localhost:PORT/api/v1/auth/login \ -H "Content-Type: application/json" \ -d '{"email": "john@gmail.com","password": "pass#123"}'
- URL :
/api/v1/records - Method :
POST - Body :
{ "name": "John Doe", "age": 25, "status": "Healthy" } curlcommand :curl -X POST http://localhost:PORT/api/v1/records \ -H "Content-Type: application/json" \ -d '{"name": "John Doe", "age": "25", "status": "Healthy"}'
- URL :
/api/v1/records/:id - Method :
GET curlcommand :curl http://localhost:PORT/api/v1/records/{id}
- URL :
/api/v1/records/:id - Method :
PUT - Body :
{ "age": 20 } curlcommand :curl -X POST http://localhost:PORT/api/v1/records/{id} \ -H "Content-Type: application/json" \ -d '{"age": "20"}'
- URL :
/api/v1/records/:id - Method :
DELETE curlcommand :curl -X DELETE http://localhost:PORT/api/v1/records/{id}
Connect to the websocket server
- Endpoint :
http://localhost:PORT/ws - Use
https://hoppscotch.io/realtime/websocketto test and addws://localhost:PORT/wsas URL and hit connect
Connect to the SSE at
- Endpoint :
http://localhost:PORT/sse/health-updates - Use
curlto testcurl http://localhost:PORT/sse/health-updates
An HTML file (public/test.html) is included to manually test all API endpoints and real-time functionalities using WebSockets and SSE.
- Organized the code into separates
modules(models, controllers, routes etc). - Single server to handle
API,WebsocketandSSE. - Created a separate Notification worker to consume the messages from the queue, keeping the processes decoupled.
- Used docker to isolate environments for
Node.js server,Redis,RabbitMQandWorker, each run in their own container.