This is a TypeScript REST API for managing a subject index with words and page numbers. It allows you to search, add, update, delete entries, and sort them alphabetically.
- Node.js (v12+ recommended)
- PostgreSQL
- Clone (or create) the project and navigate to it:
mkdir book-index-app
cd book-index-app
- Create the files as specified in the project structure.
Install dependencies:
npm install
- Create a PostgreSQL database named
book_index
(or adjust per your .env).
Run the SQL script to create the table:
psql -U postgres -d book_index -f create_tables.sql
- Create a
.env
file by copying.env.example
(or provide via environment variables):
cp .env.example .env
- Start the development server:
npm run dev
- Or run in production mode:
npm start
The API will be available at http://localhost:3000.
curl -X POST http://localhost:3000/entries
-H "Content-Type: application/json"
-d '{"word": "apple", "pages": [1, 5, 9]}'
curl -X POST http://localhost:3000/entries
-H "Content-Type: application/json"
-d '{"word": "banana", "pages": [2, 4, 6]}'
curl -X POST http://localhost:3000/entries
-H "Content-Type: application/json"
-d '{"word": "cherry", "pages": [3, 7, 11]}'
curl -X POST http://localhost:3000/entries
-H "Content-Type: application/json"
-d '{"word": "date", "pages": [4, 8, 12]}'
curl -X POST http://localhost:3000/entries
-H "Content-Type: application/json"
-d '{"word": "elderberry", "pages": [5, 10, 15]}'
curl http://localhost:3000/entries
curl "http://localhost:3000/entries?search=an"
curl "http://localhost:3000/entries?sort=alphabetical"
curl http://localhost:3000/entries/<APPLE_ID>
curl -X PUT http://localhost:3000/entries/<APPLE_ID>
-H "Content-Type: application/json"
-d '{"word": "apricot", "pages": [1, 3, 5]}'
curl -X DELETE http://localhost:3000/entries/<BANANA_ID>
curl http://localhost:3000/entries/<BANANA_ID>
curl -X POST http://localhost:3000/entries
-H "Content-Type: application/json"
-d '{"word": "fig", "pages": [13, 17, 21]}'
curl -X POST http://localhost:3000/entries
-H "Content-Type: application/json"
-d '{"word": "grape", "pages": [14, 18, 22]}'
curl -X POST http://localhost:3000/entries
-H "Content-Type: application/json"
-d '{"word": "honeydew", "pages": [15, 19, 23]}'
curl -X PUT http://localhost:3000/entries/<FIG_ID>
-H "Content-Type: application/json"
-d '{"pages": [2, 4, 6]}'
curl -X DELETE http://localhost:3000/entries/<CHERRY_ID>
curl http://localhost:3000/entries
curl -X POST http://localhost:3000/entries
-H "Content-Type: application/json"
-d '{"word": "kiwi", "pages": [16, 20, 24]}'
20. Update the "elderberry" entry by changing word to "elderflower" (replace <ELDERBERRY_ID> with its actual id)
curl -X PUT http://localhost:3000/entries/<ELDERBERRY_ID>
-H "Content-Type: application/json"
-d '{"word": "elderflower"}'