Services
Web Development with Clojure, Third Edition — by Dmitri Sotnikov, Scot Brown (32 / 107)
👈 Chapter 5 Setting Up for Success | TOC | ClojureScript Development Tools 👉
We’ll factor out the API endpoints from the guestbooks.routes.home into a separate guestbook.routes.services namespace. The endpoints in the home namespace will be responsible for serving HTML and related assets to be used by the web browser to render the page. Meanwhile, the services namespace will provide a data API (for example, JSON, EDN, and others) to be used by the client over Ajax.
The key motivation for separating our page rendering code and the API endpoints is to facilitate maintainability. Keeping all of our logic in one place has been manageable so far, but it will require discipline to keep things organized as our application grows. We should separate our model logic early so that it doesn’t become entangled with our routing going forward. We should also separate our API routes (endpoints responsible for querying and modifying data) from our application routes (our webpages). As our app grows, having a well-documented, validated, and tested API to build against will be invaluable.
The ability to interact with our data independently of a heavyweight client allows us to tailor our views to the needs of our users as they evolve. This approach also makes it easier to work on tasks independently. New service endpoints can be implemented and tested independent of the client UI development…