An in-depth analysis of the Node.js event loop_node.js
Node.js is a single-process, single-threaded application, but supports concurrency through events and callbacks, so its performance is very high.
(Source: Javascript is single-threaded and asynchronous, but these languages have a common feature: they are event-driven. The events that drive them come from a heterogeneous platform.)
Every API in Node.js is asynchronous and runs as a separate thread, uses asynchronous function calls, and handles concurrency.
Basically all event mechanisms in Node.js are implemented using the observer pattern in the design pattern.
Node.js single thread is similar to entering a while(true) event loop until no event observer exits. Each asynchronous event generates an event observer. If an event occurs, the callback function is called.
Event-driven model
Node.js uses an event-driven model. When the webserver receives a request, it closes it and processes it, and then serves the next web request.
When this request is completed, it is put back into the processing queue, and when the beginning of the queue is reached, the result is returned to the user.
This model is very efficient and scalable because the webserver always accepts requests without waiting for any read or write operations.
(This is also called non-blocking IO or event-driven IO)
In the event-driven model, a main loop is generated to listen for events and trigger a callback function when an event is detected.
This is how the entire event-driven process is implemented, which is very simple. Somewhat similar to the observer pattern, the event is equivalent to a subject (Subject), and all handler functions registered to this event are equivalent to observers (Observer).
Node.js has multiple built-in events. We can bind and listen to events by introducing the events module and instantiating the EventEmitter class, as shown in the following example:
//引入events模块 var events = require('events'); //创建eventEmitter对象 var eventEmitter = new events.EventEmitter(); //创建事件处理程序 var connectHandler = function connected() { console.log('连接成功。'); //触发data_received事件 eventEmitter.emit('data_received'); } //绑定connection事件处理程序 eventEmitter.on('connection', connectHandler); //使用匿名函数绑定data_received事件 eventEmitter.on('data_received', function(){ console.log('数据接收成功。'); }); //触发connection事件 eventEmitter.emit('connection'); console.log("程序执行完毕。");
The output result of the above code is:
Connection successful.
Data received successfully.
The program execution is completed.
The above results are just as you think. Through this event-driven model, we can achieve asynchronous operations.
(For example, we can read the file while executing other commands. After the file reading is completed, we return the file content as a parameter of the callback function. This way there is no blocking or waiting for file I/O while executing the code Operation. This greatly improves the performance of Node.js and can handle a large number of concurrent requests)
.
var fs = require("fs"); fs.readFile('input.txt', function (err, data) { if (err){ console.log(err.stack); return; } console.log(data.toString()); }); console.log("程序执行完毕。");
The output result of the above code is:
The program execution is completed.
Contents of input.txt.
From the above code, we can see that fs.readFile can distribute events. Of course, many objects in Node.js will distribute events. A net.Server object will also distribute an event every time there is a new connection. All of these generate Event objects are instances of events.EventEmitter.
But most of the time we don’t use EventEmitter directly, but inherit it in the object. Including fs, net, http, as long as the core modules that support event response are subclasses of EventEmitter.
Why do you do this? There are two reasons:
First of all, the event implementation of an object with a certain entity function conforms to the semantics. The monitoring and emission of events should be methods of an object.
Secondly, the object mechanism of JavaScript is based on prototype and supports partial multiple inheritance. Inheriting EventEmitter will not disrupt the original inheritance relationship of the object.
Let me share with you my first NodeJs project
There are usually two ways to install Node.js: compiling the source code yourself and using compiled files. I use compiled files here
Currently, I have the node-v4.2.3-linux-x64 I just downloaded in my home directory
1. First decompress
tar xvf node-v4.2.3-linux-x64
2. Set the link. The purpose of setting the link is that the node command can be used in any path
ln -s /home/node-v4.2.3-linux-x64/bin/node /usr/local/bin/node
ln -s /home/node-v4.2.3-linux-x64/bin/npm /usr/local/bin/npm
3. Create a directory called mynodeproj under the home folder, and then create a file called server.js (of course you can also name it another way)
var http = require('http'); http.createServer(function (request, response) { //发送 HTTP 头部 //HTTP 状态值: 200 : OK //内容类型: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); //发送响应数据 "Hello World" response.end('Hello World\n'); }).listen(8888); //终端打印如下信息 console.log('Server running at http://127.0.0.1:8888/');
4. Use the node command to execute the above code
node server.js
After executing the above command, "Server runnint at http://127.0.0.1:8888/" will be displayed on the command line, which means that the program has been executed successfully!
Because my server is running in Alibaba Cloud, I can access it by just typing Alibaba Cloud IP + port in an external browser.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
