BetterReads is a robust backend API for a book review platform, designed to function similarly to Goodreads. It provides a comprehensive set of features for managing users, books, and reviews, with a secure authentication system. Built with Node.js, Express, and Prisma, it's a scalable and efficient foundation for any book-centric application.
- Features
- Tech Stack
- Prerequisites
- Setup & Installation
- API Endpoints
- Authentication
- Contributing
- License
- 👤 User Authentication: Secure sign-up, log-in, and log-out functionality using JSON Web Tokens (JWT).
- 📚 Book Management: Operations to add, update, delete, and retrieve books from the database.
- ✍️ Review System: Users can add, update, and delete their reviews for any book.
- User Profile: Fetch a user's profile along with all the reviews they have written.
- 🔒 Protected Routes: Middleware to protect sensitive routes, ensuring only authenticated users can perform certain actions.
- Backend: Node.js, Express.js
- ORM: Prisma ORM
- Database: PostgreSQL
- Authentication: JSON Web Tokens (JWT)
- Password Hashing: bcrypt.js
- Validation: (Optional, e.g.,
express-validatororzod)
Before you begin, ensure you have the following installed on your local machine:
- Node.js (v16 or higher recommended)
- npm or yarn
- PostgreSQL
- Git
-
Clone the repository:
git clone [https://github.com/your-username/bookverse.git](https://github.com/your-username/bookverse.git)
-
Navigate into the directory:
cd bookverse -
Install dependencies:
npm install
-
Set up the environment variables: Create a
.envfile in the root of the project and add the following variables.# PostgreSQL connection URL from your database provider # Format: postgresql://USER:PASSWORD@HOST:PORT/DATABASE DATABASE_URL="postgresql://postgres:password@localhost:5432/bookverse" # Secret key for signing JWTs (use a long, random string) JWT_SECRET="YOUR_SUPER_SECRET_KEY" # Port for the server to run on PORT=3000
-
Set up the database: Push the Prisma schema to your PostgreSQL database. This will create the necessary tables.
npx prisma db push
-
Generate the Prisma Client: Generate the Prisma Client based on your schema.
npx prisma generate
-
Start the server:
npm start
The API should now be running at
http://localhost:3000.
All endpoints are prefixed with /api.
| Method | Endpoint | Description | Request Body |
|---|---|---|---|
POST |
/auth/signup |
Register a new user. | { "email": "user@example.com", "password": "password123", "name": "John Doe" } |
POST |
/auth/login |
Log in an existing user. | { "email": "user@example.com", "password": "password123" } |
Authentication required for POST methods.
| Method | Endpoint | Description |
|---|---|---|
GET |
/books |
Get a list of all books. |
GET |
/books/:id |
Get a single book by its ID. |
POST |
/books |
Add a new book. |
POST |
/books/:id |
Delete a book. |
Authentication required for all review endpoints.
| Method | Endpoint | Description | Request Body |
|---|---|---|---|
GET |
/books/:bookId/reviews |
Get all reviews for a specific book. | |
POST |
/books/:bookId/reviews |
Add a new review for a book. | { "rating": 5, "comment": "Amazing read!" } |
GET |
/reviews/:id |
Get a single review by its ID. | |
PUT |
/reviews/:id |
Update a user's own review. | { "rating": 4, "comment": "Still great on second read." } |
DELETE |
/reviews/:id |
Delete a user's own review. |
Authentication required.
| Method | Endpoint | Description |
|---|---|---|
GET |
/users/me |
Get the profile of the currently logged-in user. |
To access protected routes, you must include the JWT in the Authorization header of your request.
Format: Bearer <token>
Example:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
The token is provided in the response body upon successful user signup or login.
Contributions are welcome! If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/new-feature). - Commit your changes (
git commit -am 'Add some feature'). - Push to the branch (
git push origin feature/new-feature). - Create a new Pull Request.
This project is licensed under the MIT License - see the LICENSE.md file for details.