Certainly! Below is a structured README.md
template for the CRUD API you've developed using Node.js, Express, and MongoDB:
This API allows for the basic CRUD (Create, Read, Update, Delete) operations on a "Person" resource.
-
Clone the Repository
git clone [https://github.com/ochosteve08/HNG.git]
-
Install Dependencies
npm install
-
Setup Environment Variables
Rename
.env.example
to.env
and update the values:MONGO_URI=YOUR_MONGODB_CONNECTION_STRING PORT=3500
-
Run the Server
npm run dev
Once the server is up and running, you can access the API at:
-
Add a Person
- Method:
POST
- Route:
/api
- Request Body:
{"name":"glorious"}
- Response:
{ "name": "glorious", "_id": "650262b5420f620d558be6c4", "__v": 0 }
- Method:
-
Read a Person
- Method:
GET
- Route:
/api/:userId
- Response:
{ "_id": "65025d5e420f620d558be6c0", "name": "glorious", "__v": 0 }
- Route:
-
Update a Person
- Method:
PUT
- Route:
/api/:userId
- Request Body:
"name":"mungo park"}
- Response:
{ "name": "mungo park", "_id": "650262b5420f620d558be6c4", "__v": 0 }
- Method:
-
Delete a Person
- Method:
DELETE
- Route:
/api/:userId
- Response:
{ "message": "Deleted Successfully" }
- Method:
-
Error Response Format:
{ "error": "Detailed error message" }
-
Common Errors:
400 Bad Request
: Invalid input or malformed request.404 Not Found
: The requested resource (person) was not found.500 Internal Server Error
: An unexpected error on the server side.
For testing the API, you can use tools like Postman.
If you'd like to contribute, please fork the repository, make your changes, and then submit a pull request. Ensure that your code has adequate coverage and maintains the existing coding style.
MIT License. See the LICENSE file for details.