This project is a server-side application built with Node.js and MongoDB that fetches cryptocurrency data (Bitcoin, Ethereum, Matic) from the CoinGecko API and provides various statistics through a set of API routes. The background service runs every 2 hours to update the cryptocurrency prices in the MongoDB database.
The API is hosted at: https://koinx-assignment-4cav.onrender.com
- Node.js
- MongoDB
- npm (Node Package Manager)
-
Clone the repository:
git clone <repository-url>
-
Install dependencies:
npm install
-
Create a
.envfile with the following content:MONGO_URI=<Your MongoDB URI> -
Start the server:
npm start
-
Get Latest Cryptocurrency Stats
- Route:
/api/stats - Method:
GET - Query Params:
coin: Must be one ofbitcoin,ethereum,matic
- Hosted Endpoint: https://koinx-assignment-4cav.onrender.com/api/stats?coin=bitcoin
- Sample Request:
GET /api/stats?coin=bitcoin - Sample Response:
{ "price": 40000, "marketCap": 800000000, "24hChange": 3.4 }
- Route:
-
Get Price Standard Deviation
- Route:
/api/deviation - Method:
GET - Query Params:
coin: Must be one ofbitcoin,ethereum,matic
- Hosted Endpoint: https://koinx-assignment-4cav.onrender.com/api/deviation?coin=bitcoin
- Sample Request:
GET /api/deviation?coin=bitcoin - Sample Response:
{ "deviation": 4082.48 }
- Route:
- The application runs a background job every 2 hours using node-cron that fetches the current price, market cap, and 24-hour price change for Bitcoin, Ethereum, and Matic from the CoinGecko API, and stores it in the MongoDB database.
crypto-app/
│
├── .env # Environment variables
├── package.json # Project dependencies
├── index.js # Main application entry point
├── models/
│ └── cryptoModel.js # Mongoose schema for storing crypto data
├── services/
│ └── fetchCryptoData.js # Service to fetch crypto data from CoinGecko
│ └── saveCryptoData.js # Service to save the fetched data to MongoDB
└── node_modules/
- Express: For creating API routes
- Mongoose: For interacting with MongoDB
- Node-Cron: For scheduling background tasks
- Axios: For making HTTP requests to the CoinGecko API