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 urgently need help with something, I created an example with a very simple list (of countries) and created api routes like instructed:
app.get('/api/countries', keystone.middleware.api, routes.api.countries.list);
I get the error
Cannot read property 'find' of undefined
, the List object exists but it doesn't have amodel
property. Does anyone know why this is? The keystone admin UI works as expected and there are several objects in the database.Update: I found the solution, the problem was that I used keystone.List (capitalized) instead of keystone.list. Hopefully someone else will be helped by this answer.