First, the opening analysis
Hello everybody, Big Bear is coming again (*^__^*) hehe ..., I wrote a series of articles about JS (OOP and design mode) , the response is OK, in fact, this is also the biggest encouragement to me, so I decided I want to carry out JavaScript to the end
Prepare to write a series of nodejs aspects of the article, a gradual, progressive, adhering to the idea is to re-thinking, more practice, Qinnengbuzhuo, your insistence.
Let's take a look at the introduction of the NODEJS official website:
Its features are:
1, it is a JavaScript runtime environment
2, rely 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 thread
(1), Nodejs Why choose JavaScript as the carrier language
In fact, at the beginning of the implementation of node. js, the author Ryan Dahl did not choose JavaScript, and he tried C, Lua, because of its lack of some high-level language features, such as closures, functional programming, resulting in complicated procedures, difficult to maintain.
JavaScript is a language that supports the functional programming paradigm, which fits well with the node. JS Event-driven programming model. With the V8 engine provided by Google, the JavaScript language is executed much more quickly.
The final presentation in front of us becomes node. js, not the implementation of Node.c,node.lua or other languages.
(2), node. js is not JS application, but JS run platform
When you see the name node. js, a beginner may mistakenly assume that this is a JavaScript application, in fact, node. JS is written in the C + + language and is a JavaScript operating environment.
node. JS uses Google Chrome's V8 engine for good performance while also providing many 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 subject to a variety of security restrictions at runtime, with limited operation to the client system.
In contrast, node. JS is a comprehensive background runtime that provides JavaScript with many features that other languages can implement.
(3), features of node. JS
node. JS is also more innovative in design, running in single-process, single-threaded mode (which is consistent with how JavaScript works),
The event-driven mechanism is implemented by node. JS through an internal single thread that maintains an event loop efficiently, with no multi-threaded resource usage and context switching, which means that node. js is doing everything with event-driven response to large-scale HTTP requests.
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.
Two, the important concept
1, what is Event Loop? (very important concept)
Event Loop is a very important concept, which refers to a running mechanism of a computer system.
To understand the event Loop, you should start with the program's operating mode. A program that runs later is called a process, and typically a process can perform only one task at a time.
If there are many tasks that need to be performed, there are three ways to solve them.
(1), queued. Because a process can only perform one task at a time, you have to wait for the previous task to finish and then perform the subsequent tasks.
(2), create a new process. Use the fork command to create a new process for each task.
(3), create a new thread. Because processes are too resource-intensive, today's programs often allow a process to contain multiple threads, which are performed by threads.
In JavaScript, for example, it is a single-threaded language, and all tasks are done on one thread, using the first method above. Once a large number of tasks are encountered or a time-consuming task is encountered, the webpage will appear "suspended animation", because JavaScript can not stop, will not be able to respond to user behavior.
You might ask, why is JavaScript a single thread, can't it be multithreaded?
This is related to history:
One of the main features of the JavaScript language is single-threaded, meaning that only one thing can be done at the same time. So why can't javascript have multiple threads? This can improve the efficiency AH.
The single thread of JavaScript is related to its purpose. As a browser scripting language, JavaScript's primary purpose is to interact with the user and manipulate the DOM. This determines that it can only be single-threaded, otherwise it can lead to 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 the other thread deletes the node, which thread should the browser take precedence over?
So, to avoid complexity, JavaScript is a single thread from birth, which has become a core feature of the language and will not change in the future.
To take advantage of the computational power of multicore CPUs, HTML5 proposes a web worker standard that allows JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and must not manipulate the DOM.
So, this new standard does not change the nature of JavaScript single threading.
Back to EventLoop:
A single thread means that all tasks need to be queued, and the previous task is completed before the latter one is executed. If the previous task takes a long time, the latter task has to wait.
If the queue is because of large computational capacity, the CPU is not busy, but also forget, but many times the CPU is idle, because the IO device (input) is very slow (such as the Ajax operation from the network to read data), have to wait for the results, and then down to execute.
The designer of the JavaScript language realizes that at this point the main thread can completely ignore the IO device, suspend the waiting task, and first run the task that is in the queue. Wait until the IO device returns the result, and then go back and put the suspended task on hold.
Thus, all tasks can be divided into two types, one synchronization task (synchronous) and one asynchronous task (asynchronous). A synchronization task is a task that is queued for execution on the main thread, and only the previous task has finished executing it.
To perform the latter task, the asynchronous task refers to a task that goes into the task queue without entering the main thread, and only the task queue notifies the main thread that an asynchronous task can execute and the task goes into the main thread execution.
Such as:
As long as the main line Cheng, will read "task queue", this is the operation mechanism of JavaScript. This process is repeated.
Third, the example explains
OK, "nonsense" not much to say, immediately began our first Nodejs application: "Hello Big Bear".
Open your favorite editor and create a helloworld.js file.
The code is 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 the command "node Helloworld.js"
Next, open the browser to access http://localhost:8888/ , and you'll see a page that says "Hello, Big Bear!".
A little bit of extension knowledge:
As shown, This is nodejs in the http.js part of the source code, Createserver is a user friendly interface, the internal implementation of the Singleton model, the advantage is that the creation and initialization of the instance of effective separation, responsibility-specificity, reduce coupling, this is the idea that we can use for reference in the normal programming.
hahaha, is not very interesting, this is only a short experience, the following will be explained a lot of knowledge points, we slowly realize O (∩_∩) o haha ~
Iv. General overview
1, it is a JavaScript runtime environment
2, rely 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 thread
The last thing I want to say is: This is not a lot of examples, but these concepts are very important, we must have a clear understanding, so that the future of Nodejs learning will lay a solid foundation, friends refueling together efforts.
hahaha, the end of this article, not to be continued, hope and we have enough communication, common progress ... To shout ... (*^__^*)
Big Bear NodeJS's opening------Why NodeJS (JavaScript to the end)