scripting/river4 — pull request #3 by ameensol (2014-09-26 11:34:12+00:00)
Title: Use node.js as webserver

Hi Dave,

Great project! Really cool to see the NYT put a river on their home page, and now that they've done it everyone else probably will want to.

I've been wanting to play around with River4 for a while now, so I decided to give it a shot. I'm using a Digital Ocean droplet, so I wanted to see if I could use the River4.js script to also serve the index.html file, and with a few tweaks I was able to do.

In order to dynamically serve the user's river files, the server has to compare the pathname to each river file in the /rivers directory, and serve that file if there is a match. Since the pathname is going to be dynamic (/rivers/apple.js || /rivers/nytriver.js || /rivers/*.js), using a switch statement is difficult. The only way I could think of still using a switch statement was to use regex matching for the cases and then use switch(true). I thought this was overly complicated so I rewrote the router to use if/else statements.

The danger of relying on this setup is two-fold. First, it isn't likely to scale very well. The same process that is updating the feeds is also responsible for serving them. The process could get bogged down pretty easily if there was a lot of traffic.

If scale isn't an issue though, the other problem is that it introduces a single point of failure. If the node process crashes while updating a feed, users will no longer be able to access the site. On the other hand, if this script's role is simply to fetch feed updates and another server is responsible for serving the website, then if the node process crashes users will still be able to see the site, though a somewhat outdated version of it (depending on how long the node process stays down).

In any case, I don't expect to have either of the above issues on my personal river, since it's more for me than anyone else. I thought I'd share my code regardless, in case you find any of it helpful. I look forward to more of your hacks! I think you're on to something big here.

Amiably,
Ameen