Deployed on Cyclic- https://plain-getup-worm.cyclic.app/api/
- Clone the repository:
git clone https://github.com/n1ght-k1ng/stock-price-view-app
-
Navigate to the Project Directory
cd stock-price-view-app
-
Install Dependencies
npm install
-
Setup Environment Variables Create a .env file in the root directory and add the necessary environment variables:
MONGODB_URI = your_mongodb_uri
with npm start
The API will be accessible at http://localhost:8001/api.
- URL:
/
- Method:
GET
- Example:
https://plain-getup-worm.cyclic.app/api/
- Description: Refreshes stock data.
- URL:
/stock-by-date/:date
- Method:
GET
- Parameters:
date
: Date in the format YY-MM-DD
- Description: Retrieves stock data for a specific date.
- URL:
/top10stocks
- Example:
https://plain-getup-worm.cyclic.app/api/top10stocks
- Method:
GET
- Description: Retrieves information about the top 10 stocks of the last market day.
- URL:
/stock-by-name/:name
- Method:
GET
- Example
https://plain-getup-worm.cyclic.app/api/stock-by-name/mrf
- Parameters:
name
: Name of the stock
- Description: Retrieves stock data by name by regex search.
- URL:
/stock-history/:code
- Method:
GET
- Example
https://plain-getup-worm.cyclic.app/api/stock-history/500038
- Parameters:
code
: Stock code
- Description: Retrieves historical stock data.
- URL:
http://localhost:8001/api/post-favorite/:code
- Test Local:
curl -X POST http://localhost:8001/api/post-favorite/:code
- Test Remote
curl -X POST https://plain-getup-worm.cyclic.app/api/post-favorite/500267
- Method:
POST
- Parameters:
code
: Stock code
- Description: Adds a stock to favorites.
- Example Codes - 500038 , 500267 , 500268
- URL:
/favorites
- Example:
https://plain-getup-worm.cyclic.app/api/favorites
- Method:
GET
- Description: Retrieves a list of favorite stocks.
- URL:
http://localhost:8001/api/delete-favorite/:code
- Method:
DELETE
- Test Local:
curl -X DELETE http://localhost:8001/api/delete-favorite/:code
- Example
curl -X DELETE https://plain-getup-worm.cyclic.app/api/delete-favorite/500267
- Parameters:
code
: Stock code
- Description: Removes a stock from favorites.
- Example Codes - 500038 , 500267 , 500268
Caching is implemented to reduce redundant requests and improve response time. The cache duration is set based on the time until the end of the day.
Method: cacheMiddleware(ttl)
ttl: Time to live for cache in seconds.
Example Usage:
router.get('/', cacheMiddleware(getTimeUntilEndOfDayInSeconds()), removeDataBefore50thDay, fetchStockInfo);
How to Use
Method: validateDateFormat
Example Usage:
router.get('/stock-by-date/:date', cacheMiddleware(60 * 60 * 24 * 5), validateDateFormat, getStockByDate);
How to Use
Method: validateStockCode
Example Usage:
router.get('/stock-history/:code', validateStockCode, cacheMiddleware(60 * 60 * 24 * 5), getStockHistory);
How to Use
Method: checkStockIfPresent
Example Usage:
router.post('/post-favorite/:code', validateStockCode, checkStockIfPresent, postFavorite);
Endpoint URL: /
Method: GET
Description: Initiates the data refresh process.
Example Usage:
router.get('/', cacheMiddleware(getTimeUntilEndOfDayInSeconds()), removeDataBefore50thDay, fetchStockInfo);
Sequence:
- The
GET
request to / triggers the middleware, checking for cached data. - If cached data is present, it is sent as a response.
- If not cached, the
removeDataBefore50thDay
function removes outdated data. - The
fetchStockInfo
function fetches and inserts new stock data into the database. - The updated data is now available for subsequent requests.