Let’s first take a look at the introduction on the official website of NodeJS:
Its characteristics are:
1. It is a Javascript running environment
2. Rely on Chrome V8 engine for code explanation
3. Event-driven
4. Non-blocking I/O
5. Lightweight, scalable, suitable for real-time data interaction applications
6. Single process, single thread
(1), Why does Nodejs choose javascript as the carrier language
In fact, at the beginning of implementing Node.js, the author Ryan Dahl did not choose JavaScript. He tried C and Lua, because it lacked some high-level language features, such as closures and functional programming, which made the program complicated and difficult to maintain.
JavaScript is a language that supports the functional programming paradigm, which fits well with Node.js' event-driven programming model. In addition, the V8 engine provided by Google has greatly improved the execution speed of the JavaScript language.
What is finally presented to us is Node.js, not Node.c, Node.lua or other language implementations.
(2) Node.js is not a JS application, but a JS operation platform
When you see the name Node.js, beginners may mistakenly think that this is a Javascript application. In fact, Node.js is written in C++ and is a Javascript running environment.
Node.js uses the V8 engine of Google Chrome, which has good performance and also provides many system-level APIs, such as file operation, network programming, etc.
Here are all the modules involved in NodeJS:
The Javascript code on the browser side will be subject to various security restrictions when running, and the operation of the customer system is limited.
In contrast, Node.js is a comprehensive background runtime, providing Javascript with many functions that other languages can achieve.
(3), Features of Node.js
Node.js is also relatively innovative in design. It runs in a single-process and single-threaded mode (this is consistent with the way Javascript runs),
The event-driven mechanism is implemented by Node.js through internal single-threading efficient maintenance of event loop queues. There is no resource occupation and context switching of multi-threads. This means that in the face of large-scale http requests, Node.js handles everything with event-driven.
Web service developers who are used to traditional languages may be very familiar with multi-threaded concurrency and collaboration, but in the face of Node.js, we need to accept and understand its characteristics.
Two, important concepts
1. What is Event Loop? (A very important concept)
Event Loop is a very important concept, referring to a computer system operating mechanism.
If you want to understand Event Loop, you must start with the program's running mode. The program after running is called a process. Generally, a process can only perform one task at a time.
If there are many tasks to be executed, there are only three solutions.
(1), line up. Because a process can only execute one task at a time, it has to wait until the previous task is completed before executing the subsequent task.
(2), create a new process. Use the fork command to create a new process for each task.
(3), create a new thread. Because processes consume too much resources, today's programs often allow a process to contain multiple threads, and the threads complete the task.
Take JavaScript language as an example. It is a single-threaded language. All tasks are completed on one thread, that is, the first method above is adopted. Once you encounter a large number of tasks or a time-consuming task, the web page will experience "fake death" because JavaScript cannot stop and cannot respond to user behavior.
You may ask, why is JavaScript single threaded? Can't it be implemented as multi-threaded?
This has something to do with history:
A major feature of the JavaScript language is single threading, which means that you can only do one thing at the same time. So, why can't JavaScript have multiple threads? This will improve efficiency.
JavaScript's single threading is related to its purpose. As a browser scripting language, JavaScript's main purpose is to interact with users and operate DOM. This determines that it can only be single threaded, otherwise it will cause very complex synchronization problems.
For example, suppose JavaScript has two threads at the same time, one thread adds content on a certain DOM node, and the other thread deletes this node, which thread should the browser take at this time?
Therefore, in order to avoid complexity, JavaScript is a single thread from its birth, which has become the core feature of this language and will not change in the future.
In order to utilize the computing power of multi-core CPUs, HTML5 proposed the Web Worker standard, allowing JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and cannot operate the DOM.
Therefore, this new standard does not change the nature of JavaScript single threading.
Back to EventLoop:
Single threading means that all tasks need to be queued, and the previous task will be executed before the next task will be executed. If the previous task takes a long time, the next task has to wait.
If the queue is because of the large amount of computing and the CPU is too busy, it would be fine, but many times the CPU is idle because the IO device (input and output device) is very slow (such as Ajax operation reads data from the network), and you have to wait for the result to come out before executing it.
The designer of the JavaScript language realized that at this time the main thread can completely ignore the IO device, suspend the waiting tasks, and run the next tasks first. Wait until the IO device returns the result, then turn around and continue the suspended task.
Therefore, all tasks can be divided into two types, one is synchronous task and the other is asynchronous task. Synchronous tasks refer to the tasks queued on the main thread, only the previous task has been executed.
The latter task can be executed; an asynchronous task refers to a task that does not enter the main thread but enters the "task queue". Only when the "task queue" notifies the main thread that an asynchronous task can be executed will the task enter the main thread for execution.
As shown in the figure below:
As long as the main thread is empty, it will read the "task queue". This is the running mechanism of JavaScript. This process will be repeated continuously.
3. Example explanation
Okay, let’s not say much “nonsense”, let’s start our first NodeJS application: “Hello Big Bear” immediately.
Open your favorite editor and create a HelloWorld.js file.
The code is as follows:
The code copy 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 code. First, execute your script with Node.js:
Open the command line tool CMD, switch to your working directory, and run the command "node HelloWorld.js"
Next, open the browser and visit http://localhost:8888/, and you will see a web page with the words "Hello, Big Bear!".
A little bit of extended knowledge:
As shown in the figure below, this is part of the source code of http.js in NodeJS. CreateServer is a user-friendly interface. The internal implementation adopts a singleton model. The advantage of this is to effectively separate the creation and initialization tasks of instances, focus on responsibilities, and reduce coupling. This is an idea that everyone can learn from when programming.
Hahaha, isn't it very interesting? This is just a short experience. I will explain a lot of knowledge points later. Everyone can slowly understand O(∩_∩)O Haha~
IV. Overall overview
1. It is a Javascript running environment
2. Rely on Chrome V8 engine for code explanation
3. Event-driven
4. Non-blocking I/O
5. Lightweight, scalable, suitable for real-time data interaction applications
6. Single process, single thread
Finally, what I want to say is: There are not many examples in this article, but these concepts are very important. You must have a clear understanding, so that you will lay a solid foundation for future NodeJS learning. Come on, work hard together.
Hahaha, this article is over and will not be continued. I hope to communicate with you more and make progress together. . . . . . Huhuhuhu…(*^__^*)