The two sides of Node.js

By Ev Bogue - October 23rd 2013

Since I wrote Deploy Node at the beginning of 2013 I've gotten a lot of questions about the benefits and limitations of using Node.js in production.

There are two categories of questions I get that fit into the more esoteric world of 'why should I use Node.js?'.

  1. People who want me to justify Node.js to them.
  2. People who want to do something with Node.js that Node cannot do.

And then, of course, there are real technical questions about Node.js. "How do I get routing to work in Express?" and "How do I reverse-proxy inbound connections to my various Nodes?" These technical questions are answered in detail in Deploy Node, because Deploy Node is a technical book that shows you how to build tiny modular web servers using Node.js. But you get esoteric for free.

Onto the esoteric questions!

The first is 'why should I use Node.js when I'm using Apache/PHP or Ruby on Rails?'

When I get a question along these lines, I first have to determine if the person wants an honest answer, or wants to get in a fight with me about why Ruby on Rails or Apache/PHP is better than Node.js. If it's a fight they want, well, I'll just insult their ass and move on. If they really want to know the difference, then I attempt to explain it.

The first big difference is this: Node.js is evented and asynchronous.

Starbucks uses an async model to deliver drinks to customers as fast as possible. If Starbucks employees were web servers, they'd be the Node.js of web/ servers.

The other day, I walked into a Starbucks which WASN'T working in an evented/async way, and it was a horrible experience as a customer. Why?

Because the cashier was also the barista. She stepped away from the register every time she needed to make a drink, and then returned to the cash register to take the next order. This is ideal for neither Starbucks nor web servers.

Other web servers are threaded. This means they fire up a new cashier/barista combo every time a new browser connects to them. Each of these threads is its own process.

Meaning when your Apache web server gets to the top of Hackernews, you'd best be using a CDN (a content delivery network), or your shizzle will crash your server as it tries to spin up 1000 processes at once. Meanwhile, Node attempts to flawlessly scale up to 1000 requests with async baristas filling every single drink order. This doesn't mean Node won't crash, it does --this is why you keep your shit up with systemd-- but it won't crash crash your web server while spinning up thousands of threads.

The next difference has to do with the relationship with Apache/PHP.

PHP is a server-side scripting language. You run little server scripts that do things on your server, and those are then served using Apache.

Node, in contrast, allows you to program the Node.js API with JavaScript. To the untrained eye, this appears to be the same thing. It is not. So, if you want a webserver you can program with JavaScript, then you want to choose Node.js.

If you want a server-side scripting language to do some stuff behind the scenes, and then be served with Apache, then you need to choose this option. PHP has to be 'server safe' this means anything PHP does has to end up being an HTML site at the end of the day. This means you get some things you might be familiar with in Node, such as server-side templating. But you don't get a lot of other things, such as streams and all sorts of cool mad-science-y ways to interact with databases.

Still, you might be confused. The reason you're confused is both of these pieces of software interact with a web browser. A web browser is the thing you're reading this on. You might think of Apache/PHP or Ruby on Rails as a backend web server. Node.js is a backend web server too.

With Node.js, you're programming in the same language you to use to program a web browser, JavaScript. This means the Node.js developer you hire can also program a web browser. This means instead of hiring someone who is PHP or Ruby strong, and has no idea what to do with a web browser, you can just hire one dude who can do them both. This will save you $$$. Except Node.js programmers are expensive, and PHP/Ruby devs are a dime a dozen.

And I can't forget to mention Node.js has the coolest package manager ever, npm.

You might argue about is you can use Apache/PHP on an oldskool web host, but you can't use Node.js there. This is because old skool web hosts are old skool. If you want to use Node.js, you're going to have to learn how to use a VPS. I use Digital Ocean (rewards link). This means you might need to learn a little Unix/Linux to get around.

Get it? If you don't, email me and I'll attempt to make this argument stronger.

Now on to the other esoteric question I get about Node.js: Can Node.js do X thing Node.js cannot do?

Node is NOT for everything. I see Node.js developers and newbs alike suggesting Node.js for just about every problem. Again: Node.js is not for everything.

If you read the above carefully, you'll notice I mention two things often. One is web servers, the other is web browsers. Node.js is a web server with an API you can program in JavaScript. This means it's good at being a web server, and not good at other things.

If you want a window manager with an API you can program in Haskell, you might try Xmonad. If you want a window manager you can program in C, you might try dwm. If you want a text editor, you might try Vim or Emacs. If you want to encrypt your packets over a mesh network, try cjdns + Hyperboria. Node.js isn't good for these things, but other programs are. Node.js is not Linux, it's also not a terminal emulator. You run Node.js in a terminal, it is not a terminal. If you want an awesome terminal emulator, try using Terminology.

Node.js is good for programming async/evented web servers using JavaScript. Nothing more, nothing less. This gives you a huge range of expression, with web servers, but might not be the best tool for the job if you want to code something that isn't a web server.

You wouldn't use a hammer to hit a screw, don't expect Node.js to do something it can't do.

To learn more about Node.js, buy Deploy Node.


← The Exorcism of Steve Jobs

Hi! I'm Ev Bogue. I serve at a restaurant in Fayetteville, NC.

I no longer use email. Please use Decent to contact me.

about | ev@evbogue.com | gitmx