Understanding node. JS (Translate)

Source: Internet
Author: User
Tags readfile

Body

When I introduce someone to node. js, there are usually two kinds of reactions, either immediately figuring out what it is, or being confused by it.

If you're still in the latter, here's my node explanation:

    • It is a command line tool, you can download a tarball file, compile and then install the source file;

    • It allows you node my_app.js to run JavaScript programs at your terminal input;

    • Node's JS code is executed by the V8 JavaScript engine (which is what makes chrome so fast);

    • Node provides information such as access to the network or operation of the file system.Javascript API

"But I can also use Ruby, Python, Php,java, ... and the language to do what I want to do.

I heard what you said, you were right. Nodenot a cunning unicorn, I'm sorry, it won't help you do what you have to do. It's just a tool, and he won't replace some of the tools you're used to now, at least not yet.

"Say the point!!! "

OK, I will, when you need to do a few things at the same time Node will be very good performance. You wrote a piece of code and said to him, "I think you can run it in parallel!" "The experience?" hahaha, everything in node except your code is running in parallel.

"Ah?! "

Yes, yes, all the code except your code runs in parallel. To understand this, you can think of your own code as a king, and it Node is his servant army.

The new day begins like this: a servant wakes up the king and asks him if he needs anything. The king gave the servant a list of tasks and went back to sleep. The servant then distributed the tasks on the task list, and the servants began to work.

When a servant finished his task, he ran out of the king's Chambers to wait for the report. The king can only hear one servant at a time, and sometimes the king will give him more tasks at the end of the servant's report. (See your code to write it)

Life is good, because many of the king's servants perform multiple tasks simultaneously, but when the results are reported one by one, the king is able to concentrate.

"That's really nice, but can you end this stupid metaphor and tell me in a more geek way?" "

Okay, a node program may be the following:

var  Fs = require (' FS ')   , sys = require (' sys ');//Translator Note:sys is  Deprecated. use util instead. Here we use Console.log directly to Fs.readfile (' Treasure-chamber-report.txt ',  function (report)  {  //sys.puts ("oh, look at all my money: " +report);   console.log ("oh, look at all my money: " +report)}); Fs.writefile (' letter-to-princess.txt ',  ' ... ',  function ()  {  //sys.puts ("Can ' t  Wait to hear back from her! ");   console.log ("Can ' t wait to hear back from her!")}); 

Your code (the king) gave node (the servant) two tasks to read (ReadFile) and write (WriteFile) files, and then went to sleep. Once node has completed a task, the callback corresponding to the task is triggered. But only one callback is triggered at the same time, and all other callbacks are queued until the callback executes. Further, the order in which the callback is triggered cannot be guaranteed.

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

You do understand that this is a beautiful place for JavaScript's single-process/event loop design.

"Great, but why should I use it?" "

One reason is efficiency. In a Web application, the response time is spent primarily on executing database queries, and using node , you can execute all database queries at once. Reduces response time to the time it takes to perform the slowest database query.

javascript . You can use node let your browser and backend share the code. JavaScript is also becoming a real universal language. No matter what you used to be in Python, Ruby, Java, PHP, ... Wait, you've been using javasctipt more or less, right?

The last reason is the native speed. V8 is constantly pushing the boundaries of one of the fastest dynamic language compilers on the planet, and I don't think any other language will ever be as fast as JavaScript. Further, node the I/O devices are really lightweight, allowing you to maximize the system's I/O capacity.

"So you're saying that from now on I should use node to write all my apps?" "

No, and once you start yellow orchids x node The Hammer, everything will start to look like a nail. But if your current job has a deadline, you can refer to the following points to make a decision without node :

    • is low response time/high concurrency important? Node is really good at dealing with these two problems;

    • How big is the project? Small project is not a problem, if it is a large project should be carefully evaluated (available libraries, repair a bug required resources, or both upstream, etc.)

"Can I access the DOM in node?" "

This is a good question! The answer is no, Dom is a browser thing, but fortunately node's JS engine (V8) is completely separate from the chaotic stuff. However, the implementation of the DOM in the form of a node module may bring exciting possibilities such as unit testing of client code. (Translator Note: Now someone has implemented this module, see Node-dom for details).

"Is event-driven programming really hard?" "

It's up to you, and if you've learned how to invoke Ajax in a browser or invoke an event, then learning node won't be a problem for you.

At the same time, test-driven development can really help you start learning node from a maintainable design.

"Where should I learn more?" "

Tim Caswell is running an excellent how-to node blog. Follow Nodejs on Twitter. Subscribe to Mailing lists. (Translator Note: can also be combined with the node. JS 6.9.5 document to learn, in addition, the translator wrote a small application of node Node-sample can be seen under the clone)

Postscript

This article is a bit simple to say, but it is too difficult to find the real thing that corresponds to it. , in addition, due to time, some irregularities in this article or at that time was still in the experimental phase of the translator or deleted or changed. The ability is limited, the level is general, the translation is inappropriate, also looks correct. Thank.


Understanding node. JS (Translate)

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.