您现在的位置是:热点 >>正文
面试官:说说对 Node 中的 Stream 的理解?应用场景?
热点3768068人已围观
简介...
一、是什么
流(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转换为es5,我们在左No边写入 es6,从右边读取 es5
基本代码如下所示:
const { Transform } = require('stream'); const myTransform = new Transform({ transform(chunk, encoding, callbadeck) { // ... } }); 三、应用场景
stream的应用场景主要就是处理IO操作,而http请求和文件操作都属于IO操作
思想一下,如果一次IO操作过大,硬件的开销就过大,而将此次大的IO操作进行分段操作,让数据像水管一样流动,知道流动完成
常见的场景有:
get请求返回文件给客户端 文件操作 一些打包工具的 中底层操作
get请求返回文件给客户端
使用stream流返回文件,res也是一个stream对象,通过pipe管道将文件数据返回
const server = http.createServer(function (req, res) { const method = req.method;的 // 获取请求方法 if (method === 'GET') { // get 请求 const fileName = path.resolve(__dirStname, 'data.txt'); let stream = fs.createReadStream(fileName); stream.pipe(res); // 将&nbsrep;res 作为 stream 的 dest } }); server.listen(8000); 文件操作
创建一个可读数据流readStream,一个可写数据流writeStream,通过pipe管道把数据流转过去
const fs = require('fs') constam path = require('path') // 两个文件名 const fileName1 = path.resolve(__dirname, 'data.txt') const fileName2 = path.resolve(__dirname, 'data-bak.txt' 的) // 读取文件的 stream 对象 const readStream = fs.createReadStream(fileName1) // 写入文件的 stream 对象 const writeStream = fs.createWriteStream(fileName2)&理解nbsp;// 通过 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:
相关文章
苹果12 mini销量不佳已遭停产 未来或全面取消mini机型
热点作者:小胖 据海外媒体报道,苹果(AAPL.NASDAQ)已经停产了iPhone 12 mini,并开始专注于清理库存。 针对此消息的真实性,有媒体向苹果方面进行了求证,但截至发稿未获回复。如果消息属...
【热点】
阅读更多标签: 腾讯云 吴运声 王慧星 优图实验室
热点云梦级3月24日云梦级消息,据媒体报道,腾讯云近期决定将腾讯云产品三部从腾讯云副总裁、腾讯云智能和优图实验室负责人吴运声辖下划归由腾讯副总裁、腾讯云 CTO 王慧星负责。此外,不少算力训练平台相关业务...
【热点】
阅读更多yyds!用机器学习预测 bilibili 股价走势
热点本文主要讲解用Python分析哔哩哔哩股价,通过对股票数据进行基础分析,结合运用matplotlib绘图库进行可视化,并用机器学习方法 — 蒙特卡洛模拟预测未来一年股价走势。 安装 我们需要安装num...
【热点】
阅读更多
热门文章
最新文章
友情链接
- 标签: 康师傅
- https://www.vietnamconsulate-sf.org/?action=CHECK&s=谷歌搜索留痕【tg:seogoogle987】,MHQ
- https://www.mintos.org/?s=谷歌搜索留痕【tg:seogoogle987】,m4p
- https://www.terramania.nl/catalogsearch/result/index/?q=谷歌搜索留痕【tg:seogoogle987】,SXe
- https://www.lechzuers.com/de/search?cat=163&q=谷歌搜索留痕【tg:seogoogle987】,hKR
- https://www.qwant.com/?q=谷歌搜索留痕【tg:seogoogle987】,C75
- https://zhidao.baidu.com/index/?action=QUERY&word=谷歌搜索留痕【tg:seogoogle987】,eze
- https://www.lyrics.com/lyrics/谷歌搜索留痕【tg:seogoogle987】,b8X
- https://www.ntv.co.jp/3min/search/?action=QUERY&q=谷歌搜索留痕【tg:seogoogle987】,udU
- https://www.parl.ca/legisinfo/fr/projets-de-loi?parlsession=all&view=list&keywords=谷歌搜索留痕【tg:seogoogle987】,LTk
- https://www.wellbees.com/index.php/catalogsearch/result/index/?q=谷歌搜索留痕【tg:seogoogle987】,9Yh
- https://www.nonsolocap.it/cap?action=SEARCH&k=谷歌搜索留痕【tg:seogoogle987】,SBb
- https://www.ushmm.org/search/results.php?q=谷歌搜索留痕【tg:seogoogle987】,5eQ
- https://www.varian.com/en-au/search?cat=113&search=谷歌搜索留痕【tg:seogoogle987】,XCz
- https://www.lezhin.com/ko/tags/谷歌搜索留痕【tg:seogoogle987】,vUj
- https://www.kelsa.co.uk/catalogsearch/result/index/?T=1686132119291&q=谷歌搜索留痕【tg:seogoogle987】,Wx1
- https://www.lssystems.co.uk/search?page=0&per_page=24&term=谷歌搜索留痕【tg:seogoogle987】,Gxg
- https://www.dba.dk/soeg/?soeg=谷歌搜索留痕【tg:seogoogle987】iDA
- https://www.nissin.com/jp/search/?cat=105&q=谷歌搜索留痕【tg:seogoogle987】,k0k
- https://www.sic-info.org/en/?s=谷歌搜索留痕【tg:seogoogle987】,6Uz
- https://www.tcv.org.uk/search/谷歌搜索留痕【tg:seogoogle987】,4mc
- https://www.teacherspayteachers.com/Browse/Price-Range/Free/Search:Western International University Physics degree=谷歌搜索留痕【tg:seogoogle987】,tDT
- https://www.rollingstone.it/?ID=DVMeu3AlmzZE1cuJ2FmgmO5TSCQRb3yh&s=谷歌搜索留痕【tg:seogoogle987】,uhy
- https://www.roblox.com/discover/?cat=107&Keyword=谷歌搜索留痕【tg:seogoogle987】,VGE
- https://www.kaiyueleather.com/search/谷歌搜索留痕【tg:seogoogle987】,BCw
- https://www.sdcard.org/ja/?s=谷歌搜索留痕【tg:seogoogle987】,Zbh
- https://www.wilsonart.com/catalogsearch/result/?action=CHECK&q=谷歌搜索留痕【tg:seogoogle987】,PB1
- https://www.uctronics.com/catalogsearch/result/?q=谷歌搜索留痕【tg:seogoogle987】,9Jy
- https://www.uckfieldtc.gov.uk/?s=谷歌搜索留痕【tg:seogoogle987】,F5d
- https://www.lenovo.com/gb/en/search?action=SEARCH&text=谷歌搜索留痕【tg:seogoogle987】,bSp
- https://www.tarleton.edu/news/search/谷歌搜索留痕【tg:seogoogle987】,HNl
- https://www.krisinformation.se/en/search?q=谷歌搜索留痕【tg:seogoogle987】,0kA
- https://www.vfw.org/search-results?q=谷歌搜索留痕【tg:seogoogle987】,IpB
- https://www.tumblr.com/tagged/谷歌搜索留痕【tg:seogoogle987】,8gd
- https://www.duksung.ac.kr/search/front/Search.jsp?qt=谷歌搜索留痕【tg:seogoogle987】aGi
- https://www.samen-maier.at/search/谷歌搜索留痕【tg:seogoogle987】,YxZ
- https://www.sonepar.ch/fr/search?q=谷歌搜索留痕【tg:seogoogle987】,eRx
- https://www.kuaikanmanhua.com/sou/谷歌搜索留痕【tg:seogoogle987】,3Bo
- https://www.youthforum.org/search-results?cx=4f5236218ab5e8ef9&ie=UTF-8&q=谷歌搜索留痕【tg:seogoogle987】,Gto
- https://www.impactpool.org/search/?type=79&q=谷歌搜索留痕【tg:seogoogle987】7Jz
- https://m.shoppinghow.kakao.com/m/search/q/谷歌搜索留痕【tg:seogoogle987】hpq
- https://www.searcychamber.com/list/search?AsfnA=DtkTQzs&q=谷歌搜索留痕【tg:seogoogle987】,iuR
- https://www.abbreviations.com/谷歌搜索留痕【tg:seogoogle987】EqN
- https://store.line.me/search/sticker/ja?q=谷歌搜索留痕【tg:seogoogle987】eTG
- https://www.jusoen.com/addreng.asp?p1=谷歌搜索留痕【tg:seogoogle987】ag2
- https://www.metaltix.com/?view=tours&filter=search&q=谷歌搜索留痕【tg:seogoogle987】,xXC
- https://www.spoiledsweetboutique.com/search/谷歌搜索留痕【tg:seogoogle987】,GCa
- https://www.zisha.com/shop/list.shtml?wap_keyword=谷歌搜索留痕【tg:seogoogle987】,yIh
- https://www.welshmountainzoo.org/shop/index.php?routeproduct/search&tag=谷歌搜索留痕【tg:seogoogle987】,M10
- https://www.lindseymedicalsupply.com/search/谷歌搜索留痕【tg:seogoogle987】,gaw
- https://search.danawa.com/dsearch.php?query=谷歌搜索留痕【tg:seogoogle987】xM1