📜 ⬆️ ⬇️

Understanding node.js

Node.js usually caused two different reactions to the people I told about it. Basically, they perceived it "as is", but many were confused.

If you belong to the second group, I will try to explain what Node.js is:

“But I can do what I need with Python, Ruby, Java, PHP, ...!”

Yes, I already heard it. And you are right! Node is not a magical unicorn that will come and do everything for you. It's just a tool, and it probably won't be able to replace everything else, at least not now.
"Get to the point!"

Good. Node is well applicable when you need to do several things at the same time. Have you ever written another piece of code, said: "I would like it to work in parallel?" So in Node, everything runs in parallel, with the exception of your code.


That's right, everything runs in parallel, with the exception of your code . To understand this, imagine that your code is king, and Node is his servant.

Every day begins with the fact that one servant awakens the king and asks if he needs anything. The king gives the servant a list of tasks and goes to sleep. The servant distributes these tasks among all the servants, and they go to work.

As soon as the servant completes a task, he comes to the king and reports. The king simultaneously receives only one servant. Sometimes after the report, the king gives the servant other tasks.

Servants perform tasks at the same time, only report the results one by one, so that the king can concentrate. *

“This is great, but could you not speak in metaphors?”

Of course. A simple program for Node is as follows:

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!"); }); 

This code gives Node 2 tasks to read and write files, and then go to sleep. When the Node completes the task, the callback function will be called. But only one such function can be executed at a time, so that until the end of its execution, the rest will wait for their turn. In addition, there is no guarantee in which order the callback functions will be performed for these tasks.

“So you don't have to worry about simultaneous access to data from different functions?”

You got it! Yes, this is a great consequence of the single-threaded event loop design in JavaScripts!

“Very good, but why should I use it?”

One of the reasons is efficiency. In a web application, your response time is usually basically the sum of the time it takes to complete all of your database queries. Using Node you can fulfill all your requests at once, reducing the response time to the duration of the slowest request.

Another reason is the use of javascript. Using Node, you can reuse part of the code on the server and client side. JavaScript evolves toward becoming a truly universal language. It doesn't matter if you used to use Python, Ruby, Java, PHP, ..., but you probably also used JavaScript, didn't you?

And the last reason is speed. V8 is constantly expanding the boundaries of our understanding of the speed of execution of dynamic languages. I can't remember any other language that would get faster as aggressively as JavaScript currently does. In addition, the I / O operations in Node are really well optimized and allow you to tap the potential of the system as much as possible.

“So you say, should I write my applications on Node now?”

Yes and no. Once you learn how to use Node as well as a hammer, all tasks will begin to seem like nails. But if you are working on something with specific deadlines, you can choose (or not select) a Node by answering the following questions:

"Does Node work on Windows?"

Not. If you are using Windows, you need a virtual machine (I recommend VirtualBox ) with Linux. Windows support is planned, but you should not hold your breath for the next several months while waiting, better help with porting ( note: at the moment Node should work in Windows + Cygwin).

“Can I access the DOM in a Node?”

Great question! No, the DOM is defined in the browser separately from the JavaScript engine (V8). However, there are people working on implementing the DOM as a Node module , which can open up some very interesting features, such as testing client code with Node.

“Is programming in languages ​​with events difficult?”

It depends on you. If you have already learned how to juggle AJAX calls and user events in a browser, there should not be a problem and get used to Node.

In any case, TDD can greatly help you create easily serviced applications.

"Who already uses Node?"

An incomplete list is on the wiki (scroll to "Companies using Node"). Yahoo is experimenting with Node and YUI, Plurk uses it for a large-scale Comet server, and Paul Bakaus (from the jQuery UI team) writes a mind-blowing game engine that uses Node on the backend. Joyent hired the author Node and largely sponsors its development.

Oh yeah, Heroku just announced (experimental) support for hosting Node apps .

"Where can I find out more?"

Tim Caswell opened the wonderful blog How To Node . Follow #nodejs on Twitter. Subscribe to the newsletter . And come to IRC, # node.js (yes, there’s a dot in the name).

I will also write about Node on the debuggable.com blog.

It's all for today. Feel free to comment if you have questions!

- Felix Geisendörfer

*: I, obviously, exaggerate, but to find an analogue for the concept of non-blocking code execution in reality is quite difficult.

Source: https://habr.com/ru/post/104171/

All Articles