{ res.json(books); }); // GET single book by ID app.get('/books/:id', (req, res) => { const book = books.find(b => b.id === parseInt(req.params.id)); if (!book) return res.status(404).send("Book not found"); res.json(book); }); // POST – Add new book app.post('/books', (req, res) => { const newBook = { id: books.length + 1, title: req.body.title, author: req.body.author }; books.push(newBook); res.status(201).json(newBook); }); // PUT – Update existing book app.put('/books/:id', (req, res) => { const book = books.find(b => b.id === parseInt(req.params.id)); if (!book) return res.status(404).send("Book not found"); book.title = req.body.title || book.title; book.author = req.body.autho"> { res.json(books); }); // GET single book by ID app.get('/books/:id', (req, res) => { const book = books.find(b => b.id === parseInt(req.params.id)); if (!book) return res.status(404).send("Book not found"); res.json(book); }); // POST – Add new book app.post('/books', (req, res) => { const newBook = { id: books.length + 1, title: req.body.title, author: req.body.author }; books.push(newBook); res.status(201).json(newBook); }); // PUT – Update existing book app.put('/books/:id', (req, res) => { const book = books.find(b => b.id === parseInt(req.params.id)); if (!book) return res.status(404).send("Book not found"); book.title = req.body.title || book.title; book.author = req.body.autho"> { res.json(books); }); // GET single book by ID app.get('/books/:id', (req, res) => { const book = books.find(b => b.id === parseInt(req.params.id)); if (!book) return res.status(404).send("Book not found"); res.json(book); }); // POST – Add new book app.post('/books', (req, res) => { const newBook = { id: books.length + 1, title: req.body.title, author: req.body.author }; books.push(newBook); res.status(201).json(newBook); }); // PUT – Update existing book app.put('/books/:id', (req, res) => { const book = books.find(b => b.id === parseInt(req.params.id)); if (!book) return res.status(404).send("Book not found"); book.title = req.body.title || book.title; book.author = req.body.autho">

ways

const express = require('express');
const app = express();
app.use(express.json());

let books = [
  { id: 1, title: "The Alchemist", author: "Paulo Coelho" },
  { id: 2, title: "1984", author: "George Orwell" }
];

// GET all books
app.get('/books', (req, res) => {
  res.json(books);
});

// GET single book by ID
app.get('/books/:id', (req, res) => {
  const book = books.find(b => b.id === parseInt(req.params.id));
  if (!book) return res.status(404).send("Book not found");
  res.json(book);
});

// POST – Add new book
app.post('/books', (req, res) => {
  const newBook = {
    id: books.length + 1,
    title: req.body.title,
    author: req.body.author
  };
  books.push(newBook);
  res.status(201).json(newBook);
});

// PUT – Update existing book
app.put('/books/:id', (req, res) => {
  const book = books.find(b => b.id === parseInt(req.params.id));
  if (!book) return res.status(404).send("Book not found");

  book.title = req.body.title || book.title;
  book.author = req.body.author || book.author;
  res.json(book);
});

// PATCH – Partial update
app.patch('/books/:id', (req, res) => {
  const book = books.find(b => b.id === parseInt(req.params.id));
  if (!book) return res.status(404).send("Book not found");

  if (req.body.title) book.title = req.body.title;
  if (req.body.author) book.author = req.body.author;
  res.json(book);
});

// DELETE – Remove a book
app.delete('/books/:id', (req, res) => {
  const index = books.findIndex(b => b.id === parseInt(req.params.id));
  if (index === -1) return res.status(404).send("Book not found");

  const deleted = books.splice(index, 1);
  res.json(deleted);
});

// HEAD – Get headers only
app.head('/books/:id', (req, res) => {
  const book = books.find(b => b.id === parseInt(req.params.id));
  if (!book) return res.status(404).end();
  res.status(200).end();
});

// OPTIONS – Allowed methods
app.options('/books', (req, res) => {
  res.set('Allow', 'GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS');
  res.send();
});

// Start server
app.listen(3000, () => {
  console.log("Server running at <http://localhost:3000>");
});

image.png

image.png

image.png

image.png