The ZMQ server (appZMQ.js) listens for client requests on port 5555 using the REQ-REP pattern of ZeroMQ.
Steps:
- Client sends a JSON-encoded string to the server containing the amount, source currency, and target currency.
- Server responds with the conversion result or an error message.
Example Request (ZMQ):
{
"amount": 100,
"from_currency": "USD",
"to_currency": "EUR"
}Response:
{
"type": "currency_convert_response",
"from_currency": "USD",
"to_currency": "EUR",
"original_amount": 100,
"converted_amount": 90.5,
"rate": 0.905,
"timestamp": 1698287200000
}The HTTP server runs using app.js and listens for POST requests on /convert. Clients can send a JSON request with the following fields:
amount: The amount to be converted.from_currency: The currency to convert from.to_currency: The target currency.
Example Request (HTTP):
{
"amount": 100,
"from_currency": "USD",
"to_currency": "EUR"
}Response:
{
"type": "currency_convert_response",
"from_currency": "USD",
"to_currency": "EUR",
"original_amount": 100,
"converted_amount": 90.5,
"rate": 0.905,
"timestamp": 1698287200000
}Run the HTTP server:
node app.jsThe service returns a JSON response with the following fields:
type: Always "currency_convert_response"from_currency: Original currency codeto_currency: Target currency codeoriginal_amount: Input amountconverted_amount: Converted amount in target currencyrate: Exchange rate usedtimestamp: Unix timestamp of conversion
Example successful response:
{
"type": "currency_convert_response",
"from_currency": "USD",
"to_currency": "EUR",
"original_amount": 100,
"converted_amount": 91.85,
"rate": 0.9185,
"timestamp": 1700432991657
}Error responses will have a 400 or 500 status code, and will return:
{
"error": "Error message",
"details": "Additional error details" // only on 500 errors
}sequenceDiagram
participant A as Client
participant B as Currency Converter
participant C as External API
A->>B: Send JSON via POST
opt If invalid Amount
B->>A: Return Error
end
B->>B: Build JSON request
B->>C: Get Exchange Rate
opt If invalid Currency code
C->>B: Return Error
B->>A: Return Error
end
B->>B: Build JSON Response
B->>A: Return JSON with currency