This project demonstrates how to set up a Node.js server that retrieves dummy JSON data, stores it in MongoDB, and provides an API for accessing, filtering, and sorting the data.
- MongoDB Integration: Uses MongoDB for storing and retrieving data. Data is fetched from an external source and initialized in the database.
- Data Processing: Filtering and sorting operations are performed in-memory after retrieving data from MongoDB.
- Error Logging: Errors are logged to the console for debugging purposes.
- Node.js (version 12.0.0 or above)
- npm (version 6.0.0 or above)
- MongoDB (version 4.0 or above)
- Clone the repository:
git clone https://github.com/jiya-singhal/nodejs-dummy-data-project.git
cd nodejs-dummy-data-project
- Create a
.env
file: In the root directory, create a .env file and add the following environment variables:
- DATA_URL=<insert_data_source_url_here>
- PORT=3000
- MONGO_URI=<your_mongodb_connection_string>
- Install dependencies:
- dotenv - For managing environment variables:
npm install dotenv
- axios - For making HTTP requests:
npm install axios
- mongodb - For interacting with MongoDB:
npm install mongodb
- Initialize the database: Run the script to fetch and store the initial data in MongoDB:
node initialize.js
- Start the server:
node server.js
The server will be available at http://localhost:3000
- server.js: Initializes the Express application.
- initialize.js: Fetches and inserts initial data into the database.
- routes/dataRoute.js: Defines the API endpoints.
- controllers/dataController.js: Contains the business logic for data handling.
- services/dataService.js: Manages database interactions.
- utils/dataUtils.js: Utility functions for processing data.
- Retrieve All Data
- Function: getData
- Endpoint: GET /api/data
- Description: Retrieves all records from the database. No query parameters are required.
- Filter Data
- Function: filterData
- Endpoint: GET /api/data?filter=key:value[,key:value]
- Description: Filters data based on the provided key-value pairs. Multiple filters can be applied, separated by commas.
- Example: GET /api/data?filter=language:Sindhi
- Sort Data
- Function: sortData
- Endpoint: GET /api/data?sort=key:order
- Description: Sorts data by the specified field (key) and order (asc or desc).
- Example: GET /api/data?sort=version:asc
- Filter and Sort Data
- Function: Combination of filterData and sortData
- Endpoint: GET /api/data?filter=key:value[,key:value]&sort=key:order
- Description: Applies both filtering and sorting to the data.
- Example: GET /api/data?filter=language:Hindi&sort=id:desc
- Unrecognized Query Parameters:
- Description: Handles cases where query parameters are not recognized.
- Response: { "error": "Invalid query parameter", "message": "The following query parameter(s) are not recognized: " }
- Invalid Filter Format:
- Description: Handles invalid filter formats.
- Response: { "error": "Filter application error", "message": "Filter error: Invalid filter format: " }
- No Matching Items:
- Description: Handles cases where no items match the filter.
- Response: { "error": "Filter application error", "message": "Filter error: No items match this filter: " }
- Invalid Sort Format:
- Description: Handles invalid sort formats.
- Response: { "error": "Sort application error", "message": "Sort error: Invalid sort format. Expected 'field:asc' or 'field:desc', got ''" }
- Launch Postman and create a new request.
- Set the request type to GET.
- Enter the appropriate URL (e.g., http://localhost:3000/api/data).
- Add query parameters as needed.
- Click Send to execute the request and view the response.
- url :
http://localhost:3000/api/data?filter=language:Sindhi
- Description: Retrieves records where the language is Sindhi.
-
Description: Sorts the records by the version in ascending order.
-
url :
http://localhost:3000/api/data?sort=version:asc
-
Description: Retrieves records where the language is Uyghur and sorts them by ID in descending order.
-
url :
http://localhost:3000/api/data?filter=language:Uyghur&sort=id:desc
-
Unknown query parameters
-
Invalid sort format
-
No matching items
-
Invalid filter format
To test and interact with the API, you can use the provided Postman collection. It includes example requests and responses for the API endpoints.
-
Download the Collection:
-
Open Postman:
- Launch Postman on your computer.
-
Import the Collection:
- Click on the "Import" button in Postman.
- Choose the downloaded JSON file or paste the link to the file.
- Click "Import" to add the collection to your workspace.
-
Explore the Requests:
- Once imported, you can explore the requests and responses included in the collection.
- Run requests to test the API endpoints and view example responses.
Here are some example requests included in the collection:
- Get All Data: Fetches all data from the API.
- Endpoint:
GET /api/data
- Response: Contains a list of data with various attributes.
- Endpoint:
For more details on each request, refer to the imported collection in Postman.
For more information on the API endpoints and how to use them, refer to the API Documentation.