This mock server is built using Node.js and Express to provide a simple way to create, read, update, and delete (CRUD) entities. All data is stored in an in-memory object (store
) and persisted in a file called store.json
.
-
Initialize Store
- Reads from
store.json
once on server startup to populate thestore
object in memory.
- Reads from
-
Create Entities (POST)
- Accepts JSON data to create new entities under any specified entity type.
- Automatically generates a unique
id
(UUID).
-
Read Entities (GET)
- Retrieves a list of all entity types or all records of a specific entity type.
- Allows fetching a single entity by its
id
.
-
Update Entities (PUT)
- Updates the properties of an existing entity without allowing changes to the entity’s
id
.
- Updates the properties of an existing entity without allowing changes to the entity’s
-
Delete Entities (DELETE)
- Removes a specific entity by its
id
.
- Removes a specific entity by its
-
Write Back to
store.json
- After any POST, PUT, or DELETE operation, the updated data is written to
store.json
for persistence.
- After any POST, PUT, or DELETE operation, the updated data is written to
When making GET requests for a specific entity type, you can combine filters, search, and sorting parameters:
-
Field-based Filters
- Query format:
?key=value
- Filters out results that match the specified key-value pair(s).
- Example:
GET /products?category=Electronics
- Query format:
-
Search
- Query format:
?_q=term
- Searches for the provided term (
term
) across all fields of the entities. - Multiple Search Terms: You can include multiple
?_q
parameters to search for different terms as an OR condition. For example:This will return all products whose fields includeGET /products?_q=phone&_q=tv
phone
ORtv
(case-insensitive). - Example (single term):
GET /products?_q=phone
- Query format:
-
Sorting
- Query format:
?_sort=<field>&_order=asc|desc
- Sorts the resulting array based on the provided field in either ascending or descending order.
- Example:
GET /products?_sort=price&_order=desc
- Query format:
Note: All these operations (filters, search, sorting) can be chained together. For example:
GET /products?_q=phone&_sort=price&_order=desc&category=Electronics
Happy Mocking!