This repository contains the backend API for NexRead, an e-library ("elib") project. It is a TypeScript and Express.js application that handles user authentication, book management, and file uploads.
- User Authentication: Secure user registration and login using JWT (JSON Web Tokens) and bcrypt for password hashing.
- Book Management: Full CRUD (Create, Read, Update, Delete) functionality for books.
- File Uploads: Handles uploading of book cover images and PDF files using Multer for temporary storage and Cloudinary for permanent cloud storage.
- Configuration Management: Uses
dotenvto manage environment variables for different environments. - Error Handling: A global error handler is implemented to provide consistent error responses.
- Database: Uses Mongoose to model and interact with a MongoDB database.
- Language: TypeScript
- Framework: Express.js
- Database: MongoDB with Mongoose
- Authentication:
bcrypt,jsonwebtoken - File Storage: Cloudinary
- File Uploads:
multer - Package Manager:
pnpm - Dev Tools:
nodemon,tsx,eslint,tsc-alias
- Node.js (v18 or higher recommended)
pnpmpackage manager (version 10.20.0 specified)- A MongoDB database (local or cloud-hosted)
- A Cloudinary account for API credentials
-
Clone the repository:
git clone https://github.com/luckygoswami/nexread-api cd nexread -
Install dependencies using
pnpm:pnpm install
-
Create a
.envfile in the root directory by copying the example:cp .env.example .env
-
Update the
.envfile with your credentials:PORT=5513 # Or any port you prefer MONGODB_CONNECTION_STRING='your-mongodb-connection-string' NODE_ENV='development' JWT_SECRET='your-strong-jwt-secret' CLOUD_NAME='your-cloudinary-cloud_name' CLOUD_API_KEY='your-cloudinary-api_key' CLOUD_API_SECRET='your-cloudinary-api_secret'
-
Development Mode: This command uses
nodemonandtsxto run the server with hot-reloading.pnpm dev
The server will start on the port specified in your
.envfile (e.g.,http://localhost:5513). -
Production Mode:
-
Build the TypeScript project:
pnpm build
This compiles the TypeScript code to JavaScript in the
dist/directory and resolves path aliases. -
Start the compiled server:
pnpm start
This runs the compiled
dist/server.jsfile using Node.
-
All endpoints are prefixed with /api.
POST /register: Creates a new user and returns a JWT.- Body:
{ "name": "...", "email": "...", "password": "..." }
- Body:
POST /login: Authenticates a user and returns a JWT.- Body:
{ "email": "...", "password": "..." }
- Body:
POST /: Creates a new book.- Auth: Required (Bearer Token).
- Body:
multipart/form-datawith fields fortitle,description,genre,coverImage(file), andfile(PDF file).
GET /: Lists all books.- Auth: Optional (Bearer Token). If provided, returns only books uploaded by the authenticated user. Without auth, returns all books.
- Query Parameters:
title(optional) - filters books by title using case-insensitive regex.
GET /:bookId: Gets a single book by its ID.- Auth: Not required.
PATCH /:bookId: Updates a book's details.- Auth: Required (Bearer Token). Only the author of the book can update it.
- Body:
multipart/form-datawith optional fields fortitle,description,genre,coverImage, andfile.
DELETE /:bookId: Deletes a book.- Auth: Required (Bearer Token). Only the author of the book can delete it.
.
├── server.ts # Main application entry point
├── src
│ ├── app.ts # Express app configuration (middleware, routes)
│ ├── config
│ │ ├── config.ts # Environment variable loading (dotenv)
│ │ ├── db.ts # MongoDB connection logic
│ │ └── cloudinary.ts # Cloudinary SDK configuration
│ ├── features
│ │ ├── book # Book-related module
│ │ │ ├── bookController.ts # Business logic for book CRUD
│ │ │ ├── bookModel.ts # Mongoose schema for Books
│ │ │ ├── bookRoute.ts # Routes for /api/books
│ │ │ └── bookTypes.ts # TypeScript interfaces for Books
│ │ └── user # User-related module
│ │ ├── userController.ts # Business logic for auth
│ │ ├── userModel.ts # Mongoose schema for Users
│ │ ├── userRoute.ts # Routes for /api/users
│ │ └── userTypes.ts # TypeScript interfaces for Users
│ └── middlewares
│ ├── authenticate.ts # JWT authentication middleware
│ ├── globalErrorHandler.ts # Global error handling
│ └── multer.ts # Multer configuration for file uploads
├── .env.example # Example environment variables
├── package.json # Project dependencies and scripts
├── pnpm-lock.yaml # PNPM lock file
└── tsconfig.json # TypeScript compiler options
Contributions are welcome! Please feel free to open issues or submit pull requests to enhance the functionality of Credinox.
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature). - Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/YourFeature). - Open a Pull Request.
This project is licensed under the MIT License. See the LICENSE file for more information.
If you have any questions or suggestions, feel free to open an issue on GitHub or contact me directly via GitHub Issues.