This forum has moved, please join us on github discussions. We will keep these old posts available for reference. Thank you!

REST API for fetch posts in json

Hi to all,
there will be the possibility of having the REST API on the various posts, authors, tags blog?

This would be very useful to be able to build templates SPA (Single Page Application) or in order to fetch the posts in json to render them in a mobile application.

Hi Lughino, doing this as a standard feature is an interesting idea. In a CMS project we often find that since users are cruising in from Google to glance at a page, we don’t necessarily want to “pay the freight” up front to send them a single page application, particularly when Nunjucks renders content server-side as well and as quickly as it does. The startup time isn’t always worth the reduced page transition time.

But there is no reason you can’t easily add REST APIs to any module at project level. Check out the self.route method of apostrophe-module (the base class of all modules, so you can mess with it in any module, such as one that extends apostrophe-pieces). You can also work directly with self.apos.app to add routes directly via Express if you don’t like the way self.route prefixes URLs for each module (/modules/module-name/your-route-name).

In particular, you might consider adding REST APIs to apostrophe-pieces-pages because it already provides the right convenience methods for fetching pieces on behalf of the general public.

1 Like