JSON API

A route in Jeasx is just a function that answers a request, so it can return JSON as well as HTML. This API is a single file: [books].js.

It checks request.method and returns a Response with Response.json(), so status codes and headers are explicit.

List books

Open /api-server/books in your browser, or search with ?q=fowler.

bashGET
curl http://expo.jeasx.dev/api-server/books

Add a book

Send title and author as JSON. The API answers with 201 Created and the new book, or 400 Bad Request if a field is missing.

bashPOST
curl -i -X POST http://expo.jeasx.dev/api-server/books \
  -H 'content-type: application/json' \
  -d '{"title": "Eloquent JSX", "author": "Ada"}'

Other methods

Everything else gets 405 Method Not Allowed with an Allow header.

bashDELETE
curl -i -X DELETE http://expo.jeasx.dev/api-server/books

More on returning different kinds of responses in Response objects and Content negotiation.