Dear readers! This article is 3 years old, but, in my opinion, it contains interesting things. Thank you for understanding.
Node.js, in general, causes two reactions in people to whom I represent it. Basically, they either understand it right away, or end up not understanding anything.
If you still fall into the second category, let me try to explain Node:
- This is a command line tool. You download the archive, compile the source and install it.
- You can run your JavaScript programs by writing “node my_app.js” in the terminal.
- JavaScript code is executed in the V8 javascript engine (the thing that makes Google Chrome so fast).
- Node introduces the JavaScript API for accessing the file system and the network.
But after all, I can do everything that I need in Ruby, Python, PHP, Java, etc
I hear you! Node is not a freaking unicorn that will come and do all the work for you, forgive me. It's just a tool and it probably won't replace your regular tools, at least for now.
Get to the point!
Of course, come on. Node is good when you need to do several things at the same time. Have you ever written a piece of code and said - "I want to parallelize it"? Well, in Node, everything works in parallel, except for your code.
')
What?
Yes, yes,
everything works in parallel, with the exception of your code. To understand this, imagine that your code is the king, and Node is his army of servants.
The day begins with the fact that one servant awakens the king and asks him if he needs anything. The king gives the servant a list of tasks and goes to bed. Now the servant distributes these tasks among his colleagues and they go to work.
After the subordinate finishes work, he goes to report about it to the royal quarter. The king listens to one servant at a time. Sometimes the king throws up more quests when the servant leaves.
Life is beautiful, servants perform their tasks in parallel, but the king listens to only one result at a time and can focus on one result.
This is all great, but could you leave these stupid metaphors and tell everything like a geek?
Of course. A simple Node program might look like this:
var fs = require('fs'), sys = require('sys'); fs.readFile('treasure-chamber-report.txt', function(report) { sys.puts("oh, look at all my money: "+report); }); fs.writeFile('letter-to-princess.txt', '...', function() { sys.puts("can't wait to hear back from her!"); });
Your code gives Node two tasks: read and write the file, and then it goes to bed. Once the Node has completed the task, a
callback will be called. But only one callback function is called at a time. Until this function completes, all other callbacks are queuing and waiting. In addition, there are no guarantees in which order these functions will be called.
So I don’t have to worry about whether my code uses the same data structures at the same time?
Exactly! Here is the whole beauty of single-threaded / event loop JavaScript design!
This is all great, but why should I use it?
One of the reasons is efficiency. In a web application, typically, response time is the sum of the time it takes to complete all queries to the database. With the help of Node you can immediately all requests at once, it follows from this that the response time of the application will be reduced to the execution time of the longest query.
Another reason is javascript. You can use Node to exchange code between the browser and the server. Also, JavaScript is becoming a truly universal language. It doesn't matter what you wrote in Python, Ruby, Java, PHP ... in the past you probably wrote something in JS, right?
And the last reason is the speed of the code. V8 is constantly expanding and remains one of the fastest dynamic interpreters on the planet. I don’t even know about any other language that is as aggressive as JavaScript is now, speeding up. On top of that, Node's I / O tools are really lightweight, which allows you to use I / O on your system as quickly as possible.
So you say that I have to start writing all of my applications on Node, from now on?
Yes and no. As soon as you begin to rock the hammer Node, everything will obviously look like nails. But, if you are working on something with a deadline, you can base your decision on these things:
- Is low response time and concurrency important? Node is doing well with these things.
- How big is the project? For small ones, it will be great, but large projects should be qualitatively assessed for the availability of necessary libraries and other things.
Does Node work on Windows?
No, if you have Windows, then you need to put a virtual machine (I recommend VirtualBox) with Linux on board. Windows support is planned, but you should not hold your breath for several months if you do not want to help with porting.
Can I access the DOM in Node?
Great question! No, DOM is a browser thing, and Node's JS engine (V8) is, fortunately, separated from all this mess. However, there are people working on the Node’s implementation of the DOM as a module that can open up some very interesting features, such as unit testing of client-side code.
Isn't event-oriented programming too complicated?
Everything depends on you. If you have already learned how to juggle AJAX with requests and user events in the browser, then getting used to Node will not be difficult.
In any case, TDD can really help you with project support.
Who uses it?
There is a small / incomplete list on the
Node wiki (see “Companies using Node”).
Yahoo experiments with Node for YUI, Plurk uses it for a massive comet, Paul Bicaz (the creator of JQuery) makes a
mind-blowing game engine that uses a bit of Node on the server.
Joyent hired Ryan Dahl (the creator of Node) and is actively sponsoring the project.
Oh yes. Heroku has just announced (experimental)
support for hosting Node projects .
Where can I find out more?
Tim Caswell has launched a
great Node blog . Read
#nodejs on Twitter. Subscribe to the
newsletter . And then come to the # node.js IRC channel (yes, the dot in the name). There will be 200 of us there soon.
I also continue to write articles here at
debuggable.com .
This is a translation of the
article .
Thank you for your attention and invite.