Understanding node. js

Source: Internet
Author: User
Tags windows support

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.

"Huh?"

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 '   = require (   ' Treasure-chamber-report.txt ' ,    +report);}); Fs.writefile (  ' Letter-to-princess.txt ' , function   () {sys.puts (   

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!

--fg

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

Transferred from: HTTP://DEBUGGABLE.COM/POSTS/UNDERSTANDING-NODE-JS:4BD98440-45E4-4A9A-8EF7-0F7ECBDD56CB

Understanding node. js

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.