This project was built as a means to learn ExpressJS in conjunction with Mongo Atlas (10 March 2023).
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:
To run the Library System server,
- Install the following packages from Node.JS (Express, CORS, DotEnv and MondoDB)
npm install express cors dotenv mongodb
- Install Node Monitor
npm install -g nodemon
- 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.
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.
- 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.
- 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. - 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).
The following API endpoints are available to perform the CRUD operations.
GET /books
: Retrieve information of all booksPOST /books
: Create a new bookPUT /books/{bookid}
: Update details of an existing bookDELETE /books/{bookid}
: Delete an existing book
POST /user
: Create a new userPOST /login
: Generate JSONWebToken via user login (expires in 3 days)POST /profile
: Access protected route via valid JSONWebToken
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!
- Retrieve information of all books
GET http://localhost:3001/books
Successful retrival of book information in ARC:
Book information stored in MongoDB:
- Create a new book
POST http://localhost:3001/books
Successful creation of new book in ARC:
New book information stored in MongoDB:
Validation of languages and authors entered:
Validation of publisher ID entered:
Validation of other information entered:
- Update details of an existing book
PUT http://localhost:3001/books/{bookid}
Successful update of existing book information in ARC:
Existing book information (before updating languages) stored in MongoDB:
Existing book information (after updating languages) stored in MongoDB:
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:
Existing book information (before deletion) stored in MongoDB:
Existing book information (after deletion) stored in MongoDB:
- Create a new user
POST http://localhost:3001/user
Successful creation of new user in ARC:
New user information stored in MongoDB:
- Generate JSONWebToken via user login (expires in 3 days)
POST http://localhost:3001/login
Successful user login and generation of JSONWebToken in ARC:
- Access protected route via valid JSONWebToken
POST http://localhost:3001/profile
Valid JSONWebToken used to access protected route in ARC:
Invalid JSONWebToken used to access protected route in ARC:
- JavaScript
- NPM Packages (Express, CORS, DotEnv, bcrypt, JSONWebToken)
- Mongo Atlas Database
This readme file was written with reference to the following repository: https://github.com/nadialefebvre/library