前言
我是歌谣 微信公众号关注前端小歌谣一起学习前端知识 今天继续给大家讲解node中stream模块的讲解
案例
const fs=require("fs")
const rs=fs.createReadStream("./1.txt","utf-8")
rs.on("data",(chunk)=>{
console.log(chunk)
})
rs.on("end",()=>{
console.log("end")
})
rs.on("error",(err)=>{
console.log(err)
})
运行结果
优化
const http=require("http")
const fs=require("fs")
const zlib=require("zlib")
const gzip=zlib.createGzip()
http.createServer((req,res)=>{
const reads=fs.createReadStream("./index.js")
res.writeHead(200,{"Content-type":"applocation/x-javascript;charset=utf-8"})
reads.pipe(gzip).pipe(res)
}).listen(3000,()=>{
console.log("geyao")
})