This is an example, written in javascript (node.js), of a RES service with collections and resource references to books, which can be created, edited and deleted.
- It exposes a collection,
library.books
, containing book model references. - It exposes book models,
library.book.<BOOK_ID>
, of each book. - It allows setting the books' title and author property through the
set
method. - It allows creating new books that are added to the collection with the
new
method. - It allows deleting existing books from the collection with the
delete
method. - It verifies that a title and author is always set.
- It resets the collection and models on server restart.
- Have NATS Server and Resgate running
- Have node.js installed
Run the following commands:
npm install
npm start
Open the client
http://localhost:8083
Realtime updates
Run the client in two separate tabs to observe realtime updates.
System reset
Run the client and make some changes. Restart the node.js server to observe resetting of resources in clients.
Resynchronization
Run the client on two separate devices. Disconnect one device, then make changes with the other. Reconnect the first device to observe resynchronization.
GET http://localhost:8080/api/library/books
GET http://localhost:8080/api/library/book/<BOOK_ID>
POST http://localhost:8080/api/library/book/<BOOK_ID>/set
Body
{ "title": "Animal Farming" }
POST http://localhost:8080/api/library/books/add
Body
{ "title": "Dracula", "author": "Bram Stoker" }
POST http://localhost:8080/api/library/books/delete
Body
{ "id": <BOOK_ID> }