Skip to content

Shreeja5714/elib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

eLib - Digital Library Management System

A full-stack REST API for managing digital books with user authentication, file uploads, and CRUD operations. Built with Node.js, Express, TypeScript, MongoDB, and Cloudinary.

πŸš€ Features

  • βœ… User Authentication - JWT-based registration and login
  • βœ… Book Management - Complete CRUD operations for books
  • βœ… File Upload - Cover images and PDF files via Cloudinary
  • βœ… Secure Access - Protected routes with authentication middleware
  • βœ… TypeScript - Full type safety throughout the application
  • βœ… MongoDB - NoSQL database with Mongoose ODM
  • βœ… Cloud Storage - Cloudinary integration for file management

πŸ“‹ Table of Contents

πŸ› οΈ Technologies Used

  • Backend Framework: Node.js, Express.js
  • Language: TypeScript
  • Database: MongoDB with Mongoose
  • Authentication: JWT (JSON Web Tokens)
  • File Upload: Multer
  • Cloud Storage: Cloudinary
  • Password Hashing: bcrypt
  • Validation: express-validator
  • Error Handling: http-errors

πŸ“¦ Prerequisites

Before running this project, make sure you have:

  • Node.js (v18 or higher)
  • MongoDB (local or Atlas)
  • Cloudinary account
  • npm or yarn package manager

βš™οΈ Installation

  1. Clone the repository
git clone https://github.com/Shreeja5714/elib.git
cd elib
  1. Install dependencies
npm install
  1. Create environment file
cp .env.example .env
  1. Configure environment variables (see below)

  2. Start MongoDB (if running locally)

mongod
  1. Run the development server
npm run dev

The server will start at http://localhost:5513

πŸ” Environment Variables

Create a .env file in the root directory:

# Server Configuration
PORT=5513
NODE_ENV=development

# Database
DATABASE_URL=mongodb://localhost:27017/elib

# JWT Secret
JWT_SECRET=your_super_secret_jwt_key_here

# Cloudinary Configuration
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret

Getting Cloudinary Credentials:

  1. Sign up at cloudinary.com
  2. Go to Dashboard
  3. Copy Cloud Name, API Key, and API Secret

πŸ“š API Endpoints

Authentication

Register User

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

{
  "name": "Shreeja Vyas",
  "email": "shreeja@example.com",
  "password": "password123"
}

Response:

{
  "message": "User registered successfully",
  "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": "507f1f77bcf86cd799439011",
    "name": "Shreeja Vyas",
    "email": "shreeja@example.com"
  }
}

Login User

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

{
  "email": "shreeja@example.com",
  "password": "password123"
}

Response:

{
  "message": "Login successful",
  "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "id": "507f1f77bcf86cd799439011",
    "name": "Shreeja Vyas",
    "email": "shreeja@example.com"
  }
}

Books (Protected Routes)

Note: All book routes require authentication. Include the JWT token in the Authorization header:

Authorization: Bearer <your_token_here>

Create Book

POST /api/books
Authorization: Bearer <token>
Content-Type: multipart/form-data

Fields:
- title: "Book Title"
- genre: "Fiction"
- coverImage: <image file>
- file: <pdf file>

Response:

{
  "message": "Book created successfully",
  "data": {
    "id": "507f1f77bcf86cd799439011",
    "title": "Book Title",
    "coverImage": "https://res.cloudinary.com/...",
    "file": "https://res.cloudinary.com/..."
  }
}

Get All Books

GET /api/books

Response:

{
  "message": "Books retrieved successfully",
  "data": [
    {
      "_id": "507f1f77bcf86cd799439011",
      "title": "Book Title",
      "genre": "Fiction",
      "author": {
        "_id": "507f1f77bcf86cd799439012",
        "name": "John Doe"
      },
      "coverImage": "https://res.cloudinary.com/...",
      "file": "https://res.cloudinary.com/...",
      "createdAt": "2025-01-20T10:30:00.000Z",
      "updatedAt": "2025-01-20T10:30:00.000Z"
    }
  ]
}

Get Single Book

GET /api/books/:bookId

Response:

{
  "message": "Book retrieved successfully",
  "data": {
    "_id": "507f1f77bcf86cd799439011",
    "title": "Book Title",
    "genre": "Fiction",
    "author": {
      "_id": "507f1f77bcf86cd799439012",
      "name": "John Doe",
      "email": "john@example.com"
    },
    "coverImage": "https://res.cloudinary.com/...",
    "file": "https://res.cloudinary.com/...",
    "createdAt": "2025-01-20T10:30:00.000Z"
  }
}

