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 seem to be having this error when trying to implement something along the lines of Line 28 of routes-api-posts.js
app.get('/api/post/:id', keystone.initAPI, routes.api.posts.get);
Does anyone have any ideas? I'm using
Node 4.1.2
andKeystone 0.3.16
EDIT
Ah, after a small amount of digging and reading:
keystonejs/keystone#836
https://github.com/keystonejs/keystone/wiki/0.2.x-to-0.3.x-Changes
and in combination with:
keystonejs/keystone#1135
app.get('/api/post/:id', keystone.middleware.api, routes.api.post.get);
Seemed to do the trick.