The Person API is a simple yet powerful RESTful web service that allows you to manage information about individuals, or "persons." This API provides endpoints for creating, retrieving, updating, and deleting person records. It is designed to be user-friendly and can be integrated into various applications that require basic person management functionality.
The Person API offers the following features:
-
Create a Person:
- Create a new person record with a name as a string only.
-
Retrieve Persons:
- Retrieve the record of the person stored in the database using their unique ID.
-
Update Person Information:
- Update a person's record by providing their unique ID.
-
Delete Person:
- Delete a person's record by specifying their unique ID.
Before you start using the Person API, ensure you have the following:
- Node.js and npm installed on your system.
- MongoDB installed and running (if you are using MongoDB as your database).
- Basic knowledge of RESTful API concepts.
To begin using the Person API, follow these steps:
- Clone the GitHub repository:
git clone https://github.com/Robotron2/hngtasktwo.git
- cd into
hngtasktwo
folder if you used a command line to clone. - Install project dependencies using
npm install
. - Configure your MongoDB connection settings in the
db.js
file inside theconfig
folder. - DB configuration
import mongoose from "mongoose"
const connectDB = async () => {
try {
const connect = await mongoose.connect(process.env.MONGO_URL) //If you have created a cluster on mongoDB Atlas
console.log(`Connected to Atlas`)
//If you want to use the local database
// const connect = await mongoose.connect("mongodb://localhost:27017/`{dbName}`")
// console.log(`Connected to MongoLocal`)
} catch (error) {
console.log(error)
}
}
export default connectDB
- Start the API server using
node app.js
.
- HTTP Method: POST
- Endpoint:
https://theophilus-hng-task-two.onrender.com/api
- Description: Create a new person with a name property.
- Request Body:
{ "name": "Mark" }
- Response:
- Status Code: 200
- Body:
{ "name": "John" "_id": "uniqueId123" }
- HTTP Method: GET
- Endpoint:
https://theophilus-hng-task-two.onrender.com/api/:userId
- Description: Retrieve the record of person stored in the database.
- Response:
- Status Code: 200
- Body:
{ "name": "Mark", "_id": "uniqueId123" },
- HTTP Method: PUT
- Endpoint:
https://theophilus-hng-task-two.onrender.com/api/:userId
- Description: Update a person's name by providing their unique ID.
- Request Body:
{ "newName": "Mark Essien" }
- Response:
- Status Code: 200
- Body:
{ "name": "Mark Essien" "_id": "uniqueId123" }
- HTTP Method: DELETE
- Endpoint:
https://theophilus-hng-task-two.onrender.com/api/:userId
- Description: Delete a person's record by specifying their unique ID.
- Response:
- Status Code: 200
- Body:
{ "success": true, "message": "Person deleted successfully!" }
For more documentation, visit https://documenter.getpostman.com/view/23278903/2s9YC2ztsR
To ensure the proper functioning of the API, automated tests have been implemented. You can run these tests using the following command:
npm test
- This API uses mongoDB atlas for demonstration purposes.
- Input validation is handled by simple IF statements. It is important to use more robust validation and error handling in a production application.
- No userAuth of any kind. Ensure secure access to your API in a real-world scenario.
- This documentation assumes that the developer is well acquainted with MongoDB/Mongoose, NodeJs and Express
The Person API simplifies the process of managing person records within your application. Whether you're building a personal information management system, a contact list, or any other application that requires person-related data, the Person API can help you get started quickly.