How thomas131104/rest-api-basic works
Project Type & Purpose
• It’s a learning project aimed at understanding and implementing CRUD operations (GET, POST, PUT, DELETE) with a RESTful API.
• The focus is on basic API calls using a self-written API helper class (RestfulAPI in restful.js) and testing those operations via test.js.
• The project uses json-server with db.json as a mock database to simulate a real backend.
File Structure
• Top-level files:
• db.json – the JSON data file used by json-server.
• restful.js – contains the RestfulAPI class with methods to perform CRUD operations.
• test.js – a script to invoke methods from the RestfulAPI class and test the API interactions.
• package.json and .gitignore – standard configuration/metadata files.
• The structure is simple and demonstrates a clear separation between API logic and testing code.
Key Components
a. Client/Test Runner (test.js):
• Represents the part of the code that initiates API calls.
b. API Handler (RestfulAPI in restful.js):
• This is the core component that abstracts away the CRUD operations.
• It acts as the intermediary between the client code and the actual API server.
c. Mock API Server (json-server):
• Although the json-server is an external dependency started from the command line, it plays a vital role.
• It as the backend server that handles API requests against the db.json data.
d. Data Store (db.json):
• Serves as the mock database containing the JSON objects.
• It is accessed indirectly via the json-server.
Relationships and Data Flow
• Finally, illustrate the flow from the Mock API Server to the Data Store (db.json) indicating where data is read/written.
Architectural Patterns and Design Principles
• The project emphasizes modularity by separating API logic (restful.js) from test scripts (test.js) and backend simulation (db.json with json-server).
• Although the project is simple, it reflects a clear client-service separation and demonstrates basic RESTful API design patterns.
• This design also provides a good example of dependency injection where the RestfulAPI is provided a base URL, which increases its reusability.