A RESTful API for managing books in a bookstore. This project provides CRUD operations for books and includes authentication and role-based access control.
- User Authentication: Register and login with JWT-based authentication.
- Role-Based Access Control:
- Admin: Can create, update, and delete books.
- User: Can view the list of books.
- CRUD Operations for Books:
- Add new books (Admin only).
- Update book details (Admin only).
- Delete books (Admin only).
- Retrieve all books (All users).
Follow these steps to set up and run the project locally.
-
Clone the repository:
git clone https://github.com/bienherasme/Bookstore-API.git cd bookstore-api -
Install dependencies:
npm install
-
Create a
.envfile in the project root and configure the following variables:MONGO_URI=your_mongodb_connection_string JWT_SECRET=your_jwt_secret PORT=5000
-
Start the server:
npx nodemon src/app.js
-
Use Postman or any REST client to interact with the API.
| Method | Endpoint | Description | Access |
|---|---|---|---|
| POST | /api/auth/register |
Register a new user | Public |
| POST | /api/auth/login |
Login and get a token | Public |
| Method | Endpoint | Description | Access |
|---|---|---|---|
| GET | /api/books |
Get all books | Authenticated |
| POST | /api/books |
Add a new book | Admin Only |
| PUT | /api/books/:id |
Update a book by ID | Admin Only |
| DELETE | /api/books/:id |
Delete a book by ID | Admin Only |
bookstore-api/
├── src/
│ ├── controllers/ # Controllers for business logic
│ ├── models/ # Mongoose models for MongoDB
│ ├── routes/ # Route definitions
│ ├── middlewares/ # Custom middleware
│ ├── config/ # Database connection
│ ├── app.js # Application entry point
├── package.json # Project metadata and dependencies
├── .env # Environment variables
└── README.md # Project documentation
- Token: Users must send a JWT in the
Authorizationheader:Authorization: Bearer <token> - Roles:
- Admin: Full access (create, update, delete books).
- User: Limited access (view books only).
- Node.js - Backend runtime
- Express - Web framework
- MongoDB - NoSQL database
- Mongoose - ODM library for MongoDB
- JWT - JSON Web Tokens for authentication
This project is licensed under the MIT License. See the LICENSE file for details.