I recently decided to actually run the examples in David Herron's Node Web Development book (disclaimer: Amazon affiliate link) and, while all examples until the middle of chapter 4 worked with the current Node 0.6.14 version, the Connect examples rely on the Router middleware, which TJ Holowaychuck removed from Connect.
Of course, Connect 1.6.1 still contains the Router, but it only works from Node 0.4.1 included to 0.5.0 excluded. And I still wanted to see these examples run on a current Node. A simple (and dirty) hack did the trick:
- clone Connect from Github:
git clone https://github.com/senchalabs/connect.git
- note the last commit before the Router was removed, to obtain the most up-to-date version: 7d96c75960c9a6502d44bc367c97ab8101d660e6, or 7d96c7 for short
- checkout that version:
git checkout 7d96c7
- in your project, install Connect normally. These days, this gives you version 2.1.0
lib/middleware/router.jsfrom your Connect detached checkout to the
node_modules/connect/lib/middleware/directory under your project directory.
- at this point, you will notice that the
router.generateMethodFunction()code makes use of the defunct
flatten()method. If you do not plan on chaining middleware, or more generally use the
middlewareproperty on your router callback, you can just comment out that line: this is sufficient to run the examples in David's book, although it breaks the middleware chaining mechanism for more advanced uses.
- restart Node: at this point, the book examples work.
Now, is this really a solution to use the Connect router ? Definitely not ! But it lets you run the Connect examples in the book, to get a better feel about them. If you really want to use that router, you should maintain it as a separate component; but in most cases, you will want to take the next step and move on to Express, or your own custom router component.
About the "Node Web Development" book itself
As commenters on Amazon.com have mentioned, Node itself is moving a breakneck pace, so any book published in 2011 is no longer completely usable in 2012 but, after browsing a number of blogs and being quite annoyed by the only other book available about Node, I consider this one to be the most useful, and i also contributed a number of errata with fixes on Packtpub.com, which the publisher may even publish to keep the book more current.
Like most Packt books, this one is rather thin and quickly read, but will help get the job done far more easily than spending days on blogs or trying to make sense of Node + npm modules available, without any idea where to start, or even relying on the very incomplete only other non-Packt book about Node. More books definitely need to be written while Node and its major middleware matures, but until then, this one is my choice.