A simple CRUD API for managing books built with Node.js, Express, MySQL, and Swagger for API documentation.
- CRUD operations: Create, Read, Update, Delete books
- MySQL: Uses MySQL as the database to store book data
- Swagger: API documentation with Swagger UI
- Environment Configuration: Sensitive data is stored securely using environment variables
- Backend: Node.js, Express
- Database: MySQL
- Documentation: Swagger
├── .env # Environment variables
├── db.js # MySQL connection setup
├── index.js # Entry point of the application
├── routes
│ └── bookRoutes.js # API routes for book management
├── controllers
│ └── bookController.js # Book controllers for handling requests
├── models
│ └── book.js # Book model with MySQL queries
└── swagger.js # Swagger configuration file
- Node.js and npm
- MySQL Server
- MySQL Workbench (optional, for database management)
-
Clone the repository:
git clone https://github.com/sahejadkadivar/Book-Management-API.git cd book-management-api
-
Install dependencies:
npm install
-
Create a
.env
file with your database credentials::DB_HOST=localhost DB_USER=yourUsername DB_PASSWORD=yourPassword DB_NAME=yourDatabase DB_PORT=3306
-
Set up the database:
- Use MySQL Workbench or any MySQL client to create a database with the name provided in your
.env
file (DB_NAME
). - Create a books table:
CREATE TABLE books ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, author VARCHAR(255) NOT NULL, published_year INT );
- Use MySQL Workbench or any MySQL client to create a database with the name provided in your
- Start the server:
node index.js
-
Open
http://localhost:3000
to verify the server is running. -
Access the Swagger API documentation at:
http://localhost:3000/api-docs
- GET
/books
- Retrieve all books - GET
/books/:id
- Retrieve a single book by ID - POST
/books
- Create a new book - PUT
/books/:id
- Update a book by ID - DELETE
/books/:id
- Delete a book by ID
Swagger UI documentation is available at http://localhost:3000/api-docs
.
Add a Book
POST /books
Content-Type: application/json
{
"title": "1984",
"author": "George Orwell",
"published_year": 1949
}
Retrieve All Books
GET /books
Feel free to contribute to this project by creating issues or submitting pull requests. Your contributions are welcome!
I hope you like the project. Happy Coding! :)