您现在的位置是:热点 >>正文
面试官:说说对 Node 中的 Stream 的理解?应用场景?
热点7015326人已围观
简介...
一、是什么
流(Stream),是一种数据传输手段,是端到端信息交换的一种方式,是有顺序的,是逐块读取数据、处理内容,用于顺序读取输入或写入输出
在很多时候,流(Stream)是字节流(Byte Steram)的简称,也就是长长的一串字节
除了字节流,还可以有视频流、音频流、数据流
流的独特之处在于,它不像传统的程序那样一次将一个文件读入内存,而是逐块读取数据、处理其内容,而不是将其全部保存在内存中
流可以分成三部分:source、dest面试、pipe
在source和dest之间有一个连接的管道pipe,它的基本语法是source.pipe(dest),source和dest就是通过pipe连接,让数据从source流向了dest,如下图所示:
二、种类
在NodeJS,几乎所有的地方都使用到了流的概念,分成四个种类:
可写流:可写入数据的流。例如 fs.createWriteStream() 可以使用流将数据写入文件
可读流:可读取数据的流。例如fs.createReadStream() 可以从官:文件读取内容 双工流:既可读又可写的流。例如 net.Socket 转换流:可以在数据写入和读取时修改或转换数据的流。例如,在文件压缩操作中,可以向文件写入压缩数据,并从文件中读取解压数据
在NodeJS中HTTP服务器模块中,request 是可读流,response 是可写流。还有fs 模块,能同时处理可读和可写文件流
可读流和可写流都是单向的,比较容易理解,而另外两个是双向的
双工流
之前了解过websocket通信,是一个全双工通信,发送方和接受方都是各自说说独立的方法,发送和接收都没有任何关系
如下图所示:
基本代码如下:
const { Duplex } = require('stream'); const myDuplex = new Duplex({ read(size) { 对 // ... }, write(chunk, encoding, callback) { // ... } }); 转换流
转换流的演示图如下所示:
比如一个 babel,把es6转换为esNo5,我们在左边写入 es6,从右边读取 es5
基本代码如下所示:
const { Transform } = require('stream'); const myTransform = new Transform({ transform(chunk, encoding, decallback) { // ... } }); 三、应用场景
stream的应用场景主要就是处理IO操作,而http请求和文件操作都属于IO操作
思想一下,如果一次IO操作过大,硬件的开销就过大,而将此次大的IO操作进行分段操作,让数据像水管一样流动,知道流动完成
常见的场景有:
get请求返回文件给客户端 文件操作 一 中些打包工具的底层操作
get请求返回文件给客户端
使用stream流返回文件,res也是一个stream对象,通过pipe管道将文件数据返回
const server = http.createServer(function (req, res) { const method = req.meth的 od; // 获取请求方法 if (method === 'GET') { // get 请求 const fileName = path.resolveSt(__dirname, 'data.txt'); let stream = fs.createReadStream(fileName); stream.pipe(res); //&nbsrep;将 res 作为 stream 的 dest } }); server.listen(8000); 文件操作
创建一个可读数据流readStream,一个可写数据流writeStream,通过pipe管道把数据流转过去
const fs = require('fs') amconst path = require('path') // 两个文件名 const fileName1 = path.resolve(__dirname, 'data.txt') const fileName2 = path.resolve(__dirname, 'data-ba 的k.txt') // 读取文件的 stream 对象 const readStream = fs.createReadStream(fileName1) // 写入文件的 stream 对象 const writeStream = fs.createWriteStream(fileNa理解me2) // 通过 pipe执行拷贝,数据流转 readStream.pipe(writeStream) // 数据读取完成监听,即拷贝完成 readStream.on('end', function () { console.log('拷贝完成') }) 一些?应打包工具的底层操作
目前一些比较火的前端打包构建工具,都是通过node.js编写的,打包和构建的过程肯定是文件频繁操作的过程,离不开stream,如gulp
参考文献
https://xie.infoq.cn/article/1a9695020828460eb3c4ff1fa
https://juejin.cn/post/6844903891083984910
Tags:
相关文章
Zoom CEO袁征预言:AI将终结五天工作制,未来每周只需上班三到四天
热点在AI浪潮席卷职场的当下,Zoom创始人兼CEO袁征(Eric Yuan)给出了一个大胆而诱人的愿景:借助AI助手,人类有望在五年内告别传统的五天工作制,迈向每周仅需工作三到四天的新常态。 在近日举行...
【热点】
阅读更多亚马逊云服务计划在加拿大开设第二个云计算服务器中心
热点11月9日消息,据国外媒体报道,亚马逊云服务(AWS)将扩大其全球基础设施。当地时间周一,它宣布,将在加拿大开设第二个云计算服务器中心,该中心已破土动工。 据报道,该中心将位于卡尔加里,以便更好地覆盖...
【热点】
阅读更多不用代码趣讲 ZooKeeper 集群
热点一、为什么需要集群 1.1 马果果病了 ZKr~老规矩~ 马果果毕竟年纪大了,这办事处的事情越来越多,终于有一天扛不住,生病了,住院了,听医生说要休息好几天。办事处负责人不在的话就不能给村民们提供服务...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.zutari.com/?s=谷歌快排系统官网【tg:seogoogle987】,ghU
- https://www.whatsonstage.com/search?cat=64&term=谷歌快排系统官网【tg:seogoogle987】,rbK
- https://www.teacherspayteachers.com/Browse/Search:谷歌快排系统官网【tg:seogoogle987】,lDF
- https://www.paxnet.co.kr/search/jongmok?kwd=谷歌快排系统官网【tg:seogoogle987】KHf
- https://www.pearson.com/en-us/search.html?aq=谷歌快排系统官网【tg:seogoogle987】,ksR
- https://nicolazzi.it/en/?s=谷歌快排系统官网【tg:seogoogle987】,lJv
- https://www.zutari.com/search/谷歌快排系统官网【tg:seogoogle987】,8iU
- https://www.udc.co.uk/index.php?route=product/search&sort=p.price&order=DESC&tag=谷歌快排系统官网【tg:seogoogle987】,UzY
- https://www.teamlab.art/pt/?submit=谷歌快排系统官网【tg:seogoogle987】,wjQ
- https://www.lauben.com/en/?action=SEARCH&s=谷歌快排系统官网【tg:seogoogle987】,jHt
- https://www.soccerbible.com/search?q=谷歌快排系统官网【tg:seogoogle987】,qTU
- https://dict.naver.com/dict.search?dicQuery&query=谷歌快排系统官网【tg:seogoogle987】qio
- https://www.scivee.tv/?s=谷歌快排系统官网【tg:seogoogle987】,qwU
- https://www.spicebar.de/catalogsearch/result/?q=谷歌快排系统官网【tg:seogoogle987】,8o4
- https://www.merchbar.com/search?q=谷歌快排系统官网【tg:seogoogle987】,LjD
- https://www.ushmm.org/search/results.php?q=谷歌快排系统官网【tg:seogoogle987】,IDs
- https://www.pinkpanda.ro/magazin/ingrijirea-tenului?page=87&q=谷歌快排系统官网【tg:seogoogle987】,I4Y
- https://www.scq.ubc.ca/?page=2&s=谷歌快排系统官网【tg:seogoogle987】,Oc4
- https://zhidao.baidu.com/index/?action=QUERY&word=谷歌快排系统官网【tg:seogoogle987】,OlA
- https://www.wattpad.com/search/谷歌快排系统官网【tg:seogoogle987】Hmf
- https://www.valuecolleges.com/?action=QUERY&s=谷歌快排系统官网【tg:seogoogle987】,Byl
- https://www.searcychamber.com/list/search?AsfnA=DtkTQzs&q=谷歌快排系统官网【tg:seogoogle987】,bN0
- https://www.sic-info.org/?s=谷歌快排系统官网【tg:seogoogle987】,rbx
- https://www.nytimes.com/search?dropmab=true&query=谷歌快排系统官网【tg:seogoogle987】,Eqo
- https://www.maine.gov/pfr/professionallicensing/search/node?keys=谷歌快排系统官网【tg:seogoogle987】,9kS
- https://www.roberthalf.com/us/en/jobs/all/谷歌快排系统官网【tg:seogoogle987】1Td
- https://www.flickr.com/photos/tags/谷歌快排系统官网【tg:seogoogle987】0Gm
- https://www.varian.com/en-au/search?cat=113&search=谷歌快排系统官网【tg:seogoogle987】,R9D
- https://m.shoppinghow.kakao.com/m/search/q/谷歌快排系统官网【tg:seogoogle987】jtA
- https://www.polliwogs.com/search/谷歌快排系统官网【tg:seogoogle987】,bAH
- https://www.dba.dk/soeg/?soeg=谷歌快排系统官网【tg:seogoogle987】pAf
- https://www.voices.com/talents/search?keywords=谷歌快排系统官网【tg:seogoogle987】,xSf
- https://blvdseafood.com/?s=谷歌快排系统官网【tg:seogoogle987】Xq8
- https://www.londontheatredirect.com/search/谷歌快排系统官网【tg:seogoogle987】,uNs
- https://wwwcp.umes.edu/search/谷歌快排系统官网【tg:seogoogle987】,n5P
- https://www.yr.no/en/search?q=谷歌快排系统官网【tg:seogoogle987】,mbO
- https://www.temu.com/search_result.html?search_key=谷歌快排系统官网【tg:seogoogle987】t7R
- https://www.mintos.org/?s=谷歌快排系统官网【tg:seogoogle987】,VJD
- https://www.smics.com/tc/site/search?token=谷歌快排系统官网【tg:seogoogle987】,eIc
- https://www.lee.com/search?q=谷歌快排系统官网【tg:seogoogle987】,KSV
- https://www.teamlab.art/ko/?submit=谷歌快排系统官网【tg:seogoogle987】,D8P
- https://www.pmindia.gov.in/en/?s=谷歌快排系统官网【tg:seogoogle987】,F8U
- https://search.danawa.com/dsearch.php?query=谷歌快排系统官网【tg:seogoogle987】zrk
- https://www.oikos-tecnics.com/?s=谷歌快排系统官网【tg:seogoogle987】,V4J
- https://www.urmc.rochester.edu/news/search?q=谷歌快排系统官网【tg:seogoogle987】,Mlq
- https://www.lneg.pt/?cat=140&s=谷歌快排系统官网【tg:seogoogle987】,OTv
- https://www.toy-people.com/?s=谷歌快排系统官网【tg:seogoogle987】,Rhg
- https://www.samen-maier.at/search/谷歌快排系统官网【tg:seogoogle987】,eL5
- https://www.selfsufficientish.com/main/?s=谷歌快排系统官网【tg:seogoogle987】,85l
- https://www.rookwinkel.nl/catalogsearch/result/index/?q=谷歌快排系统官网【tg:seogoogle987】,mOr