node. JS Preliminary Understanding

Source: Internet
Author: User

Node. JS Features: single-threaded, non-blocking asynchronous I/O, event-driven .

First say I/O:i is input data read, O is the output data write , most web site I/O is very much, but I/O when the CPU is the command disk to do things, the CPU itself is idle.

Sync (synchronous): When the system meets a time-consuming thing, choose death.

Asynchronous ( Asynchronous): When the system meets a time-consuming thing, do not death, do the following things first, time-consuming things after the execution of the callback function.

node. JS is behind the scenes.

We write a procedure for example

// read the built-in module HTTP, which is used by the module development Server var http = require ("http"); // Create a server var server = Http.createserver (function(req,res) {
Res.setheader ("Content-type", "text/html;charset=utf-8"); Res.end ("}); // monitoring Server.listen (3000)

After writing, use the cmd command to invoke

In node. js, who's the node?

at this point the cursor hangs and does not return, indicating that node is performing a listener at this point.

Press CTRL + C to end the pending state.

Access to port 3000 to see the page.

We F12 View Source code

At this point we look at the source code, we found that the number 8 is directly written up, no 4+4 operation traces.

The reason is that Nodejs runs on the server side, not the browser side. After the server calculates the number 8 , it is sent to the browser that is flat, pure HTML , without any program.

Single Thread

Only one thread is used in node. js.

Asynchronous I/O characteristics

A brief summary is:

It takes a long time to access the database to get the data. In the traditional processing mechanism , after the Access database code is executed, the whole thread will pause and wait for the database to return the results before executing the code. In other words,I/O blocks the execution of the code, 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, it immediately executes the code that follows it, placing the processing code of the database return result in the callback function , thus improving the execution efficiency of the program.

Event-Driven

Event-Driven is NodeJS 's underlying mechanism, we just need to understand that the cause of NodeJS error is event-driven and has an event loop.

The event loop mechanism is the underlying mechanism of NodeJS, which ensures that the NodeJS can operate efficiently and accurately without disturbance.

What is node. js suitable for?

when an application needs to handle a large number of concurrent I/O, andnode. JS is ideal forapplications where there is no need for very complex processing within the application until a responseis made to the client . 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

Scoring System

Chat room

Graphic Live

node. JS Preliminary Understanding

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.