Node.js APIs shouldn’t have to involve any guesswork, so I set out to create a case study Node.js microservice API that had all of the rough edges smoothed off. It was also a good chance to play with some new Postgres JSON features. Thus, the Yeti Threads API was born. The Yeti Threads API is a conversation threading forum API. It’s MIT licensed, so feel free to use it to add conversation threading to your applications.
Writing APIs in Node.js should be really, really straightforward, even if you’re doing live eventing. You’re probably exposing a REST API that has basic Create Read Update Delete handlers that roughly translate to some database calls. You’re probably sending basic updates over a websocket based on the changes to the database. These things are so well established, that several products out there attempt to commoditize these sorts of APIs.
Let’s talk about tool choice first.
Tool Choice
JSON Web Tokens because this is a microservice, we’re not providing our own authentication and user management. OpenID Connect is a standard extension on oAuth2 that handles this problem nicely.
Postgresql is not a new database, but it is getting to be more popular. With new JSON features in 9.4 and 9.5, it brings the best of both worlds in terms of key-stores, document-stores, and SQL querying. Additionally, the new logical replication features provide a basis for much more powerful and flexible scaling.
hapi is not the most popular HTTP service for Node.js, that honor belongs to Express, but it provides a much more opinionated and direct approach for writing routes and plugins, as well as having a lot of complimentary libraries that are flexible for use in and outside of hapi.
VeryModel is my go-to library for modeling. It provides a flexible way to manage data-state in a way that can easily transition from a source to export way.
I wrote Gatepost to experiment with the idea of binding queries directly to a model for Postgresql.
PGBoom is a hapi plugin which translates Postgres errors to Boom’s HTTP Errors, so that we didn’t need to write error logic for every handler.
Websockets have good support these days, and provide a clean way for APIs to give continued updates to client applications. Yeti Threads uses websockets to send notifications relayed from Postgresql’s NOTIFY channels.
The Series
In follow-up blog posts, we’ll dive into the individual pieces, experiments, and choices of Yeti Threads in order to provide a deep understanding of why and how. If you’d like to get it going, get a free trial of auth0 (or any other OpenID Connect provider) and give it a whirl. I’ll be working on it more during the series, like adding knex.js-based migrations, and possibly breaking out the eventing into an npm module. Pull requests, issues, and critique is welcome!
&yet provides consulting for APIs: architecting, coding, testing, and deploying -- whichever services you need.
The next part of this series is now up: A microservices case study: Auth.