Welcome to the CRUD REST API project for managing a "person" resource. This project allows you to perform basic CRUD (Create, Read, Update, Delete) operations on person data. The API is built with Node.js, Express, and MongoDB.
- Node.js
- Express.js
- MongoDB
- TypeScript
- Yarn
This project aims to create a robust REST API for managing person data. It includes endpoints for creating, reading, updating, and deleting person records. The API is designed to handle dynamic parameters, making it flexible to perform operations based on provided details.
- Create a new person record.
- Retrieve person details by name.
- Update existing person records by name.
- Delete person records by name.
- Dynamic parameter handling for flexible queries.
- Secure interactions with the database to prevent common vulnerabilities.
- Clone this repository to your local machine using
git clone
or download the ZIP file and extract it to a folder.`
git clone https://github.com/Clifftech123/hng-rest-crud-api.git
- Install the required dependencies using
yarn install
-
Create a
.env
file and configure your environment variables (e.g., database connection details). -
Run the API using
yarn dev
-
Endpoint:
POST https://hngtask1-stage-2.onrender.com/api
-
Request Format:
{ "name": "Clifford Opoku Isaiah" }
-
Response Format (Success):
{
"name": "Clifford Opoku Isaiah ",
"_id": "6501030f31ce063922818171",
"__v": 0
}
This endpoint creates a new person record in the database. The name attribute is required and must be a string. The API returns the newly created person record in the response body.
- Response Format (Error): If an number input is provided, the API returns an error message in the response body.
{
"error": "[\n {\n \"code\": \"invalid_type\",\n \"expected\": \"string\",\n \"received\": \"number\",\n \"path\": [\n \"name\"\n ],\n \"message\": \"Expected string, received number\"\n }\n]"
}
- Response Format (Error):
If an invalid input is provided, the API returns an error message in the response body.
{ "error": "Invalid input data" }
- Endpoint:
GET https://hngtask1-stage-2.onrender.com/api/:id
-
Example :
GET https://hngtask1-stage-2.onrender.com/api/6501030f31ce063922818171
-
- id (string) - The ID of the person record to retrieve.
- Response Format (Success):
{
"_id": "6501030f31ce063922818171",
"name": "Clifford Opoku Isaiah ",
"__v": 0
}
- Response Format (Error - Person not found):
{ "error": "Person not found" }
- Endpoint:
PUT https://hngtask1-stage-2.onrender.com/api/:id
- Example :
PUT https://hngtask1-stage-2.onrender.com/api/6501030f31ce063922818171
- Request Format:
{ "name": "Updated Name" }
-
- id (string) The ID of the person record to update.
-
- Response Format (Success):
{
"_id": "6501030f31ce063922818171",
"name": "Updated Name ",
"__v": 0
}
- Response Format (Error - Person not found):
{ "error": "Person not found" }
- Endpoint:
Delete https://hngtask1-stage-2.onrender.com/api/:id
- Example :
Delete https://hngtask1-stage-2.onrender.com/api/6501030f31ce063922818171
- Response Format (Success): No content (204)
-
- id (string) The ID of the person record to delete.
-
- Response Format (Error - Person not found):
{
"message": "Person deleted successfully."
}
- UML diagrams representing the API's class structure and entity-relationship design can be found in the
docs/UML_diagrams.png
folder.
This project includes automated tests for each API endpoint using Postman. The tests are located in the Postman folder
in src/Postman
and are organized by endpoint. The tests are designed to run against a local instance of the API. To run the tests, follow the steps below.
- Endpoint:
POST https://hngtask1-stage-2.onrender.com/api
- Test code
- * `Postman Collection name `: Post-Person-Test
pm.test("Create a Person - Status Code is 201", function () {
pm.response.to.have.status(201);
});
pm.test("Create a Person - Response Name", function () {
pm.response.to.have.jsonBody("name");
});
- Response Format (Pass):
- Create a Person - Status Code is 201
- Create a Person - Response Name
- Endpoint:
GET https://hngtask1-stage-2.onrender.com/api/:id
-
Postman Collection name
: Get-Person-by-id
- Test code
pm.test("Response body is valid JSON", function () {
pm.response.to.be.json;
});
- Response Format (Pass):
- Response body is valid JSON
- Endpoint:
PUT https://hngtask1-stage-2.onrender.com/api/:id
-
Postman Collection name
: update-person-by-id
- Test code
pm.test("Update a Person - Status Code is 200", function () {
pm.response.to.have.status(200);
});
- Response Format (Pass):
- Update a Person - Status Code is 200
- Endpoint:
Delete https://hngtask1-stage-2.onrender.com/api/:id
-
Postman Collection name
: Delete-Person-by-id
pm.test("Update a Person - Status Code is 404", function () {
pm.response.to.have.status(404);
});
- Response Format (Pass):
- Update a Person - Status Code is 404