This is an example of how to scaffold API endpoints to list / get / create / update / delete Posts in a Keystone website.
It's a modification of the default project created with the yo keystone
generator (see https://github.com/JedWatson/generator-keystone)
Gists don't let you specify full paths, so in the project structure the files would be:
routes-index.js --> /routes/index.js // modified to add the api endpoints
routes-api-posts.js --> /routes/api/posts.js // new file containing the Post API route controllers
It creates JSON endpoints for:
/api/post/list
- lists all posts/api/post/create
- creates a new post/api/post/{id}
- returns the details of posts by id/api/post/{id}/update
- updates a post by id and returns the details/api/post/{id}/delete
- deletes a post by id
The create
and update
routes accept either GET or POST requests for simplicity, and look in either the URL parameters of the request body for data using the same paths as set on the models.
You can add your own logic in for security, default values, limiting fields etc. by configuring the functions exported by /routes/api/posts.js
I think it's just a helper that makes it a lot easier to manage consistency in your responses. If you only have a couple of endpoints its value isn't obvious but when you have like 40 it's awesome to be able to just blindly pass the responder the err and data objects that come out of a query without manual formatting.