Last time, I talked about the Bowling Game kata in Node.js, and mentioned that the next step in looking at the MEAN stack is Express, which is a JavaScript web application framework.
The inclusion of Express means that the game scoring will need to be exposed as a web application. This seems like a good use case for REST. I’m aware that restify is a better fit for this kind of application, but in keeping with exploration of the MEAN stack, I stuck with Express. In doing some more exploration with Restify, I found that it needs the node development environment to be installed on Windows, which I found off-putting.
For testing, I used supertest, which offers nice functionality for testing HTTP servers with a fluent interface, another programming technique that I’m fond of.
Here’s a sample of what the tests look like:
The supertest library is what provides the request
object. Whenever you call request
, you need to execute it with an end()
call. I fumbled with this for a few hours. You’ll notice that I’m ignoring the error object. I found that when I tried to use it, not only did it clutter up the test code with a done
callback:
but I also found while running in a mocha -w
loop, it caused Mocha to throw exceptions after some time elapsed. I need to dig into this deeper.
Here’s the game code, using Express to expose some REST endpoints for knocking down pins and scoring the game:
As you can see, while the plumbing is more complicated, it uses the same structure as the pure Node.js solution. That’s why I find tools like the Bowling Game kata to be valuable. By solving a problem I’ve solved many times before, the problem fades into the background and I’m able to focus on learning the language.
The next step is to include AngularJS.