Understanding node. js

Source: Internet
Author: User
Tags windows support hosting joyent

Understanding node. js

Posted on 29/4/10 by Felix Geisendörfer

node. JS has generally caused II reactions in people I ' ve introduced it to. Basically people either "got it" right away, or they ended up being very confused.

If you have been in the second group so far, this is my attempt to explain node:

    • It's a command line tool. You download a tarball, compile and install the source.
    • It Let's run JavaScript programs by typing ' node My_app.js ' in your terminal.
    • The JS is executed by the V8 JavaScript engine (the thing, makes Google Chrome so fast).
    • Node provides a JavaScript API to access the network and file system

"But I can do everything I need in:ruby, Python, PHP, java, ...!".

I hear you. And you is right! Node is no freaking unicorn that would come and do your work for you, sorry. It's just a tool, and it probably won ' t replace your regular tools completely, at least isn't for now.

"Get to the point!"

Alright, I'll. Node is basically very good when you need to do several things at the same time. Ever written a piece of code and said "I wish this would run in parallel"? Well, in node everything runs in parallel, except your code.


That's right, everything runs in parallel, except your code. To understand this, imagine your code is the king, and Node was his army of servants.

The day starts by one servant waking up the king and asking him if he needs anything. The king gives the servant a list of tasks and goes back to sleep a little longer. The servant now distributes those tasks among he colleagues and they get to work.

Once a servant finishes a task, he lines up outside the Kings quarter to report. The king lets one servant in at a time, and listens to things he reports. Sometimes the king would give the servant more tasks on the the-the-out.

Life was good, for the king's servants carry out all of the he tasks in parallel, and only report with one result at a time, s o the King can focus. *

"That's fantastic, but could you quit the silly metaphor and speak geek to me?"

Sure. A Simple node program could look like this:

var fs = require(' FS ')
, SYS = require(' SYS '
Fs. Readfile ( ' Treasure-chamber-report.txt ',  function< Span class= "Br0" > (Report)  {
  sys. Puts ( "Oh, look at all my money:" +report });

Fs. Writefile ' Letter-to-princess.txt ',  function ({
  sys. Puts ( "can ' t wait for hear back from her!" });

Your Code gives node the both tasks to read and write a file, and then goes to sleep. Once node has completed a task, and the callback for it is fired. But there can is only a callback firing at the same time. Until that callback had finished executing, all other callbacks had to wait on line. In addition to, there was no guarantee on the order in which the callbacks would fire.

"So I don ' t has to worry about code accessing the same data structures at the same time?"

You got it! That ' s the entire beauty of javascripts Single-threaded/event Loop design!

"Very Nice, and why should I use it?"

One reason is efficiency. In a Web application, your main response time cost was usually the sum of time it takes to execute all your database Querie S. With node, you can execute all your queries at once, reducing the response time to the duration it takes to execute the Slowest query.

Another reason is JavaScript. You can use node to share code between the browser and your backend. JavaScript is also on the its-to become a really universal language. No matter if you do Python, Ruby, Java, PHP, ... in the past, you've probably picked up some JS along the "right"?

And the last reason are raw speed. V8 is constantly pushing the boundaries in being one of the fastest dynamic language interpreters on the planet. I can ' t think of any and language that's being pushed for the speed as aggressively as JavaScript are right now. In addition to that, node ' s I/O facilities is really light weight, bringing as close to fully utilizing your system ' s Full I/O capacities as possible.

"So is saying I should write all my apps in node from now on?"

Yes and No. Once you start to swing the node hammer, everything are obviously going to start looking like a nail. But if you ' re working on something with a deadline, you might want to base your decision on:

    • is low response times/high concurrency important? Node is really good on that.
    • How big is the project? Small projects should be fine. Big projects should evaluate carefully (available libraries, resources to fix a bug or both upstream, etc.).

"Does node run on Windows?"

No. If you is on Windows, you need to run a Vsan (I recommend VirtualBox) with Linux. Windows support for node was planned, but don ' t hold your breath for the next few months unless the P Ort.

"Can I access the DOM in node?"

Excellent question! No, the DOM is a browser thingy, and node's JS engine (V8) is thankfully totally separate from all that mess. However, there is people working on implementing the DOM as a node module, which may open very exciting possibilities suc H as unit testing client-side code.

"Isn ' t event driven programming really hard?"

That's depends on you. If you already learned how to juggle AJAX calls and user events on the browser, getting used to node shouldn ' t is a proble M.

Either, test driven development can really help you to come up with maintainable designs.

"Who is using it?"

There is a small/incomplete list in the node wiki (scroll to "Companies using Node"). Yahoo is experimenting with node for YUI, Plurk are using it for massive comet and Paul Bakaus (of the JQuery UI fame) is build ing a mind-blowing game engine that had some node in the backend. Joyent has hired Ryan Dahl (the creator of node) and heavily sponsors the development.

Oh, and Heroku just announced (experimental) hosting support for node. js as well.

"Where can I learn more?"

Tim Caswell is running the excellent-to Node blog. Follow #nodejs on Twitter. Subscribe to the mailing list. And come and hang out in the IRC channel, #node. js (yes, the dot was in the name). We ' re close to hitting the lurker-mark there soon:).

I ' ll also continue to write articles here on debuggable.com.

That's it for now. Feel free to comment if you had more questions!


*: The metaphor is obviously a simplification, but if it's hard-to-find a counterpart for the concept of non-blocking in R Eality.

Did you like the This blog post? If So, please consider subscribing to the Blog RSS feed.

(theoretically) Related Posts
    • Node.js-dealing with uncaught exceptions
    • Testing node. JS modules with Travis CI
    • Parsing form data with node. js
    • Streaming UTF-8 (with node. js)
    • Releasing Node-mysql 2.0.0-alpha
    • Understanding hidden classes in V8
    • Unit Testing with node. js
    • Parsing file uploads at $ MB/s with node. js
    • node. js
    • Streaming file uploads with node. js

You can skip to the end and add a comment.

COMMENTS | ADD COMMENT Mark Story said on APR:

Great introduction Felix, I ' ve been reading the node. JS docs and it looks pretty amazing. Just need to get the time to get my hands dirty:)

Felix Vargas said on APR:

hey! Thanks for the intro ...

Karl G said on APR:

Hi Felix. Good job on the node evangelism. I just have one complaint.

> V8 is constantly pushing, the boundaries in being, the fastest dynamic language interpreter on the planet. I can ' t think of any and language that's being pushed for the speed as aggressively as JavaScript are right now.

This is, unfortunately, not true. To my knowledge, the fastest dynamic language interpreter is Mike Pall ' s Luajit. There was a extended exchange between Mike, Brendan Eich, and Andreas Gal on Lambda the Ultimate at the end of March [1] But in brief, JS language semantics prevent some optimizations that Luajit have in place.

[1] http://lambda-the-ultimate.org/node/3851

Botanicus said on APR:

Hi, I like this blog, thanks for it! Anyway I ' m writing about node. js as well athttp://blog.101ideas.cz

Felix Geisendörfer said on APR:

Karl g:i agree with your partially, so I changed the wording to ' one of the fastest '.

However, I don ' t see evidence this Luajit is the *fastest* interpreter. At least we ' d has to decide on an exact definition of "fast" first:).

Luajit seems to does very well in the game of language benchmarks, but those seem rather un-realistic at times. I mean I would consider anything related to closures much more important (speed-wise), than any raw number crunching.

Either, it ' s great to see the kind of innovation going on there.

Mislav said on:

I loved the metaphor.

Vipul Limbachiya said on:

Great article and Introduction to node. js.

It helped me to understand very basic thing about it and where and what to use it.

Thanks for sharing


Paul Grayson said on Jul:

Nice introduction Felix. A lot of people seem to having heard of node. js but miss the evented/non-blocking aspect and think it ' s just another server Stack-all be it a JS one. I ' ll be pointing them at the article in the future;-)

Stuart said on Jul:

Love your RSS feed bug! Gave me a laugh.

Bruce said on Jul:

Check out Simon Willison ' s blog.


He uses an analogy with bunnies:)


