Skip to content

Latest commit

 

History

History
205 lines (157 loc) · 4.01 KB

File metadata and controls

205 lines (157 loc) · 4.01 KB

Book Review Application

A comprehensive Node.js/Express application for managing book reviews with JWT and Session authentication.

Features

  • Book Management: View books by ISBN, Author, Title
  • User Authentication: Register, Login with JWT and Session support
  • Book Reviews: Add, Modify, Delete reviews (authenticated users only)
  • RESTful API: Clean and organized API endpoints

Installation

  1. Clone the repository:
git clone <your-repo-url>
cd book-review-app
  1. Install dependencies:
npm install
  1. Create a .env file in the root directory:
JWT_SECRET=your_jwt_secret_key
SESSION_SECRET=your_session_secret_key
PORT=5000
  1. Start the server:
npm start

For development with auto-reload:

npm run dev

API Endpoints

Authentication Endpoints

Register User

POST /api/v1/register
Content-Type: application/json

{
  "username": "newuser",
  "password": "password123",
  "email": "user@example.com"
}

Login

POST /api/v1/login
Content-Type: application/json

{
  "username": "newuser",
  "password": "password123"
}

Returns: JWT token and user information

Logout

POST /api/v1/logout

Book Endpoints (Public - No Authentication Required)

Get All Books

GET /api/v1/books

Get Book by ISBN

GET /api/v1/books/isbn/:isbn

Example:

GET /api/v1/books/isbn/978-0-13-110362-7

Get Books by Author

GET /api/v1/books/author/:author

Example:

GET /api/v1/books/author/Robert%20C.%20Martin

Get Books by Title

GET /api/v1/books/title/:title

Example:

GET /api/v1/books/title/Clean%20Code

Get Reviews for a Book

GET /api/v1/reviews/isbn/:isbn

Example:

GET /api/v1/reviews/isbn/978-0-13-110362-7

Review Endpoints (Authenticated - Requires Session)

Add or Modify Review

POST /api/v1/reviews/isbn/:isbn
Content-Type: application/json
Cookie: connect.sid=<session_cookie>

{
  "review": "Great book! Very informative."
}

Delete Review

DELETE /api/v1/reviews/isbn/:isbn
Cookie: connect.sid=<session_cookie>

Sample Books

The application comes with 5 pre-loaded books:

  1. Clean Code by Robert C. Martin (ISBN: 978-0-13-110362-7)
  2. Design Patterns by Gang of Four (ISBN: 978-0-201-63361-0)
  3. Code Complete by Steve McConnell (ISBN: 978-0-201-71088-8)
  4. You Don't Know JS Yet by Kyle Simpson (ISBN: 978-1-617-29476-5)
  5. JavaScript: The Good Parts by Douglas Crockford (ISBN: 978-0-596-00712-6)

Testing with Postman

  1. Start the server: npm start
  2. Open Postman
  3. Test endpoints following the examples above
  4. For authenticated endpoints:
    • First, call POST /api/v1/login
    • Postman will automatically save session cookies
    • Use the saved session to make authenticated requests

Authentication Methods

The application supports two authentication methods:

  1. JWT (JSON Web Tokens)

    • Include token in Authorization header: Authorization: <token>
    • Token expires in 1 hour
  2. Session-based

    • Session cookie is automatically stored
    • Session expires in 60 minutes
    • Perfect for web applications

Project Structure

book-review-app/
├── server.js          # Main Express application
├── package.json       # Project dependencies
├── .env              # Environment variables
├── .gitignore        # Git ignore rules
└── README.md         # Documentation

Technologies Used

  • Express.js - Web server framework
  • jsonwebtoken - JWT implementation
  • bcryptjs - Password hashing
  • express-session - Session management
  • axios - HTTP client (for external API calls)
  • body-parser - Request body parsing
  • dotenv - Environment variable management

Notes

  • This is a demonstration/educational project
  • User data and reviews are stored in memory (not persistent)
  • For production, implement a proper database (MongoDB, PostgreSQL, etc.)
  • Change the JWT_SECRET and SESSION_SECRET in production

License

ISC