This Node.js project sets up a server that fetches and stores dummy JSON data in a MongoDB database. The server provides an API to retrieve this data with options for filtering and sorting.
- Node.js (v18.x or later)
- npm (Node Package Manager)
- MongoDB Atlas Account (for cloud-based MongoDB) or local MongoDB setup
-
Clone the Repository
git clone https://github.com/CodeMaverick2/DummyDataServer.git cd DummyDataServer
-
Install Dependencies
npm install express axios dotenv mongodb
-
Set Up Environment Variables
Create a
.env
file in the root directory with the following content:API_KEY=<DummyData_Url> MONGODB_URI=Your MongoDB URI here PORT=3000
Getting Your MongoDB URI:
- MongoDB Atlas:
- Go to the MongoDB Atlas website.
- Sign up or log in to your account.
- Create a new cluster or use an existing one.
- Navigate to the "Database Access" section to create a new database user with the appropriate permissions.
- Go to the "Network Access" section to allow access from your IP address.
- Navigate to the "Connect" section and choose "Connect your application".
- Copy the connection string provided and replace
Your MongoDB URI here
in your.env
file. Make sure to include the credentials and database name as required.
- MongoDB Atlas:
-
Initialize the database This starts the initalization script that fetches the dummy json data and stores it in MongoDB. This script should run before starting the server one time to populate our database with dummy json data.
node initialize.js
-
Start the Server
You have two options to start the server:
-
Using
npm start
:This command starts the server using the
node
command. It will not automatically reload the server when files are changed, making it suitable for production.npm start
-
Using
npx nodemon server.js
:This command starts the server with
nodemon
, which automatically reloads the server when changes are detected in your code. This is useful during development to see changes without restarting the server manually.npx nodemon server.js
You can choose either method based on your needs.
-
Endpoint: GET /api/data
Description: Returns all dummy data.
Example Request:
GET http://localhost:3000/api/data
Example Response:
[
{
"_id": "66a8aedfa13c60e969f47361",
"name": "Adeel Solangi",
"language": "Sindhi",
"id": "V59OF92YF627HFY0",
"bio": "Donec lobortis eleifend condimentum. Cras dictum dolor lacinia lectus vehicula rutrum. Maecenas quis nisi nunc. Nam tristique feugiat est vitae mollis. Maecenas quis nisi nunc.",
"version": 6.1
},
...
]
Endpoint: GET /api/data?filter=<field>:<value>
Description: Filtering is case-insensitive and uses partial matching. Sorting order is default ascending(asc) we can set it to descending(desc).
Parameters:
filter
: A filter query string in the format<field>:<value>
Example Request:
GET http://localhost:3000/api/data?filter=language:Sindhi
Example Response:
[
{
"_id": "66a8aedfa13c60e969f47361",
"name": "Adeel Solangi",
"language": "Sindhi",
"id": "V59OF92YF627HFY0",
"bio": "Donec lobortis eleifend condimentum. Cras dictum dolor lacinia lectus vehicula rutrum. Maecenas quis nisi nunc. Nam tristique feugiat est vitae mollis. Maecenas quis nisi nunc.",
"version": 6.1
}
]
Endpoint: GET /api/data?sort=<field>
Description: Returns data sorted by the specified field.
Parameters:
sort
: The field to sort by.
Example Request:
GET http://localhost:3000/api/data?sort=version:asc
Example Response:
[
{
"_id": "66a8aedfa13c60e969f47389",
"name": "Semet Alim",
"language": "Uyghur",
"id": "HI7L2SR4RCS8C8CS",
"bio": "Duis commodo orci ut dolor iaculis facilisis. Ut viverra quis eros eu tincidunt. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"version": 1.01
},
{
"_id": "66a8aedfa13c60e969f4744e",
"name": "Semet Alim",
"language": "Uyghur",
"id": "HI7L2SR4RCS8C8CS",
"bio": "Duis commodo orci ut dolor iaculis facilisis. Ut viverra quis eros eu tincidunt. Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"version": 1.01
}
]
Endpoint: GET /api/data?filter=<field>:<value>&sort=<field>
Description: Returns data filtered and sorted based on the provided parameters.
Parameters:
filter
: A filter query string in the format<field>:<value>
sort
: The field to sort by.
Example Request:
GET http://localhost:3000/api/data?filter=language:Hindi&sort=version:desc
Example Response:
[
{
"_id": "66a8aedfa13c60e969f473e8",
"name": "Chetana Hegde",
"language": "Hindi",
"id": "J9GS1RODDZL325LK",
"bio": "Aliquam sollicitudin ante ligula, eget malesuada nibh efficitur et. Nulla finibus massa at viverra facilisis. Nam tristique feugiat est vitae mollis. Phasellus tincidunt sollicitudin posuere.",
"version": 9.99
},
{
"_id": "66a8aedfa13c60e969f474ad",
"name": "Chetana Hegde",
"language": "Hindi",
"id": "J9GS1RODDZL325LK",
"bio": "Aliquam sollicitudin ante ligula, eget malesuada nibh efficitur et. Nulla finibus massa at viverra facilisis. Nam tristique feugiat est vitae mollis. Phasellus tincidunt sollicitudin posuere.",
"version": 9.99
}
]
- 400 Bad Request: Returned if the
filter
orsort
parameters are invalid or incorrectly formatted. For example, if thefilter
parameter does not follow thefield:value
format or if thesort
field is missing. - 404 Not Found: Returned if the requested data or endpoint is not found.
- 500 Internal Server Error: Returned for unexpected server errors, such as database connectivity issues or errors in data processing.
GET http://localhost:3000/api/data
GET http://localhost:3000/api/data?filter=name:Anita%20rajput
Here instead of spacing we have used %20 this ensures that url's are correctly interpreted in some cases it may work with a spacing as we can see in image below.
GET http://localhost:3000/api/data?sort=version:desc
In this request the default ordering is ascending for version we can specify if we want but the default is ascending we can alter it to descending
GET http://localhost:3000/api/data?filter=language:Hindi&sort=version
In case of invalid queries error handling is done
In case of incomplete qeuries we get error saying respective field is not present in the data