A backend RESTful API for managing books, users, and ratings.
Built with Node.js, Express.js, MySQL, and secured using JWT authentication, bcrypt, and role-based access control.
Includes features like CRUD operations, file uploads, ratings system, search, filtering, and pagination.
- Features
- Tech Stack
- Project Structure
- Getting Started
- API Overview
- Screenshots / Demo(Coming Soon)
- Detailed API Reference
- Security Notes
- Deployment
- Author
-
Authentication & Authorization
- Secure login/signup using JWT and bcrypt password hashing
- Role-based access control for users and admins
-
Books Management
- CRUD operations for books (create, update, delete, fetch)
- Advanced search, filtering, and pagination
-
Ratings System
- Users can add ratings & reviews for books
-
File Uploads
- Upload book cover images with Multer
-
Architecture
- Built with MVC pattern
- Middleware validation and centralized error handling
- Backend: Node.js, Express.js
- Database: MySQL
- Authentication & Security: JWT, bcrypt, Helmet, CORS
- File Uploads: Multer
- Architecture: MVC
Bookstore-api-main/
βββ server.js # Entry point
βββ config/
β βββ db.js # Database connection
βββ controllers/ # Controllers (Auth, Books, Ratings)
βββ middleware/ # Auth & Upload middleware
βββ routes/ # API Routes
βββ experimental_features/ # AI & Payment (in progress)
git clone https://github.com/your-username/bookstore-api.git
cd bookstore-api
npm install
Create a .env
file in the root directory:
PORT=5000
DB_HOST=localhost
DB_USER=your-mysql-user
DB_PASSWORD=your-mysql-password
DB_NAME=bookstore
JWT_SECRET=your-secret-key
npm start
POST /api/auth/register
β Register a new user (role forced to "user")POST /api/auth/login
β returns JWT +token_type
+expires_in
+ user
GET /api/book/get
β Get books (requires authentication, with validation on title & author)GET /api/book
β Get paginated & filtered book list (public)GET /api/book/:id
β Get a book by its ID (public)POST /api/book/add
β Add a new book (Admin only)POST /api/book/upload
β Upload book cover image (authenticated users only)
All users who register through /api/auth/register
are assigned the role "user"
by default.
POST /api/rating/:bookId
β Rate a book (only logged-in users)GET /api/rating/book/:bookId
β Get all ratings for a specific book
- AI Controller β early integration with AI for book-related features
- Payment Controller β payment API integration (work in progress)
Validations (express-validator)
name
β required (cannot be empty)email
β required, must be a valid email formatpassword
β required, minimum 8 characters and must include at least 1 number
Request Body
{
"name": "Tirth Patel",
"email": "[email protected]",
"password": "123456",
}
Response (201 Created)
{
"message": "User registered successfully",
"user": {
"id": 12,
"name": "Tirth Patel",
"email": "[email protected]",
"role": "user" }
}
Validations (express-validator)
email
β required, must be a valid email formatpassword
β required (cannot be empty)
Request Body
{
"email": "[email protected]",
"password": "password123"
}
Response (200 OK)
{
"message": "Login successful",
"token_type": "Bearer",
"expires_in": 2592000,
"token": "your-jwt-token",
"user": {
"id": 12,
"name": "Tirth Patel",
"role": "user"
}
}
Headers: Authorization: Bearer
{
"title": "Clean Code",
"author": "Robert C. Martin",
"price": 499,
"category": "Programming"
}
Response (200 OK)
{
"success": true,
"data":{
"id": 101,
"title": "Clean Code",
"author": "Robert C. Martin",
"price": 499,
"category": "Programming"
}
}
Response (200 OK)
{
"success": true,
"page": 1,
"limit": 10,
"total": 42,
"data": [ {
"id": 101,
"title": "Clean Code",
"author": "Robert C. Martin"
} ]
}
Headers: Authorization: Bearer <JWT>
{ "rating": 5, "review": "Excellent!" }
Response (200 OK)
{
"success": true,
"message": "Rating added",
"data": {
"bookId": 101,
"rating": 5,
"review": "Excellent!"
}
}
- Public register always creates
"user"
; clientrole
is ignored. - Admin-only endpoints guarded by
checkRole("admin")
. /login
&/register
have per-route rate limits; optional global limiter inserver.js.
- Helmet, CORS, morgan recommended.
- Do not commit
.env.
Rotate any leaked secrets.
- Can be deployed on Render, Railway, or Vercel
- MySQL database can be hosted on Railway / PlanetScale / AWS RDS
Tirth Patel