Update Book

PATCH /api/books/:bookId
Authorization: Bearer <token>
Content-Type: multipart/form-data

Fields (all optional):
- title: "Updated Title"
- genre: "Updated Genre"
- coverImage: <new image file>
- file: <new pdf file>

Response:

{
  "message": "Book updated successfully",
  "data": {
    "_id": "507f1f77bcf86cd799439011",
    "title": "Updated Title",
    "genre": "Updated Genre",
    "coverImage": "https://res.cloudinary.com/...",
    "file": "https://res.cloudinary.com/..."
  }
}

Delete Book

DELETE /api/books/:bookId
Authorization: Bearer <token>

Response:

{
  "message": "Book deleted successfully"
}

πŸ“ Project Structure

elib/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”œβ”€β”€ config.ts           # Environment configuration
β”‚   β”‚   β”œβ”€β”€ db.ts               # Database connection
β”‚   β”‚   └── cloudinary.ts       # Cloudinary setup
β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   β”œβ”€β”€ authenticate.ts     # JWT authentication middleware
β”‚   β”‚   β”œβ”€β”€ multer.ts           # File upload configuration
β”‚   β”‚   └── globalErrorHandler.ts
β”‚   β”œβ”€β”€ user/
β”‚   β”‚   β”œβ”€β”€ userModel.ts        # User schema
β”‚   β”‚   β”œβ”€β”€ userController.ts   # User controllers
β”‚   β”‚   └── userRouter.ts       # User routes
β”‚   β”œβ”€β”€ book/
β”‚   β”‚   β”œβ”€β”€ bookModel.ts        # Book schema
β”‚   β”‚   β”œβ”€β”€ bookController.ts   # Book controllers (CRUD)
β”‚   β”‚   └── bookRouter.ts       # Book routes
β”‚   β”œβ”€β”€ app.ts                  # Express app setup
β”‚   └── server.ts               # Server entry point
β”œβ”€β”€ public/
β”‚   └── data/
β”‚       └── uploads/            # Temporary file storage
β”œβ”€β”€ .env                        # Environment variables
β”œβ”€β”€ .env.example                # Environment template
β”œβ”€β”€ .gitignore
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json
└── README.md

πŸ’‘ Usage Examples

Using cURL

Register a user:

curl -X POST http://localhost:5513/api/users/register \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "email": "john@example.com",
    "password": "password123"
  }'

Create a book:

curl -X POST http://localhost:5513/api/books \
  -H "Authorization: Bearer YOUR_TOKEN_HERE" \
  -F "title=My Book" \
  -F "genre=Fiction" \
  -F "coverImage=@/path/to/cover.jpg" \
  -F "file=@/path/to/book.pdf"

Using Postman

  1. Register/Login to get your JWT token
  2. Set Authorization header: Bearer <your_token>
  3. For file uploads, use form-data body type
  4. Add fields: title, genre, coverImage (file), file (file)

πŸ”’ Security Features

  • βœ… Password hashing with bcrypt
  • βœ… JWT token authentication
  • βœ… Protected routes
  • βœ… User authorization checks
  • βœ… Input validation
  • βœ… Secure file handling
  • βœ… Environment variable protection

πŸ› Error Handling

The API uses standard HTTP status codes:

  • 200 - Success
  • 201 - Created
  • 204 - Deleted
  • 400 - Bad Request
  • 401 - Unauthorized
  • 403 - Forbidden
  • 404 - Not Found
  • 409 - Conflict
  • 500 - Internal Server Error

Error Response Format:

{
  "message": "Error description",
  "errorStack": "Stack trace (in development only)"
}

πŸ§ͺ Testing

Manual Testing

Use Postman, Thunder Client, or cURL to test endpoints.

Automated Testing (Future)

npm test

πŸ“ Scripts

# Development
npm run dev          # Start development server with nodemon

# Production
npm run build        # Compile TypeScript to JavaScript
npm start            # Start production server

# Type checking
npm run type-check   # Check TypeScript types

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Code Style

  • Use TypeScript
  • Follow existing code patterns
  • Add comments for complex logic
  • Update README if needed

πŸ‘€ Author

Shreeja Vyas

πŸ™ Acknowledgments

  • Express.js documentation
  • MongoDB documentation
  • Cloudinary API
  • Coder's Gyan Youtube Video

Made with ❀️ by Shreeja Vyas

About

A full-stack REST API for managing digital books with user authentication, file uploads, and CRUD operations. Built with Node.js, Express, TypeScript, MongoDB, and Cloudinary.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors