Related recommendations: "nodejs Tutorial"
For most students with back-end experience, the Stream object is a reasonable and common object, but front-end classmate Stream is not so taken for granted. There is even an article with more than 9,000 stars on github introducing what Stream is - stream-handbook (https://link.zhihu.com/?target=https: //github.com/substack/stream-handbook). In order to better understand Stream, let’s briefly summarize it based on this article.
What is Stream
Stream is a very common and important concept in Unix systems. In terminology, stream is an abstraction of input and output devices.
ls | grep *.js
We often encounter codes like this when writing scripts. Use |
to connect two commands and use the result of the previous command as the next one. The parameters of the command are passed in, so that the data is transmitted like water in the pipeline. Each command is like a processor, doing some processing on the data, so | is called "Pipeline symbol ".
Several types of Stream in NodeJS
From a program perspective, a stream is directional data, which can be divided into three types according to the flow direction
Device flow to program: readable
Program flow to device: writable
-
Bidirectional: duplex, transform
NodeJS’s stream operations are encapsulated into the Stream module, which is also referenced by multiple core modules. According to the Unix philosophy: everything is a file, most file processing in NodeJS uses streams to complete
Ordinary files
Device files ( stdin, stdout)
Network files (http, net)
There is a knowledge point that is easily overlooked: all Streams in NodeJS All are instances of EventEmitter.
Small example
When we write a program, we suddenly need to read a certain configuration file config.json. At this time, we will briefly analyze the
- data :Contents of config.json
- Direction: Device (physical disk file) -> NodeJS program
We should use readable stream to do this
const fs = require('fs'); const FILEPATH = '...'; const rs = fs.createReadStream(FILEPATH);
Through the createReadStream()
method provided by the fs module, we easily create a readable stream. At this time, the content of config.json flows from the device to the program. We do not use the Stream module directly because fs has already referenced the Stream module internally and encapsulated it.
After we have the data, we need to process it. For example, we need to write to a certain path DEST. At this time, we need a writable stream to allow the data to flow from the program to the device.
const ws = fs.createWriteStream(DEST);
Now that we have two streams, that is, two data processors, how do we connect the streams through the Unix-like pipe symbol |
? The pipe symbol in NodeJS is the pipe()
method.
const fs = require('fs'); const FILEPATH = '...'; const rs = fs.createReadStream(FILEPATH); const ws = fs.createWriteStream(DEST); rs.pipe(ws);
In this way, we use the stream to implement a simple file copy function. The implementation principle of the pipe() method will be mentioned later, but there is one thing worth noting: the data must be piped from the upstream to the downstream, that is Pipe from a readable stream to a writable stream.
Process the data
The readable and writable streams mentioned above, we call them processors, which is actually not appropriate because we are not processing anything, we are just reading the data, and then Storing data.
If there is a need, change all the letters in the local package.json file to lowercase and save it to the package-lower.json file in the same directory.
At this time we need to use a two-way stream. Assume that we have a stream lower that specializes in converting characters to lowercase. Then the code written is probably like this
const fs = require('fs'); const rs = fs.createReadStream('./package.json'); const ws = fs.createWriteStream('./package-lower.json'); rs.pipe(lower).pipe(ws);
At this time we can You can see why the stream connected by pipe() is called a processor. According to what was said above, the pipe must be from a readable stream to a writable stream:
- rs -> lower: lower is downstream, so lower needs to be a writable stream
- lower -> ws: Relatively speaking, lower is upstream, so lower needs to be a readable stream
It’s a bit of reasoning, but it can satisfy The lower we need must be a bidirectional flow. We will mention the specific use of duplex or transform later.
Of course, if we have some additional processing actions, such as letters that need to be converted into ASCII codes, assuming there is an ascii stream, then our code may be
rs.pipe(lower).pipe(acsii).pipe(ws);
Similarly, ascii must also be a bidirectional stream . The logic of this processing is very clear, so in addition to clear code, what are the benefits of using streams?
Why Stream should be used
There is a scenario where a user needs to watch a video online. Assume that we return movie content to the user through an HTTP request, then the code may be written like this
const http = require('http'); const fs = require('fs'); http.createServer((req, res) => { fs.readFile(moviePath, (err, data) => { res.end(data); }); }).listen(8080);
这样的代码又两个明显的问题
电影文件需要读完之后才能返回给客户,等待时间超长
电影文件需要一次放入内存中,相似动作多了,内存吃不消
用流可以讲电影文件一点点的放入内存中,然后一点点的返回给客户(利用了 HTTP 协议的 Transfer-Encoding: chunked 分段传输特性),用户体验得到优化,同时对内存的开销明显下降
const http = require('http'); const fs = require('fs'); http.createServer((req, res) => { fs.createReadStream(moviePath).pipe(res); }).listen(8080);
除了上述好处,代码优雅了很多,拓展也比较简单。比如需要对视频内容压缩,我们可以引入一个专门做此事的流,这个流不用关心其它部分做了什么,只要是接入管道中就可以了
const http = require('http'); const fs = require('fs'); const oppressor = require(oppressor); http.createServer((req, res) => { fs.createReadStream(moviePath) .pipe(oppressor) .pipe(res); }).listen(8080);
可以看出来,使用流后,我们的代码逻辑变得相对独立,可维护性也会有一定的改善,关于几种流的具体使用方式且听下回分解。
更多编程相关知识,请访问:编程视频课程!!
The above is the detailed content of Understanding Streams in Node.js. For more information, please follow other related articles on the PHP Chinese website!

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
