This is a basic REST API skeleton written on Javascript.
Great for building a starter web API for your front-end
(Vue, React, Angular, or anything that can consume an API).
The BookStoreAPI project aims to offer a foundation for building a fully-fledged virtual bookstore application. By following this API, developers can easily set up book-related operations, manage orders, generate bills, and secure routes using authentication. This project showcases best practices for API design, data modeling, and user authentication in a practical context.
- Book Management:
- Create, retrieve, update, and delete book records.
- View details of individual books.
- User Authentication:
- User registration with unique authentication tokens.
- Secure login and token-based authentication for protected routes.
- Role-Based Access Control (RBAC) is help control what different users can do within the system.
- Order Processing:
- Create and manage customer orders.
- View order history for users.
- Order Bill Generation:
- Generate detailed order bills with transaction information.
Before setting up a Node.js application with Docker and MongoDB, you need to ensure that you have the necessary prerequisites in place. Below are the prerequisites for setting up this environment:
-
Node.js and npm:
- Node.js: You should have Node.js installed on your system. You can download it from the official Node.js website: https://nodejs.org/
- npm (Node Package Manager): npm is usually included with Node.js. You can verify its installation by running
npm -v
in your terminal.
-
Docker:
- Docker Desktop (for Windows and macOS): If you are using Windows or macOS, install Docker Desktop, which includes both Docker Engine and Docker Compose. You can download it from the Docker website: https://www.docker.com/products/docker-desktop
- Docker Engine and Docker Compose (for Linux): On Linux, you can install Docker Engine and Docker Compose separately. Follow the instructions for your specific Linux distribution on the Docker website: https://docs.docker.com/get-docker/
-
MongoDB Container Image:
- You'll need access to the official MongoDB Docker image from Docker Hub. You can pull it using the following Docker command:
docker pull mongo
- You'll need access to the official MongoDB Docker image from Docker Hub. You can pull it using the following Docker command:
-
Text Editor or IDE:
- You should have a code editor or an integrated development environment (IDE) installed for writing Node.js applications. Popular choices include Visual Studio Code, Sublime Text, and WebStorm.
The installation steps you provided seem to be for setting up a Node.js-based BookStore API with MongoDB as the database and Docker for containerization. Here's a breakdown of the steps:
-
Clone the repo:
Clone the project repository from GitHub using the
git clone
command:git clone https://github.com/Tam643/BookStoreAPI.git
-
Create a .env file:
Create a
.env
file in the project's root directory and set the following environment variables inside it:DB_URI=change_to_your_MongoDB_URI JWT_SECRET=change_to_your_secret
You can manually create the
.env
file and add these variables, or you can create it using the Command Prompt:cd BookStoreAPI echo DB_URI=change_to_your_MongoDB_URI JWT_SECRET=change_to_your_secret > .env
Replace
change_to_your_MongoDB_URI
with your actual MongoDB connection URI, andchange_to_your_secret
with your desired JWT secret key. -
Install NPM packages:
Install the required Node.js packages using npm:
npm install
-
Start MongoDB:
Make sure Docker Desktop is running, and then start a MongoDB container with the following command:
docker run -d -p 0.0.0.0:27017:27017 --name mongodb mongo
This command starts a MongoDB container and binds it to IP address
0.0.0.0
on port27017
. -
Start the application:
Finally, start the BookStore API application:
npm start
Now, the BookStore API is up and running locally on your machine. Before all should add a seed with the command
npm run seed
After the test you can clear up a database:
npm run clean
The BookStore API provides the following endpoints:
-
List All Books:
GET /books
Retrieve a list of all books in the bookstore.
-
Get a Specific Book:
GET /books/{id}
Retrieve a specific book by its ID.
-
Create a New Book:
POST /books
Create a new book in the bookstore. Requires authentication.
-
Update a Book:
PUT /books/{id}
Update an existing book by its ID. Requires authentication.
-
Delete a Book:
DELETE /books/{id}
Delete a book by its ID. Requires authentication.
For more detailed information and examples, please refer to the API Documentation.
The API uses JSON Web Tokens (JWT) for authentication. To access protected routes, include your JWT token in the Authorization
header of your requests:
Authorization: Bearer your-access-token
Here are some examples of how to interact with the API:
-
List all books:
curl http://localhost:3000/books
-
Get a specific book:
curl http://localhost:3000/books/1
-
twitter: @TumTheeradach
-
E-mail: p.tam.theeradach@gmail.com
Project Link: https://github.com/Tam643/BookStoreAPI
wait a minute ....