This is a Node.js microservice for managing book purchases in the Bazar online bookstore. It communicates
with the bazar-catalog-service to check stock availability and record orders.
bazar-order-service/
├── src/
│ ├── app.js # The main Node.js application
│ ├── db/
│ │ └── orders.json # File to store order records
│ └── package.json # List of dependencies
└── Dockerfile # Dockerfile at the root
- PORT: The port on which the service will run (default: 5002).
- CATALOG_URL: URL for the catalog service, used to check stock availability (default:
http://catalog-service:5001).
-
POST
/purchase/:item_numberInitiates a purchase for a specific book by
item_number. It checks the stock in thecatalog-service, updates the stock, and records the order.-
Request Body:
{ "qty": 1 } -
Response:
- Success:
{ "message": "Purchased <quantity> book(s): <book title>" } - Failure:
{ "error": "Out of stock" }
- Success:
-
Use Docker Compose to build and start both order-service and catalog-service in one step.
docker-compose up --buildOnce the services are running, you can access the order-service API at http://localhost:5002.
-
Purchase a Book:
POST http://localhost:5002/purchase/1 Content-Type: application/json { "qty": 2 }This request initiates a purchase of 2 units of the book with
item_number1.