Skip to content

Library Management System using ExpressJS and MongoDB

Notifications You must be signed in to change notification settings

f-lsq/library-system-db

Repository files navigation

Library System Database

This project was built as a means to learn ExpressJS in conjunction with Mongo Atlas (10 March 2023).

Overview

This simple Javascript server acts as a basic library managemement system, allowing users to manage a collection of books. Users will be able to perform CRUD (Create, Delete, Update, Delete) operations on the books through a set of RESTFUL API endpoints. This application was built using the ExpressJS framework and utilises a Mongo Database for database interactions.

Here is a simple entity relationship diagram of the library system: alt text

Getting Started

To run the Library System server,

  1. Install the following packages from Node.JS (Express, CORS, DotEnv and MondoDB)
npm install express cors dotenv mongodb
  1. Install Node Monitor
npm install -g nodemon
  1. Start the server using Node Monitor
nodemon

The server will start and the API endpoints may be reached using tools such as Advanced REST Client (ARC), Postman or Insomnia.

Configuration and Environment Variables

Before running the server, ensure that the following are setup in a .env file. Please refer to the .env.sample file for the variables required.

  1. Mongo URI: This is your connection string from Mongo Atlas. To retrieved your connection string, do the following: Log in to Mongo Atlas > Database > Connect > Drivers (Node.JS) > Copy connection string from section 3.
  2. MongoDB Name: This is the name of the mongo database that will store the book information. Create a new database in Mongo Atlas or select a new database name by keying it in a .env file. An example of a database name is library-books.
  3. Token Secret: This is the private key used to generate a hash for users' password. You could generate a random key via https://randomkeygen.com/ (use the 504-bit WPA key for better security).

API Endpoints

The following API endpoints are available to perform the CRUD operations.

Books

  • GET /books : Retrieve information of all books
  • POST /books : Create a new book
  • PUT /books/{bookid} : Update details of an existing book
  • DELETE /books/{bookid} : Delete an existing book

Users

  • POST /user : Create a new user
  • POST /login : Generate JSONWebToken via user login (expires in 3 days)
  • POST /profile : Access protected route via valid JSONWebToken

Example Usage

Here are some examples of how you can interact with the API and how it will be logged in MongoDB.

Note: The endpoint for books in the screenshots differs from the actual endpoint. Please use the /books endpoint!

Books

  • Retrieve information of all books
GET http://localhost:3001/books

Successful retrival of book information in ARC: alt text

Book information stored in MongoDB: alt text

Search by author: alt text

Search by language: alt text

Search by publisher: alt text

Search by title: alt text

  • Create a new book
POST http://localhost:3001/books

Successful creation of new book in ARC: alt text

New book information stored in MongoDB: alt text

Validation of ISBN entered: alt text

Validation of languages and authors entered: alt text

Validation of publisher ID entered: alt text

Validation of other information entered: alt text

  • Update details of an existing book
PUT http://localhost:3001/books/{bookid}

Successful update of existing book information in ARC: alt text

Existing book information (before updating languages) stored in MongoDB: alt text

Existing book information (after updating languages) stored in MongoDB: alt text

Note: Validation performed when updating a book information is the same as that when a new book is created

  • Delete an existing book
DELETE http://localhost:3001/books/{bookid}

Successful deletion of existing book information in ARC: alt text

Existing book information (before deletion) stored in MongoDB: alt text

Existing book information (after deletion) stored in MongoDB: alt text

Users

  • Create a new user
POST http://localhost:3001/user

Successful creation of new user in ARC: alt text

New user information stored in MongoDB: alt text

  • Generate JSONWebToken via user login (expires in 3 days)
POST http://localhost:3001/login

Successful user login and generation of JSONWebToken in ARC: alt text

  • Access protected route via valid JSONWebToken
POST http://localhost:3001/profile

Valid JSONWebToken used to access protected route in ARC: alt text

Invalid JSONWebToken used to access protected route in ARC: alt text

Technological Stacks Used

  • JavaScript
  • NPM Packages (Express, CORS, DotEnv, bcrypt, JSONWebToken)
  • Mongo Atlas Database

References

This readme file was written with reference to the following repository: https://github.com/nadialefebvre/library

About

Library Management System using ExpressJS and MongoDB

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published