This repository contains the backend code for the BookBuddy application, a simple RESTful API for managing books using Node.js, Express, and MongoDB.
- Create a new book
- Retrieve all books
- Retrieve a book by ID
- Update a book by ID
- Delete a book by ID
- Node.js
- Express.js
- MongoDB (using Mongoose)
- JSON for data exchange
- RESTful API design
Before you begin, ensure you have met the following requirements:
- You have installed Node.js and npm.
- You have MongoDB set up. For this project, a MongoDB Atlas cluster is used.
- You have a basic understanding of Node.js and Express.js.
Follow these instructions to set up and run the project locally.
-
Clone the repository:
git clone https://github.com/your-username/bookbuddy-backend.git cd bookbuddy-backend
-
Install the dependencies:
npm install
-
Create a
.env
file:Create a
.env
file in the root directory to store your MongoDB URI (optional but recommended for security reasons).MONGODB_URI=mongodb+srv://admin:[email protected]/BookBuddy?retryWrites=true&w=majority&appName=bookbuddybackenddb
-
Start the server:
npm start
The server should be running on http://localhost:3000.
http://localhost:3000/api/books
- GET
/api/books/
- Retrieves all books. - GET
/api/books/:id
- Retrieves a specific book by ID. - POST
/api/books/
- Creates a new book. - PUT
/api/books/:id
- Updates a book by ID. - DELETE
/api/books/:id
- Deletes a book by ID.
To create a new book, you can send a POST request to /api/books/
with a JSON payload:
{
"name": "Book Title",
"Auther": "Author Name"
}
- index.js: Entry point of the application where the Express server is initialized and connected to the MongoDB database.
- models/book.model.js: Mongoose schema for the Book model.
- routes/book.route.js: Express router for defining the book-related API routes.
- controllers/book.controller.js: Controller functions to handle the logic for each API endpoint.
The API provides proper error handling with appropriate HTTP status codes:
200 OK
for successful requests.404 Not Found
for resources that are not found.500 Internal Server Error
for server-related issues.
- Implement user authentication and authorization.
- Add pagination and sorting to the list of books.
- Implement data validation and sanitization.