This project implements a RESTful API for managing book records. It utilizes Express.js and Sequelize, with SQLite as the database.
To get started, clone the repository:
git clone https://github.com/edabeyza/BookAPI.git
Install the necessary packages:
npm i express dotenv express-async-errors sequelize sqlite3
express: Web framework for Node.js, providing easy-to-use routing and middleware.
dotenv: Loads environment variables from a .env file into process.env.
express-async-errors: Simplifies error handling for async/await functions in Express.js.
sequelize: Promise-based Node.js ORM for SQL databases, offering data modeling and CRUD operations.
sqlite3: SQLite driver for Sequelize, allowing interaction with SQLite databases.
Ensure you have an .env file in the project directory with the following configurations:
PORT=8000
SQLITE=/.db.sqlite3
To create the database table, you need to execute the following command in the models/book.js file:
// Run sequelize.sync() to synchronize the model with the database
sequelize.sync();
This command synchronizes the defined Sequelize model (Book) with your database schema, ensuring the table structure matches your model definitions.
Start the server with the following command:
nodemon
The server will run on the port specified in the .env
file (default is 8000).
POST /api/book
{
"title": "Hobbit",
"author": "J.R.R. Tolkien",
"ISBN": "9789752733732",
"genre": "Fantasy Fiction",
"publicationYear": "1937",
"image": "www.example.com/Books/Hobbit.jpg"
}
GET /api/book
GET /api/book/:id
PUT /api/book/:id
{
"title": "Hobbit",
"author": "J.R.R. Tolkien",
"ISBN": "9789752733732",
"genre": "Fantasy Fiction",
"publicationYear": "1937",
"image": "www.example.com/Books/Hobbit.jpg"
}
DELETE /api/book/:id