Loving your work with multipart uploads using node.

Asksuperuser said on:

Why wouldn ' t it run on Windows since V8 can?

Felix Geisendörfer said on:

Asksuperuser:it runs on Cygwin now. Full Windows support should is possible as well, there just needs to be somebody to actually get a build going.

Jonah Bron said on:

Cool, I didn ' t really understand what node. js was before. Nice introduction, thanks!

Alexis Lee said on:

Thanks, this is just what I needed.

For nonblocking, I could be misunderstanding of course ... I Imagine a servant standing in front of the king, and making a phone call.

Of course the King can hire a peon to wait for the servant-to-finish his call, tell the Peon how to handle the results and Ask it to the him-but that's a lot of trivia to deal with a rude servant. Off with his head!

Alex C said on:


I ' m still not clear ... is the something I would use for a Web page or for a "stand alone/compiled app"?
The bit about asynchronous database calls made me just a little bit giddy. Sounds really cool.

I worry that I'm not quite smart enough to leverage the power of so but maybe we ' re all a little scared (and excited) AR Ound new languages ... but I guess you don ' t know until try.

Alex C said on:

Like was it just ' automatically faster ' or do I had to change my program architecture to take advantage?

Sam said on:

Great explanation of the most important web programming language. What is the other alternatives?

geries said on Sep, £ º

Thanks for the explanation, was kind of lost.

Here are a list of modules for node.jshttp://github.com/ry/node/wiki/modules#database in case anybody are wondering (like I did) what DB to use. Also, you'll find parsers, template systems, middleware, frameworks, etc. http://github.com/ry/node/wiki/modules

Smegroume said on Sep:

Nice blogger man also really like the design

www.tech-wd.com said on Sep:

Of course the King can hire a peon to wait for the servant-to-finish his call, tell the Peon how to handle the results and Ask it to the him-but that's a lot of trivia to deal with a rude servant. Off with his head!

A-dizzle said on Sep:

Apparently I am taking a calculus class from Ryan Dahl ' s mother. She was talking on how She son made a bunch of money making this thing called "node. js" The other day. Small World ...

WJR said on Sep:

How can I use the If my hosting site is something like godaddy.com or DreamHost?

Bruce said on OCT:

Just checking Google ' Nodejs hosting ' came us these articles ...



This was a list from a stack overflow thread ... hope that helps.
Also joyent seemed to pops up alot.

Heroku–closed Beta

Elusivehippo–not working




Amazon EC2

Rackspace Cloud



No.de–closed Beta

Dreamhostps–need A total reconfiguration of virtual machines

Webbynode–setup Guide

Cliff James said on OCT:

Just for clarification. Node isn ' t a generic tool for doing event-based multi-threading right? Isn ' t more about event-based async IO. I mean if you were developing some kind of processor intensive task (no IO) so you wanted split across multiple cores, I Don ' t see what Node helps you.

Felix Geisendörfer said on OCT:

Cliff James:it depends, but generally the distribution of CPU bound algorithms are not a strength of node, because you hav E to split tasks up into processes and use sockets for communicating between them (which are significant overhead in Certai n scenarios). So look at your problem and see if you can life with the IPC overhead or not.

Marko said on OCT:

Very good introduction, best I had found for #nodejs ... thanx


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.