What is node. js

Source: Internet
Author: User

When I introduce you to node. js, there are generally two reactions: most immediately say, "Oh, that's it," and the other one will be confused.

If you're the second, I'll try to explain node:

    • This is a command-line tool. You can download a tar package and then compile the installation.
    • Enter "Node My_app.js" in terminal to get your JavaScript code running.
    • JS is running in the V8 engine (which is why Google Chrome is so fast).
    • Node provides an API for accessing files and file systems.

"But I can use Ruby, Python, PHP, Java ... Deal with anything! ”

Yes, you're right. node is so unique. And node is just a tool, not a substitute for your regular tools, at least not yet.

"Go to Theme"

Node is basically a good choice if you're going to be working on multiple things with your coworkers. Have you ever written a bunch of code, and then you say, "Want this code to execute in parallel"? Well, the code in node is executed in parallel, except for your code.

Watt ”

Yes, except that your code is executed in parallel. You can think of your code as a king, and node is a bunch of valet.

At the beginning of each day, a small valet wakes up the king and asks what he needs. The king gave the valet a list of tasks and went to sleep. The valet assigned the task to the other valet, and then they began to work.

Whenever a valet completes a task, he stands outside the king's bedroom waiting to be reported. The king had only one valet come in to report to him at a time. Sometimes, the king will give a valet more tasks to deal with.

Life is beautiful, because the king's valet performs his assigned tasks in parallel, but each time only one of the valet who completes the task can report it. So that the king can concentrate his attention.

"It's great, but can you put down your metaphor and call for work?" ”

Of course, here's a simple node code:

var fs = require (' FS ')  = require (' sys '); Fs.readfile (function(report) {  sys.puts ("Oh, look at all my money:" + report);}); Fs.writefile (function() {  sys.puts ("can ' t wait for hear back from her!" );});

This code is to read and write a file and then hibernate. Each time node completes a task, the callback is triggered. But only one callback can be triggered at a time until the callback executes. All other callbacks need to wait. In addition, the order in which callbacks are executed is not guaranteed to be the same as the order in which callbacks are invoked.

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

Yes, that's the charm of node. JS's single-threaded, time-loop.

"Very well, but why should I use it?" ”

The first reason is efficiency. In a Web application, your primary time consumption is in database queries. With node, you can execute all of your queries at the same time, which can be compressed to return only when the slowest query is consumed.

Another reason is JavaScript. You can share the front and back code with node. JavaScript is already on the way to becoming a full-site language. Whether you've used Python, Ruby, PHP, or Java, you'll have more or less access to JavaScript in development.

The last reason is speed. V8 is not the fastest dynamic language interpreter on Earth and is very close. I really can't think of any language that can be as bold as JavaScript to speed up execution. Also, node's I/O components are lightweight and can best perform the I/O capabilities of your system.

"So you're saying I should use node to develop my web app?" ”

No, it's not. node. JS is clearly capable of any work you need to accomplish. But if there's deadline, you need to think about something:

    • High concurrency, low return time is necessary. This is node's area of expertise.
    • How big is the project? Small project Fortunately, the big project will need to consider the available libraries, modify the bug's resources and so on.

"Can node run on Windows?" ”

Yes. Versions after 0.6 can be run on Windows. You don't want to use the old version of node anymore.

"Can I access the HTML DOM in node?" ”

Good question. But you can't. Dom is something of a browser. Node's JS engine (V8) and those things are independent of each other. But someone is trying to develop such a module.

"Is event-driven becoming difficult?" ”

It depends on you. If you've already used Ajax calls to handle user events in your browser, that's not going to be a problem.

In any case, test-driven development allows you to form a maintainable design.

"Who's using it?" ”

You can refer to node's "Company using Node" list. A lot of companies are already in use.

"Where to study next?" ”

Tim Caswell's blog is good.

All right, here we go.

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

Welcome Dabigatran to learn from each other and make progress together. QQ Group: 58099570 | To be kind, reprint please indicate the source!

What is 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.