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
Hi there,
I'm having a hard time making my API work.
Models: I have a Destination model, with services attached to it, like this (in /models/Destination.js):
I've built routes like this (in /routes/index.js):
And finally I have my exported method in /api/dest.js, like this:
Now, what I receive back by calling /api/dest/:code is incomplete:
{"collection":{"_id":"59b424b5d7938827d40a755c","name":"Zanzibar","code":"ZNZ","desc":"Where Freddie Mercury was born!"}}
Where is my 'hotels' relationship? I do see relationships in AdminUI (almost as expected, 'filters' gets not respected though). But no matter what, the API call has no 'hotels' field. I've tried 'populate' in two versions (the alternative is the one commented above), I've tried using populateRelated, like this:
This way, console.log shows me the correct list of result, but then I ran out of ideas about how to get back the result into returned item.
Can anyone help or suggest?
Running Keystone with
DEBUG=keystone:* node keystone
helped figure out a bit, but not enough.Thanks in advance.
Filippo
**EDIT:
I've been able to hack this by manually populating the hotel list, like this:
I would anyway expect that populate work, just to mantain data the way they are ('destination' should be a model with a 'hotels' array field).
Thanks for any suggestion!
F.