Câu hỏi phỏng vấn Nodejs
Câu hỏi

What is **Pip...

Câu trả lời

Piping in Node.js refers to the process of connecting the output of one stream directly into another stream, allowing data to flow seamlessly between them. This is particularly useful when you want to read data from one source and write it to another without managing the flow of data manually.

In Node.js, streams are objects that let you read data from a source or write data to a destination in a continuous fashion. There are four types of streams: Readable, Writable, Duplex (which can be both Readable and Writable), and Transform (a type of Duplex stream where the output is computed from the input).

When you use piping, you essentially take the data that comes out of a Readable stream and immediately write it into a Writable stream. This can be done using the .pipe() method that's available on Readable streams. The .pipe() method takes a Writable stream as its destination and handles the data transfer automatically. It also manages backpressure to ensure that the destination is not overwhelmed by a fast source.

Here's a simple example of piping in Node.js:

javascript Copy
const fs = require('fs');

// Create a readable stream from a source file
const readStream = fs.createReadStream('source.txt');

// Create a writable stream to a destination file
const writeStream = fs.createWriteStream('destination.txt');

// Pipe the read stream to the write stream
readStream.pipe(writeStream);

In this example, the contents of source.txt w...

senior

senior

Gợi ý câu hỏi phỏng vấn

middle

What is the meaning of the @ prefix on npm package?

expert

Can Node.js use other engines than V8?

middle

What are express.json() and express.urlencoded() in Express.js?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào