My first Node server

I rewrote this Python CGI server in to a Node.js server, or most of one. The main thing it does is accept POST content, writing it to a file and returning a generated filename. (Yes, that’s really a PUT. I know.) The Python CGI is 39 lines, the Node.js is 52 with 12 lines of NPM configuration. But then the Node version is a full server and has fancier message handling. Things I learned:

The Node community and docs are good but nowhere near as mature as Python. Also the Node philosophy is one small stable core, then you download add-on modules. Batteries not included. The core Node API is well documented. Beyond that you’re hunting through npmjs or maybe node.js modules for more.

NPM isn’t so scary. Grab a template, put your dependencies in a packages.json, run “npm install” and it will build a node_modules directory for you with all the dependencies. It’s good that’s a default tool deep in the Node chain. More info on npm.

express is a reasonable webapp framework and has a nice tutorial guide. I like the router style of specifying handlers.

node by default doesn’t do anything with POST body content, you have to install middleware to handle the input bytes. senchalabs / connect seems to be the default choice for doing fancy things like parsing JSON, etc. I just want the plain message text so the raw-body middleware middle is for me. It’s an awful lot of boilerplate to set up.

I’m gonna need a content hash; here’s a pure Javascript implementation of Murmur 128 that’s in npm as “murmurhash3js”. Hard to come by; it’s tricky doing true bit twiddling in Javascript without proper bit types. NPM turns up 10 different Murmur implentations, many of which either require native code or only implement the 32 bit hash.