Node.js Smart Static Resource Server
Every now and then I think of some interesting programming project that would be fun to do. A lot of times I actually start working on these side projects. Unfortunately, I typically only stick with the project until I get to the finishing touches where it’s 90% done. Of course, it’s the last 10% of the project that always takes 90% of the time and by then I’ve stopped learning cool new stuff and all that’s left are annoying bugs. So before I start on my next project (after having lost interest in the last one), I’m going to write out my idea so that maybe someone else with more spare time than me can build it.
Every few weeks for the last year, someone around me mentions node.js as this cool new thing that everyone has to try out. In case you haven’t heard, node.js takes the V8 javascript engine and lets you run server side javascript. The especially exciting part is that all IO, including such common things as reading a file from disk and talking to a database, are asynchronous. It uses an event loop instead of a threading model to achieve really good concurrency, which apparently is important for things like really scalable network applications.
I’d love to have an excuse to play around with node.js but haven’t thought of anything that compelling until now. I usually like building web applications but unfortunately building traditional web applications in node would be missing the point (or painfully difficult). All the database drivers for traditional dbs like mysql and postgresql are blocking so you don’t get any of the benefits of node’s asynchronous IO model. If you are not doing anything cool with concurrency, you might as well stick with what you know and use python or ruby.
Ok, so what are cool concurrency related problems to solve? You could always write a chat server, but everyone and their cousin is writing a chat server in node these days. You could write a static file server or a web proxy, but that’s kind of boring and everyone already uses nginx. But what about taking static resource servers to the next level?
Imagine a static file server and/or web proxy that was geared towards serving resources like javascript and css used in larger web applications. Some features you don’t typically expect from a static file server but would be totally awesome and useful:
- automatic javascript minification – http://github.com/mishoo/UglifyJS looks promising
- automatic css minification – you might have to write this yourself, not too hard
- automatic image optimization – use pngcrush or something similar
- proxy to another server instead of the file system to get the unminified files
- rewrite static resource urls in html files on the fly to point to the minified sources
- concatenate multiple css and js files into single files based on usage as detected in html
- hooks for adding your own customizations – it’s just javascript… no need to dust off those c books
- track usage statistics and performance metrics in some key-value db
This wouldn’t replace nginx or CDNs or anything for really big scale stuff, but I think it could be a useful development tool. You would run this static resource server along side your single-threaded application server during development. You could even run it in production as the primary source backing a CDN. It would be easy to use with any sort of application server written in any other language. Being able to customize the server with javascript would make it easy to integrate any static resource packaging system you might divise for your application server. The best part though is that you get to write it in node!
Pingback: It All Starts Here « Jason James Hoyle