Creating CRUD-Book Store
Building a Simple Book Store Backend with Express.js and Postman
Introduction
In this blog post, we’ll walk through the creation of a simple Book Store Backend using Express.js. This project allows users to add books to a collection, each identified by a unique index number. We’ll explore the setup, routes, and how to test the API using Postman.
Project Overview
The Book Store Backend is built with Express.js, a minimal and flexible Node.js web application framework. The server handles a few routes to manage a collection of books, where each book is represented by a title and an author. The index number acts as a unique identifier for each book.
Setting Up the Project
Initialize the Project: Start by creating a new directory for your project and initializing it with npm:
mkdir book-store-backend cd book-store-backend npm init -y
Install Dependencies: Install Express.js:
npm install express
Create the Server: Create a file named
server.js
and set up a basic Express server:JavaScript
const express = require('express'); const app = express(); const port = 3000; app.use(express.json()); app.listen(port, () => { console.log(`Server is running on http://localhost:${port}`); });
AI-generated code. Review and use carefully. More info on FAQ.
Defining Routes
Book Collection: We’ll use an array to store our books:
JavaScript
let books = []; let index = 0;
AI-generated code. Review and use carefully. More info on FAQ.
Add a Book: Create a route to add a new book:
JavaScript
app.post('/books', (req, res) => { const { title, author } = req.body; const book = { id: index++, title, author }; books.push(book); res.status(201).json(book); });
AI-generated code. Review and use carefully. More info on FAQ.
Get All Books: Create a route to retrieve all books:
JavaScript
app.get('/books', (req, res) => { res.json(books); });
AI-generated code. Review and use carefully. More info on FAQ.
Get a Book by ID: Create a route to retrieve a book by its ID:
JavaScript
app.get('/books/:id', (req, res) => { const book = books.find(b => b.id === parseInt(req.params.id)); if (book) { res.json(book); } else { res.status(404).send('Book not found'); } });
AI-generated code. Review and use carefully. More info on FAQ.
Update a Book: Create a route to update a book by its ID:
JavaScript
app.put('/books/:id', (req, res) => { const book = books.find(b => b.id === parseInt(req.params.id)); if (book) { book.title = req.body.title; book.author = req.body.author; res.json(book); } else { res.status(404).send('Book not found'); } });
AI-generated code. Review and use carefully. More info on FAQ.
Delete a Book: Create a route to delete a book by its ID:
JavaScript
app.delete('/book/:id', (req, res) => { const bookndex = books.findIndex(b => b.id === parseInt(req.params.id)) if(bookIndex === -1) { res.status(404).send('Book not found') } books.splice(bookIndex, 1) res.status(204).send() })
AI-generated code. Review and use carefully. More info on FAQ.
Testing the API with Postman
To test the API, you can use Postman to send HTTP requests to the server:
Add a Book:
Method: POST
URL:
http://localhost:3000/books
Body: JSONJSON
{ "title": "The Great Gatsby", "author": "F. Scott Fitzgerald" }
AI-generated code. Review and use carefully. More info on FAQ.
Get All Books:
Method: GET
URL:
http://localhost:3000/books
Get a Book by ID:
Method: GET
URL:
http://localhost:3000/books/0
Update a Book:
Method: PUT
URL:
http://localhost:3000/books/0
Body: JSONJSON
{ "title": "The Great Gatsby", "author": "Fitzgerald" }
AI-generated code. Review and use carefully. More info on FAQ.
Delete a Book:
Method: DELETE
URL:
http://localhost:3000/books/0
Conclusion
This simple Book Store Backend project demonstrates how to set up an Express.js server with basic CRUD operations. By following this guide, you can expand the project further by adding more features such as user authentication, database integration, and more.
Feel free to check out the GitHub repository for the complete code and contribute to the project.
Happy coding!