Creating an API with Node.js, Express, and MongoDB to manage a collection of books.
Technology used in this project are Node.js, Express, and MongoDB.
There are couple things that I came in mind when completing (& researching) for this project.
- Implementing more features to the API
- I frequent the library a lot and here's some useful features to include:
- Adding genres and sorting by genres.
- Having a count of each inventory.
- Refactoring with better MVC architecture
- Adhering to a structured architecture is important for creating scalable, reusable, and organized applications. I have already began to separate the code into its appropriate folders. With more time, I would better organize my code according to the embedded link.
- Complete the MERN stack
- Currently, this project is only a part of the MERN stack.
- We need to also implement a frontend, React, to complete this stack and make this project a full-stack application.
- Fix my unit test (book.test.js)
- This is my first time writing unit test with Jest. I can't seem to figure out how to stop "asynchronous operations" in my test. Therefore, the test fails, but nonetheless, I decided to put my attempt here.
This guide will walk you through the steps to run an API built with Node.js, Express, and MongoDB.
Before you begin, ensure you have the following installed on your system:
- Node.js: Download and install Node.js from nodejs.org.
- Clone the repository of your Node.js API project or download into your computer and save into your Desktop
git clone https://github.com/hanpm/Book-API.git
- Navigate to the project directory.
cd Desktop/Book-API - Install dependencies
npm install
- Get all books
localhost:3000/books
- Get books with pagination
localhost:3000/books?page=4&limit=10
- Get book by ID
localhost:3000/books/<insert ID>
- Post books
localhost:3000/books { "title": "Insert title", "author": "Insert author", "publicationYear": 0 } - Update books
localhost:3000/books/<insert ID> { "title": "Insert updated title", "author": "Insert updated author", "publicationYear": 0 }
- Search books
localhost:3000/books/search?q=maryshelley&page=4&limit=10
- Get book stats
localhost:3000/books/stats
- Install dependencies
npm install --save-dev jest
- Run test in terminal
npx jest