This is a small Library app built with Node.js (Express) and TypeScript. The app provides APIs for user registration and login, as well as book management with role-based authorization. It also includes additional features such as filtering books based on creation time and logging API requests.
https://rose-adder-yoke.cyclic.app/
- User registration and login
- Role-based authorization
- JWT authentication
- Book creation and retrieval
- Filtering books based on creation time
- Logging API requests
- Clone the repository:
https://github.com/Dheeraj-pal/Library_App_NodeJS.git
- Install the dependencies:
cd library-app
npm install
- Set up environment variables:
Create a .env
file in the root directory of the project and add the following variables:
PORT=9000
MONGODB_URI=<your-mongodb-uri>
JWT_SECRET=<your-secret-key>
- Start the app:
npm run server
The app should now be running on http://localhost:9000
- Endpoint:
POST /users/signup
- Description: Register a new user.
- Request body:
- name (string, required): User's name
- email (string, required): User's email address.
- password (string, required): User's password.
- roles (array, optional, default: "VIEWER" ): User's roles. Example:
POST /users/signup
Content-Type: application/json
{
"name": "User 1"
"email": "user1@example.com",
"password": "password123",
"roles": ["CREATOR"]
}
- Endpoint:
POST /users/login
- Description: Log in a user and obtain an access token.
- Request body:
- email (string, required): User's email address.
- password (string, required): User's password. Example:
POST /users/login
Content-Type: application/json
{
"email": "user@example.com",
"password": "password123"
}
- Endpoint:
POST /books
- Description: Create a new book (requires "CREATOR" role).
- Request body:
- title (string, required): Book title.
- author (string, required): Book author.
- creatorID (objectId, required): Book author's UserID
- createdAt (Date): automatically added when new book is created Example:
POST /books
Content-Type: application/json
authorization: bearer <access-token>
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"description": "A classic novel depicting the Jazz Age in America."
}
- Endpoint: GET /books
- Description: Get books based on the user's role.
- Query parameters:
- old (number, optional): Include books created 10 minutes ago and earlier.
- new (number, optional): Include books created within the last 10 minutes.
Example:
GET /books?old=1
Authorization: Bearer <access-token>
GET /books?new=1
Authorization: Bearer <access-token>
Can view all the books present in the database Example:
GET /books?
Authorization: Bearer <access-token>
The app logs API requests using Winston. The log files can be found in the logs directory. Each log entry contains the following information:
- level: Log level (e.g., info, error).
- message: Log message containing IP, method, path, status, and timestamp.
- timestamp: Timestamp of the log entry. Example log entry:
{"level":"info","message":"IP - ::ffff:127.0.0.1, METHOD - GET, PATH - /books, STATUS - 200","timestamp":"2023-07-07T09:15:43.510Z"}