About node. js Introduction, Installation & Configuration

Source: Internet
Author: User

node. JS profile What is node. js

node. JS has a powerful and flexible Package Manager (node MANAGER,NPM)

At present, there are powerful third-party tool modules, such as database connection, Web development framework, CSS Generator, operating system API, network communication and so on.

What is node. js? is a platform for JavaScript to run on the server side, where JavaScript can only be run in the browser, and node. js resolves JavaScript.

CommonJS tried to design a set of JavaScript specifications.

The engine for node. JS is Google's V8 engine.

node. JS does not run in the browser, so there is no JavaScript browser compatibility issue, and you can safely use all the features of the JavaScript language.

What can node. js do

node. JS has built-in HTTP server support, which means you can easily implement a combination of Web sites and servers. This is not the same as PHP, Perl, because when using PHP,

An HTTP server such as Apache must be built before the execution of the PHP script can be presented to the user via the HTTP server's module load or CGI call.

Add: HTTP server, HTTP is the network Transport protocol, the HTTP server simple point is to return to the client page of the server.

asynchronous I/O and event-driven

node. JS maintains an event queue during execution, and the program enters the event loop at execution time waiting for the next event to arrive.
After each asynchronous I/O request is completed, it is pushed to the event queue for processing by the program process.

For example, for simple and common database query operations, the code implemented in the traditional way is as follows:

res = db.query (' SELECT * from some_table ');

Res.output ();

When the above code executes to the first line, the thread blocks, waits for the database to return the query results, and then continues processing. However, because database queries may involve disk read-write and network traffic,

The delay can be quite large (up to several to hundreds of milliseconds, and several orders of magnitude compared to the CPU clock), where the thread blocks waiting for the result to return.

See how node. js solves this problem:

Db.query (' SELECT * from some_table ', function (res) {

Res.output ();

});

The second parameter of Db.query in this code is a function, which we call a callback function . The process does not wait for the result to return when it executes to db.query.

Instead, proceed directly to the following statement until you enter the event loop. When the results of a database query are returned, the event is sent to the event queue until the thread enters the event loop before it is tuned

Use the previous callback function to continue with the subsequent logic.

the asynchronous mechanism of node. JS is event-based . In the example above, it is an example of asynchronous execution. All disk I/O, network traffic, database queries are requested in a non-blocking manner,

The returned result is handled by the event loop. The node. JS process processes only one event at a time, and immediately enters the event loop to check for and handle subsequent events. The advantage of doing this is that

CPU and memory focus on one thing at a time, while simultaneously executing the time-consuming I/O operations as much as possible.

The drawbacks of this asynchronous event pattern are also obvious, as it does not conform to the usual linear approach of the developer, often requiring a complete logic split into events, which increases the difficulty of development and debugging.

node. JS Performance

CommonJS

CommonJS attempts to define a set of APIs used by ordinary applications to fill the JavaScript standard library with too simplistic a deficiency.

Install & Configure node. js

Take windows as an example: Download node. js directly

http://nodejs.cn/

node. js, installed in this way, also comes with NPM automatically, and we can use it directly at the command prompt by entering NPM.

Note: Eclipse node plugin : http://www.nodeclipse.org/updates/

About node. js Introduction, Installation & Configuration

Related Article

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.