Microservices are one of the hot buzzwords floating around these days, along with the cloud, IoT, and anything as a service. First, let’s explain the term. A microservice is a service with one and only one, very narrowly focused capability that a remote API exposes to the rest of the system.
A microservice isn’t a website or your ginormous web service. It is intentionally kept narrowly focused. Now, that doesn’t mean small. It just says everything included in the service should be related to everything else.
An excellent RESTful microservice should do a few things right out of the box. These are core function and shouldn’t be relegated to a plugin.
- Understand HTTP verbs
- Be debuggable
- Built to be unit tested
Node.js with Restify is constructed to be a microservice. The example app shows a mock microservice for a contact API. This will be our first pass, later we will make the app functional. We will add MongoDB, implement our RESTful services, etc.
Our app begins with index.js. This is where the server is fired up. We’ve broken that app into nice chunks using Node modules. We are using require() since it allows us to use Node without having to compile or transpile anything.
We are using Bunyan to do our mandatory logging. What’s nice is by also using the “restify-bunyan-logger” package, the logging becomes pretty much automatic. Every API call gets logged without any extra effort on our part.
We are pushing our Socket.IO handler into its own module. This helps keep index.js nice and clean. We are doing the same with our route handlers.
We also load up both the query and body parsers. They are included plugins for Restify and are usually needed for REST operations. Pay attention to how we send data to our modules, in particular, the server variable.
This is the first chapter of a continuing series on using Node for microservices. The chapter will be coming soon. The code is on my GitHub page at https://github.com/Rockncoder/nov11.