The REST API allows you to perform CRUD (Create, Read, Update, Delete) operations on a "Person" model with id and name attributes.
The Person API provides a RESTful interface to manage person records with basic information.
Before you begin, ensure you have met the following requirements:
-
Node.js and npm installed.
-
MongoDB set up and running.
-
Git (for cloning the repository).
-
Clone the repository:
git clone https://github.com/Akinlua/HNG-two.git
-
Install dependencies
npm install
-
Create a .env file in the project root and configure your Mongodb connection. Also, add the port on which you want the server to listen to (optional, server listen to 3000 by default)
MONGODB_URI = your_mongodb-uri
PORT = your_port_value
To start the API Locally, run:
npm start
Your API will be accessible at http://localhost:PORT
The full API docs can be found in the file DOCUMENTATION.md
Here are some examples on how to use the API:
You can use any application such as Postman to make the requests
POST /api
Content-Type: application/json
.Request:
{
"name": "Elon Musk"
}
.Response:
{
"person": {
"name": "Elon Musk",
"_id": "64fecbf1623b0333646ac6cb",
"__v": 0
}
}
GET /api/{id}
.Response:
{
"person": {
"_id": "64fecbf1623b0333646ac6cb",
"name": "Elon Musk",
"__v": 0
}
}
PATCH /api/{id}
Content-Type: application/json
.Request:
{
"name": "Updated Elon Musk"
}
.Response:
{
"person": {
"name": "Updated Elon Musk",
"_id": "64fecbf1623b0333646ac6cb",
"__v": 0
}
}
DELETE /api/{id}
.Response:
{
"message": "Person 'Updated Elon Musk' has been deleted"
}
Content-Type: application/json
If a wrong ID was inputted you could get the below error
. Body:
{
"error": "Person Not Found"
}
If the url isn't correct or doesn't exist, you would get the below error
. Body:
{
"error": "Not Found"
}
Cast error: if there was a problem with the id in the url, an error similar to the below will show
. Body:
{
"error": "No item found with id: 64fe0e3c5"
}
Content-Type: application/json
Validation error: if there was an error with your input such as not providing a name you will receive a similar error to the one below with the exact problem
. Body:
{
"error": "Validation Error: must provide a name""
}
Content-Type: application/json
If there was any other error with the server, you will get the below error
.Body:
{
"error": "Something Went wrong"
}