This server provides two main functionalities:
- Current Share Price Retrieval: Fetches the current market price of a specified stock symbol using the Yahoo Finance API.
- Real-time Streaming Data: Streams real-time data from a Fluvio topic to clients via Server-Sent Events (SSE).
The server is built with Node.js, Express, and integrates with Yahoo Finance and Fluvio.
- Price Endpoint: Retrieve the current price of a stock. Defaults to Apple Inc. (AAPL) but can be changed via query parameters.
- Stream Endpoint: Stream real-time data from a specified Fluvio topic.
- Node.js (v18 or higher recommended)
- bun (or npm)
- Fluvio client library (
@fluvio/client) - Yahoo Finance API library (
yahoo-finance2)
-
Install dependencies:
bun install
-
Set environment variables:
Ensure you have an environment variable
PORTset, or it will default to8080.
No additional configuration is required. The server uses default settings for both endpoints.
To start the server, run:
bun devThe server will start and listen on the port defined by PORT or default to 8080.
- URL:
/price - Method:
GET - Query Parameters:
symbol(optional): The stock symbol to retrieve the price for. Defaults toAAPLif not specified.
- Response:
symbol: The stock symbol.price: The current price of the stock (with an additional random value for demonstration purposes).time: The timestamp of the response.
Example Request:
GET http://localhost:8080/price?symbol=MSFT
Example Response:
{
"symbol": "MSFT",
"price": 342.15,
"time": 1692294246794
}- URL:
/stream/:topic - Method:
GET - URL Parameters:
topic: The Fluvio topic to stream data from.
- Response: Streams data in Server-Sent Events (SSE) format.
Example Request:
GET http://localhost:8080/stream/my-topic
Example Response:
{"symbol":"MSFT","price": 342.15,"time": 1692294246794}
{"symbol":"MSFT","price": 344.11,"time": 1692294246795}
- For
/price: Returns a500status code and error message if there is an issue retrieving the price. - For
/stream: Returns a500status code and error message if there is an issue with streaming.