Skip to content

jiya-singhal/nodejs-dummy-data-project

Repository files navigation

nodejs-dummy-data-project with MongoDB

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.

Implementation Details

  • 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.

To run this project, we'll need:

  • Node.js (version 12.0.0 or above)
  • npm (version 6.0.0 or above)
  • MongoDB (version 4.0 or above)

Instructions to Setup

  1. Clone the repository:
   git clone https://github.com/jiya-singhal/nodejs-dummy-data-project.git
   cd nodejs-dummy-data-project
  1. 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>
  1. 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
  1. Initialize the database: Run the script to fetch and store the initial data in MongoDB:
node initialize.js
  1. Start the server:
node server.js

The server will be available at http://localhost:3000

Structure of Project

  • 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.

API Endpoints

  1. Retrieve All Data
  • Function: getData
  • Endpoint: GET /api/data
  • Description: Retrieves all records from the database. No query parameters are required.
  1. 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
  1. 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
  1. 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

Error Handling

  1. 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: " }
  1. Invalid Filter Format:
  • Description: Handles invalid filter formats.
  • Response: { "error": "Filter application error", "message": "Filter error: Invalid filter format: " }
  1. 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: " }
  1. 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 ''" }

Postman Collection

Testing the API with Postman

  • 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.

Retrieve All Data

  • url : http://localhost:3000/api/data

  • Description: Fetches all records from the database.

    • outputRetrieve All Data

Filter data according to the format requested

  • url : http://localhost:3000/api/data?filter=language:Sindhi
  • Description: Retrieves records where the language is Sindhi.
    • outputFilter data

Sort data according to the format requested

  • Description: Sorts the records by the version in ascending order.

  • url : http://localhost:3000/api/data?sort=version:asc

    • output Sort data

Filter and sort

  • 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

    • output Filter and sort

Errors

  • Unknown query parameters

    • URL: http://localhost:3000/api/data?unknownParam=test
    • Response: The following query parameter(s) are not recognized: sorted
  • Invalid sort format

    • URL: http://localhost:3000/api/data?sort=version:asec
    • Response: Sorting error: An invalid sort format!, received 'version:asec'
  • No matching items

    • URL: http://localhost:3000/api/data?filter=language=English
    • Response: Filter error: No items match this filter: language=English
  • Invalid filter format

    • URL: http://localhost:3000/api/data?filter=language
    • Response: Filter error: Invalid filter format: language

PostMan Collection Exported

API Documentation

Postman Collection

To test and interact with the API, you can use the provided Postman collection. It includes example requests and responses for the API endpoints.

How to Import the Collection

  1. Download the Collection:

  2. Open Postman:

    • Launch Postman on your computer.
  3. 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.
  4. 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.

Example Requests

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.

For more details on each request, refer to the imported collection in Postman.

API Documentation

For more information on the API endpoints and how to use them, refer to the API Documentation.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published