Sunday, July 27, 2014

Tailoring Express & Node.js for Microservices

Recently, I needed to begin putting together a Microservices-based system for a research project at the University of New Mexico. I've used sinatra for this kind of thing in the past, but I was interested in using node.js for this system rather than our usual Ruby stack - basically because we've begun to use ember for our front-end work, and it made some sense to unify our development languages. I looked around for a Sinatra-like replacement in the Node ecosystem, and didn't have much luck. As I'd used Express in the past, I realized that I could likely come close to what I was looking for using it to some extent, even though out-of-the-box it has a bit more power that I need. So I went ahead and created an Express project and began to strip out what I could to make it a pure microservice delivery environment rather than a web server.

Now keep in mind, Express is more like a (very) stripped down version of Rails than a system like Sinatra - which is just fine. Rails gives you quite a bit, but you don't always need everything that it supplies (at least I don't). Sinatra is very easy to use, and I really enjoy its DSL and flexibility, but you need to build a few extra things that Express will give you for free. Nevertheless, the routing model in Express is very similar to Sinatra, and it allows you to use Node's primitives to deliver data in various formats (though all I was really interested in was JSON, honestly). So it seemed like a good starting point.

First, create the application - once you've installed Express, Node, and the various dependencies, you create a simple Express application via the express command. This gives you a standard Express application layout that you can now start to tweak. First, let's start with the app module in the application root. This is referenced by the www script in the bin subdirectory called by npm when you start an express application with npm start:


Note some key changes in the app module. First, we've removed access to the /public subdirectory that usually contains various web resources like images, scripts, and HTML pages. We've also removed references to views or view engines, as we aren't providing any visual user interfaces to this system. Finally, we've added the ability to emit debug statements from the module, and we've changed the error handlers to return JSON rather than HTML views describing errors, while keeping the separation between production and debug error handling and resulting stack trace containment.

At this point, you can modify your package.json file to remove unused dependencies:


The only dependency we've removed is the view engine, Jade. Remember, we originally created this using standard CSS processing, so we didn't include a CSS pre-processor like Less.

That's it! You now have a microservice container configured with no visual engines, storage, or elements. Type npm start at the command line, and you should be ready to go.

2 comments: