Node. JS Introductory Series (i)--node.js introduction

Source: Internet
Author: User

What is Nodejs

JS is a scripting language, and the scripting language requires a parser to run. For JS written in the HTML page, the browser acts as the parser's role. For a js,nodejs that needs to run independently is a parser.

Each parser is a running environment that not only allows JS to define various data structures, perform various calculations, but also allows JS to do something with the built-in objects and methods provided by the running environment. For example, the purpose of JS running in the browser is to manipulate the DOM, and the browser provides the document built-in objects. While the use of JS running in Nodejs is to manipulate disk files or build an HTTP server, Nodejs provides, and so on, the fs http built-in objects.

node. JS is a development platform that lets JavaScript run on the server side, which extends the tentacles of JavaScript to the server side, with PHP, JSP, Python, Ruby, JAVA ... Equal.

But node seems a little different:

node. JS is not a standalone language, unlike PHP, JSP, Python, Perl, and Ruby's "both language and platform", node. JS is programmed with JavaScript to run on the JavaScript engine (V8).

compared to PHP, JSP, and so on (PHP, JSP,. NET need to run on the server program, Apache, Naginx, Tomcat, IIS. node. JS skips HTTP servers such as Apache, Naginx, IIS, and so on, without building any server software on its own.

Many of the design concepts of node. js are very different from the classic architecture (LAMP = Linux + Apache + MySQL + PHP) and can provide great scalability. In a moment we'll see that node. js does not have a Web container.

node. JS's own philosophy is to spend the smallest hardware costs, pursuing higher concurrency and higher processing performance.

Nodejs Features

The so-called feature is how node. js solves the server's high-performance bottleneck problem.

Single Thread

In a server-side language such as Java, PHP, or. NET, a new thread is created for each client connection. Each thread consumes about 2MB of memory. That is, theoretically, a server with 8GB of memory can connect at the same time to a maximum of 4,000 users. For a Web application to support more users, you need to increase the number of servers, and the cost of hardware for Web applications is of course rising.

node. JS does not create a new thread for each client connection, but only uses one thread. When a user is connected, an internal event is triggered, with a non-blocking I/O, event-driven mechanism that makes the node. JS Program Macro-parallel. With node. js, a 8GB memory server that can handle connections with more than 40,000 users at the same time.

In addition, with the benefits of threading, there is no way the operating system is completely no longer wired to create, destroy the time overhead.

The downside is that a user has caused the thread to crash, the entire service has collapsed, and others have collapsed.

A comparison of multithreading, single threads.

In other words, a single thread can also cause macro "concurrency".

Non-blocking I/O asynchronous I/O

For example, it can take a while to access the database to get data. In the traditional single-threaded processing mechanism, after the Access database code is executed, the entire thread will be paused, waiting for the database to return the results before executing the code. in other words, I/O The execution of the code is blocked, which greatly reduces the execution efficiency of the program.

The capture of the result value in the JS asynchronous call is in accordance with the principle of don ' t calling Me,i will,

The picture is the classic Ajax

asynchronous I/O is also common in node, very similar to the way Ajax is called. Because the non-blocking I/O mechanism is used in node. js, after executing the code that accesses the database, the code behind it is immediately executed, and the processing code of the database return result is placed in the callback function, which improves the execution efficiency of the program.

When an I/O execution is complete, the thread executing the I/O operation is notified as an event, and the thread executes the callback function for the event. In order to handle asynchronous I/O, threads must have an event loop, constantly checking for unhandled events, and sequentially processing them.

In blocking mode, a thread can only handle one task, and the throughput must be multithreaded if it is to be improved. instead of blocking mode, a thread is always performing a compute operation, and the thread's CPU core utilization is always 100% . So, this is a particularly philosophical solution: with more people, but a lot of people idle, rather than a person's life, to die to work.

Event-driven Event-driven

In node, the client requests to establish a connection, commit data, and so on, triggering the corresponding event. In node, only one event callback function can be executed at a time, but in the middle of executing an event callback function, it is possible to handle other events (for example, a new user connection) and then return to the callback function that continues the original event, which is called the "event loop" mechanism.

The underlying node. JS is C + + (V8 is also written in C + +). nearly half of the underlying code is used for event queuing, callback function queue construction. event-Driven to complete the server's task scheduling, this is a genius can think of. The dance on the tip, with a thread, takes on a mission to handle very many tasks.

single -threaded, single-threaded benefits reduce memory overhead and the operating system's memory is paged out.

If something comes in, but is blocked by I/O, the thread is blocked.

non-blocking I/Odoes not fool the end of the I/O statement, but executes the subsequent statement.

is non-blocking going to solve the problem? such as the implementation of the small red business, the implementation process, the small just I/O callback is complete, what to do??

Event mechanism, event ring , whether it is a new user's request, or the old user's I/O completion, will be an event to join the event ring, waiting for dispatch.

Say is three characteristics, is actually a feature, leave no one, all play not turn.

node. JS is like a stingy restaurant owner, employing only 1 waiters and serving a lot of people. As a result, it is more efficient than many waiters.

All the I/O in node. js are asynchronous, callback functions, and set of callback functions.

What's the use?

Despite the existence of a heard can directly run JS file to feel very cool classmate, but most students in contact with new things the first concern is what is the use, and can bring what value.

Nodejs's author says that his goal of creating nodejs is to achieve a high-performance Web server, and he first values the superiority of the event mechanism and asynchronous IO model, not JS. But he needs to choose a programming language to implement his idea that the programming language does not have IO capabilities and needs to be able to support the event mechanism well. JS does not have the IO feature, is inherently used to handle DOM events in the browser, and has a large group of programmers, so it becomes a natural choice.

when an application needs to handle large amounts of concurrent I/O , and when the application does not need to perform very complex processing in response to the client, node. js very suitable. node. JS is also ideal for interfacing with Web sockets to develop long-connected real-time interactive applications.

Like what:

User Form Collection

Test system

Chat room

Graphic Live

As he wishes, Nodejs is active on the service side, and a large number of Nodejs-based Web services appear. On the other hand, Nodejs let the front end as the artifact, and finally let their ability to cover the range out of the browser window, a large number of front-end tools such as springing up.

Therefore, for the front end, although not everyone has to take Nodejs to write a server program, but simple to use the command interactive mode debugging JS code snippets, complex to write tools to improve productivity.

The Nodejs ecological circle is thriving.

Node. JS Introductory Series (i)--node.js introduction

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.