Why Nodejs choose JavaScript as the carrier language _node.js

Source: Internet
Author: User

Let's take a look at the introduction of the Nodejs official web site:

Its characteristics are:

1, it is a JavaScript running environment

2, relies on the chrome V8 engine for code interpretation

3, event-driven

4, non-blocking I/O

5, lightweight, scalable, suitable for real-time data interactive applications

6, single process, single-threaded

(1), why Nodejs choose JavaScript as carrier language

In fact, at the beginning of the implementation of Node.js, the author Ryan Dahl did not choose JavaScript, he tried C, Lua, because it lacks some advanced language features, such as closures, functional programming, resulting in complex procedures, difficult to maintain.

JavaScript, however, is a language that supports functional programming paradigms, well suited to node.js event-driven programming models. Combined with Google's V8 engine, the JavaScript language is being executed much more quickly.

What is finally presented to us is the node.js, not the Node.c,node.lua or the realization of other languages.

(2), Node.js is not JS application, but JS operating platform

See node.js This name, beginners may mistakenly think that this is a JavaScript application, in fact, Node.js is written in C + + language, is a JavaScript running environment.

Node.js uses the Google Chrome browser's V8 engine, which performs well and provides a wide range of system-level APIs, such as file manipulation, network programming, and more.

The following are all the modules covered by Nodejs:

    

Browser-side JavaScript code is run with a variety of security restrictions and limited operation on the client system.

Node.js, by contrast, is a comprehensive background runtime that provides JavaScript with many of the features that other languages can achieve.

(3), the characteristics of node.js

Node.js is also more innovative in design, with a single process, single-threaded mode (which is consistent with the way JavaScript works),

The event-driven mechanism is node.js through an internal single-threaded, efficient maintenance of event-loop queues, with no multi-threaded resource consumption and context switching, which means that in the face of massive HTTP requests, Node.js takes care of everything by event-driven,

Web service developers who are accustomed to traditional languages may be familiar with multithreading concurrency and collaboration, but in the face of node.js we need to accept and understand its characteristics.

Second, important concepts

1, what is Event Loop? (Very important concept)

Event Loop is a very important concept, referring to the operating mechanism of a computer system.

To understand the event Loop, start with the running mode of the program. Running programs are called processes (process), and in general, a process can only perform one task at a time.

If there are many tasks that need to be performed, there are only three ways to solve them.

(1), line up. Because a process can only perform one task at a time, wait until the previous task is finished, and then perform the following tasks.

(2) To create a new process. Use the fork command to create a new process for each task.

(3), new thread. Because processes are too resource intensive, today's programs often allow a process to contain more than one thread to complete a task by a thread.

The JavaScript language, for example, is a single-threaded language in which all tasks are done on a single thread, using the first method above. Once you encounter a large number of tasks or encounter a time-consuming task, the page will appear "suspended animation", because JavaScript can not stop, you can not respond to user behavior.

You may ask, why is JavaScript a single thread, can't be implemented as multithreading?

This is related to history:

One of the main features of JavaScript language is single-threaded, that is, one thing at a time. So why can't javascript have multiple threads? This will improve efficiency.

A single thread of JavaScript, which is related to its purpose. As a browser scripting language, the main purpose of JavaScript is to interact with the user and manipulate the DOM. This determines that it can only be single-threaded, or it can create complex synchronization problems.

For example, assuming that JavaScript has two threads at the same time, one thread adds content to one of the DOM nodes, and another thread deletes the node, which thread should the browser use?

Therefore, in order to avoid complexity, from the birth, JavaScript is a single thread, which has become the core characteristics of the language, will not change in the future.

To take advantage of the computational power of multi-core CPUs, HTML5 proposes web worker standards that allow JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and must not manipulate the DOM.
So, the new standard doesn't change the nature of JavaScript single-threaded threads.

Back to EventLoop:

A single thread means that all tasks need to be queued and the previous task is completed before the latter task is performed. If the previous task takes a long time, the latter task will have to wait all the time.

If the queue is because of the large amount of calculation, CPU busy, but also forget, but many times the CPU is idle, because the IO device (input) is very slow (such as AJAX operations from the network to read data), had to wait for the results out, and then down.

The designer of the JavaScript language realizes that the main thread can completely ignore the IO device, suspend the task in wait, and run the task in the back first. Wait until the IO device returns the result, and then go back to the pending task.

As a result, all tasks can be divided into two types, one is the synchronization task (synchronous) and the other is the asynchronous task (asynchronous). A synchronization task is a task that is queued on the main thread, and only the previous task executes.

To perform the latter task; an asynchronous task is a task that does not go into the main thread and goes into the task queue, only the task queue notifies the main thread, and an asynchronous task can be executed before the task enters the main thread execution.

The following figure:

        

As long as the main line Cheng, it will read "task queue", which is the operating mechanism of JavaScript. This process will continue to repeat itself.

Third, the example explanation

OK, "nonsense" said no more, immediately began our first Nodejs application: "Hello Big Bear".

Open your favorite editor and create a helloworld.js file.

The code is as follows:

Copy Code code as follows:
1 var http = require ("http"); 2 http.createserver (function (request,response) {3 Response.writehead (200,{4) Content-type ":" Text/plain "5}) 6 Response.Write (" Hello, Big Bear! "); 7 Response.End (); 8}). Listen (8888);

Let's run and test this piece of code. First, execute your script with Node.js:

Open command line tool cmd, switch to your working directory, Run Command "node Helloworld.js"

Next, open the browser to visit http://localhost:8888/, you will see a "Hello, Big Bear!" page.

A little bit of extended knowledge:

As shown in the following figure, This is the Nodejs in the http.js part of the source code, Createserver is a user-friendly interface, the internal implementation of a single example mode, the advantage is that the creation and initialization of the instance of the task of effective separation, responsibility specificity, reduce coupling, this is the normal programming can be used to learn from the idea.

  

hahaha, is not very interesting, this is just a brief experience, after will explain a lot of knowledge points, we slowly realize O (∩_∩) o haha ~

Iv. General overview

1, it is a JavaScript running environment

2, relies on the chrome V8 engine for code interpretation

3, event-driven

4, non-blocking I/O

5, lightweight, scalable, suitable for real-time data interactive applications

6, single process, single-threaded

Finally, I would like to say: This is not a lot of examples, but these concepts are very important, must have a clear understanding, so that the future Nodejs learning will lay a solid foundation, friends to refuel together.

hahaha, the end of this article, not to be continued, I hope and everyone a lot of communication enough to make progress together ... Shout out ... (*^__^*)

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.