Chapter 10

Project: RESTful API

Build a book management API using JSON

Project Introduction

We will build a simple book management API using JSON as the data exchange format. This project demonstrates the practical application of JSON in a decoupled frontend-backend architecture.

GET
Get List
POST
Add Book
PUT
Update Book
DELETE
Delete Book

API Endpoint Design

Method Path Description
GET /api/books Get all books
GET /api/books/:id Get a single book
POST /api/books Add a new book
PUT /api/books/:id Update a book
DELETE /api/books/:id Delete a book

Request and Response Examples

GET /api/books

// Response
{
  "code": 0,
  "message": "Success",
  "data": [
    {
      "id": 1,
      "title": "Professional JavaScript for Web Developers",
      "author": "Nicholas C. Zakas",
      "year": 2011
    }
  ]
}

POST /api/books

Request Body

{
  "title": "Deep Dive into Node.js",
  "author": "Pu Ling",
  "year": 2013
}

Response

{
  "code": 0,
  "message": "Created Successfully",
  "data": {
    "id": 2,
    "title": "Deep Dive into Node.js",
    "author": "Pu Ling",
    "year": 2013
  }
}