The Counter API Service is a lightweight, RESTful web service built on Node.js with the primary focus on managing and manipulating individual numeric counters. Through its endpoints, it offers clients the ability to create, read, update, and delete counters, and to adjust their numeric values based on predefined operations.
-
List All Counters: Users can retrieve a complete list of all counters currently stored within the system.
-
Unique Counter Identification: Every counter is uniquely identified using a timestamp-based ID, ensuring that each counter can be individually accessed and managed without conflicts.
-
Value Manipulation: Users can easily increment, decrement, or reset specific counter values, allowing for real-time adjustment based on user interactions or events.
-
JSON-based Persistent Storage: All counter data is securely and efficiently stored within a JSON file. This ensures data retention across system reboots and easy backup or transfer.
-
Robust Error Handling: The API provides clear error messages, making it user-friendly and easy to debug. Whether a counter is not found or there's an issue with the operation type, users are kept informed.
-
Web-based Click Counters: Developers can implement this API to track and display the number of clicks on specific elements or pages.
-
Polling Systems: For systems that track user preferences or votes, each option could be represented by a counter.
-
Performance or Event Trackers: In scenarios where specific events (like downloads, uploads, or user logins) need to be tracked, each event can be a counter that increments with each occurrence.
base url - http://localhost:3050
| Method | Endpoint | Query Params | Body | Description | Response |
|---|---|---|---|---|---|
| GET | /api/counters | - | - | Retrieves the list of all counters | JSON array of counters |
| POST | /api/counters | - | {} |
Creates a new counter with an autogenerated ID and initial value of 0 | JSON object of the newly created counter |
| GET | /api/counters/:id | - | - | Retrieves a counter based on the provided ID | JSON object of the specific counter |
| PUT | /api/counters/:id | type=increment, decrement, reset | - | Updates the counter's value based on the type query param | Updated JSON object |
| DELETE | /api/counters/:id | - | - | Deletes a counter based on the provided ID | JSON object of the deleted counter |
- For the POST endpoint
/api/counters, the body doesn't necessarily need to contain any specific information as it is mostly auto-generated. The ID is generated based on the current timestamp and the value is always set to 0. - The PUT endpoint
/api/counters/:idexpects one of three values in the query parametertype: 'increment', 'decrement', or 'reset'. Depending on the value, it increments, decrements, or resets the counter value to 0. - If a specific counter is not found for the GET, PUT, or DELETE operations, an empty JSON object
{}is returned.
-
Clone the Repository: Use Git to clone the repository to your local machine:
git clone https://github.com/dctacademy/counter-api-service.git
-
Navigate to the Project Directory:
cd counter-api-service -
Install Dependencies: Most Node.js projects use npm (Node Package Manager) or Yarn to manage dependencies. Check if there's a
package.jsonfile in the directory. If it exists, you can install the required packages using:npm install
Or if there's a
yarn.lockfile, you might want to use:yarn
-
Run the Service: Once everything is set up, you can typically start the service with:
npm start
Or, if the project uses Yarn:
yarn start
-
Access the Service: If everything is set up correctly and the service starts without any issues, you should be able to access the API through your browser or tools like Postman at the address specified