CS代写 READABLE.STREAM

READABLE.STREAM

FILE I/O BASICS
We already read in files for our JavaScript – Modules

Copyright By PowCoder代写 加微信 powcoder

We use files to store JSON, our objects, text (README and licensing) and configuration data (like which environment in property files.)

INFLOW STREAMS
An inbound stream of data is a input stream. Data that is consumed by your application.

If you use the keyboard, data flows from the keyboard into your program.
That is a type of input stream.

THE FS OBJECT
If you want to open a file, use an input stream. Use the built-in fs object.

WHY USE FILES?
Files are durable compared to in-process or memory based retrieval.
Very useful with configuration or initial values!

STREAM.READABLE
const fs = require(‘fs’);
const readStream = fs.createReadStream(‘demo.txt’); readStream.on(‘open’, () => {
console.log(‘The file is open’); });

STREAM.READABLE EVENTS
The ‘close’ event is emitted when the stream and any of its underlying resources are closed.
This event indicates that no more events will be emitted.

STREAM.READABLE EVENTS
The ‘data’ event will be emitted whenever a chunk of data is available

STREAM.READABLE EVENTS
The ‘end’ event is emitted when there is no more data to be read

STREAM.READABLE EVENTS
The ‘error’ event may be emitted anytime.

STREAM.READABLE EVENTS
The ‘readable’ event is emitted when there is data available to be read.

EXAMPLE USE
const fs = require(‘fs’);
const dataStream= fs.createReadStream(‘data.json’);
dataStream.on(‘readable’, ( ) => { while (data = this.read()) {
console.log(data); }
_stream.on(‘end’, () => { console.log(‘end’) });

A pipe is a destination to write to. readStream.pipe(writableStream)

HOW A PIPE WORKS
The readable.pipe() method attaches a Writable stream to the readable, causing it to switch automatically into flowing mode and push all of its data to the attached Writable.
The flow of data will be automatically managed so that the destination Writable stream is not overwhelmed by a faster Readable stream

CHAINING PIPES
readableStream .pipe(writeToStream1) .pipe(writeToStream2) .pipe(finalWritableStream)

READ ~ WRITE PIPES
readStream.on(‘data’, (chunk) => { writeStream.write(chunk);
readStream.on(‘end’, () => { writeStream.end();

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com