About node. JS Getting Started

Source: Internet
Author: User

About node. js

1.1 Introduction

The V8 engine itself is part of the JS interpretation for Chrome, but Ryan Dahl the V8 to the server for the server software.

node. JS is a focus on achieving high-performance Web server optimization experts, after several exploration, after setbacks, encountered V8 and the birth of the project.

node. JS is a development platform that allows JavaScript to run on the server side, which extends the tentacles of JavaScript to the server side, on an equal footing with PHP, JSP, Python, and Ruby.

But node seems a little different:

node. js not a standalone language , unlike PHP, JSP, Python, Ruby's "both language and platform", node. JS is programmed with JavaScript , 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 Apache,naginx,IIS , etc. HTTP Server, which does not build itself on any server software . 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. Also, 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.

1.2 Features

node. JS uses a model of event-driven, non-blocking I/O that makes him lightweight and efficient.

① 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 non-blocking 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.

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.

The advantages of NODEJS based on these features:

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

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

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.

Nodejs These three features are mutually reinforcing, and single threading reduces the overhead of memory, which is equivalent to reducing hardware costs. At the same time, in order to adapt to the characteristics of single-threaded, there is a non-blocking I/O mechanism, so a single thread can handle multiple I/O. Another problem arises when one I/O is executing and another I/O callback is done, so with event-driven, the event mechanism is that all I/O is queued in the event queue, and when an I/O callback completes, it will continue to return to the queue to avoid conflicts.

Nodejs's drawbacks are also very obvious, that is, it has only one thread, when this thread crashes, the entire server crashes, but I believe that with the development of technology and the improvement of computer hardware level Nodejs will become more and more popular

1.3 What is suitable for development?

What kind of application does node. js Fit to develop?

Good at I/O, not good at calculation. Because node. js is best at scheduling tasks, and if the business has a lot of CPU compute, it's actually the equivalent of blocking this single thread for node development.

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 developing long-connected, real-time interactive applications with the web socket"1".

Like what:

User Form Collection

Test system

Chat room

Graphic Live

APIs that provide JSON

"1" WebSocket is a very important new feature in the HTML5 specification, its existence can allow users to implement two-way communication in the browser, to achieve timely data push, the significance of this technology is that we can use the web to achieve a lot of previously impossible applications. For example, the current online chat room, if the user wants to send a word, when he presses the "Send" button, the browser will be delivered to the target server a request, and then the server side will display this sentence on the page. And if the user wants to see this, he needs to refresh his browser to get the latest page. But WebSocket's two-way communication is magical, the HTML5 application built through WebSocket technology, users no longer need to refresh the page frequently to get new data, and the new data is automatically pushed from the server to the user's computer screen.

Summarize:

Nodejs is the pinnacle of the pursuit of extreme performance products, server robust considerations, so not suitable for the development of securities, banking, telecommunications and other high reliability requirements of the business.

The current Nodejs in the actual combat use:

General startup companies use node to develop core business

Large companies generally use only nodejs to develop certain parts of the business, such as Baidu's form submission, the station letter, etc.

About node. JS Getting Started

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.