Oh, no – I'm beginning to understand Node.js.
It's one of those terms you inevitably hear as a developer. I'm sure I've been hearing it for over a decade since Ryan Dahl came up with it. Generally all I knew about it was that some people liked it, that it had something to do with JavaScript, and that if you wanted to use certain other cool things in your programs, you were often assumed already to have it installed – whatever it was – so that you could simply use involve it within whichever instruction of whichever tutorial. That kind of thing generally puzzled or scared me, so I tended to veer to a different avenue.
I still wanted to learn what it was, though, in isolation. I remember watching a video or two and still not getting it. It was "non-blocking," of course. We all know it's non-blocking, of course. Presumably there was something more fundamental about the spirit of the thing that I was missing.
(I can't say I care for the official name "Node.js", which includes that period. Even with my respect for exact names of software products, and even though there is a file called "node.js" which is important to the picture, and even though I think some people pronounce the period as "dot," I'm tempted to type "Node JS" to reflect my own speech. However, pending nominal disambiguation, I think just about everyone would be happy with just "Node," so I'll go with that here.)
It's only this week, when I've been experimenting much more intentionally with the possibility of hosting this site myself, that it's been finally, rapidly, clicking. Here's the explanation I was missing.
JavaScript is, to sidestep scrutinous debate, a programming language. To use it, a programmer types a list of instructions they expect a thing, eventually, to execute in sequence.
That "thing" is a web browser. The purpose of the program pertains to the display, interactivity, or other workings of a web page or app that the browser facilitates. Anyone who tinkers enough with a typical web browser on a desktop computer discovers a "console" into which they can type JavaScript instructions individually for the browser to run instantly.
Node is another "thing." In the way a web browser can be expected to run JavaScript, Node can be expected to run JavaScript. But Node is not a web browser. It doesn't have a web page in one hand and JavaScript in the other hand, and makes no sort of attempt to use them together.
Rather, Node is essentially just another app that runs natively on a computer system, like the Mac, Windows or Linux. You can write a JavaScript file that does any kind of simple abstract programming task – reciting every number from one to a thousand, reversing the letters of a word, printing the date, or whatever – then tell Node to run it. That's what Node does.
But it's more than a little programming sandbox. Because it's a typical app, Node has the ability to do things like loading, modifying and saving files. Instead of just printing the date, Node can be instructed to print the date and save it to a text file.
Additionally – and more to its intended purpose – Node has the ability to listen for incoming connections from other computers on a local network, or even from "out there" on the Internet, and send back a response according to the instructions of the program it's running. In a word, it can be a server.
That's why I started to understand the appeal this week. I've been writing code for hosted web servers for years, and I've dealt entirely with Apache and PHP. Those are some of the oldest and trustiest tools in web-serving history, but even through their modern refinements shows their long-accumulated cruft and complexity. If you're just trying to make the slightly interactive web site you imagine, the tangents and side-routes involved in learning to use them properly pile up. Writing a program in Node which does the equivalent of all that, I discovered this week, is trivial. It's as simple as it feels like it should be. "When someone else uses their web browser from across the world to activate this program, make this quick decision, select a file based on that, and send it back to them" takes barely any more code, knowledge or setup than typing the English words.
And yes, you have to learn how to use Node too. If you do want your server to load and return one of several files from the computer it's running on, you have to look up the two or three instructions or commands that do it. But what you don't have to do, if you're already familiar with JavaScript as a web developer, is even think about learning a different programming language.
I'm glad I've learned about Apache and PHP, and learning any traditional programming is enriching. But PHP feels like a chore I've learned to love, while JavaScript feels more like something I just love. Even closer to my heart is the tidy and tranquil CoffeeScript, which converts to JavaScript. To me, the idea of using CoffeeScript to program a server feels a little like being offered magical wings.
Node is also distributed under the MIT license – it's essentially free to download, use, and even to use in other commercial products. In spirit, it feels like it's there for "the community." On top of that – and this is a full topic of its own – Node comes with "NPM," the "Node Package Manager," for easily sharing and downloading other specialized, freely-offered programs and frameworks. Taking all of that into account, Node feels much like the open web: something an enthused community can sink its teeth into, play and create with, accomplish work with, and build on together.
To me, now that I'm starting to get it, the possibilities seem wide open. No wonder I kept hearing about it.