A Simple CRUD operation API for managing Persons
- Introduction
- Features
- Requirements
- Getting Started
- API Endpoints
- Error Handling
- Usage
- UML Diagram
- Testing
- Documentation
This project is a simple REST API for managing persons. It allows you to perform basic CRUD (Create, Read, Update, Delete) operations on person resources. The API is built using Node.js and MongoDB, and it provides a flexible way to handle dynamic parameters, such as adding or retrieving a person by name.
- Create, Read, Update, and Delete items.
- RESTful API endpoints.
- Simple and easy-to-understand codebase.
- Node.js and npm installed on your machine.
- Postman (for testing)
-
Clone this repository to your local machine:
git clone https://github.com/thegodhope/hng-backend-task2.git
-
Navigate to the project directory:
cd <"your_cloned_location">/hng_backend_task2
-
Install packages using
npm install
-
Start the server, use the following command:
npm start
The API will be accessible at http://localhost:8000
GET /api
: run api health checkPOST /api
: Create a new personGET /api/:id
: Fetch details of a person by IDPUT /api/:id
: Update details of an existing person by IDDELETE /api/:id
: Remove a person by ID
When using the API, you may encounter various error responses. Below are some common error scenarios and their corresponding HTTP status codes:
-
400 Bad Request: This status code is returned when the request is malformed or missing required parameters. Check your request payload and ensure it follows the API's expected format.
-
404 Not Found: When trying to access a resource that does not exist, the API responds with a 404 status code. Double-check the resource's ID or name provided in the request.
-
409 Conflict: If you attempt to create a resource that already exists, such as a person with the same name, the API will return a 409 status code. Ensure the resource is unique or consider updating the existing one.
-
500 Internal Server Error: In case of unexpected server errors, you might receive a 500 status code. If you encounter this, it's recommended to report the issue to me at here.
- Create User -Request :
post /api
content-Type: application/json
{
"name": "idorenyin"
}
- Create User -Response :
post /api
status: 200
{
"message" : "user created successfully",
"result" : {
"name" : "idorenyin",
"_id": "1",
"__v": 0
}
}
Visit here for full API documentation
- Testing
You can test the API using Postman. Use the provided Postman collection for testing CRUD operations.