首页 > 编程语言 >前端歌谣-第五拾一课-node之http模块之stream流

前端歌谣-第五拾一课-node之http模块之stream流

时间:2023-12-10 19:32:22浏览次数:38  
标签:node fs const log stream rs console http

前言

我是歌谣 微信公众号关注前端小歌谣一起学习前端知识 今天继续给大家讲解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)
})

运行结果

前端歌谣-第五拾一课-node之http模块之stream流_前端知识

优化

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")
})

运行结果

前端歌谣-第五拾一课-node之http模块之stream流_前端知识_02

标签:node,fs,const,log,stream,rs,console,http
From: https://blog.51cto.com/u_14476028/8762535

相关文章

  • 前端歌谣-第五十课-node之http模块之fs模块(续)
    前言我是歌谣微信公众号关注前端小歌谣一起学习前端知识今天继续给大家讲解node中fs模块的讲解同步创建constfs=require("fs")fs.mkdirSync("./geyao1",(err)=>{console.log(err)if(err&&err.code==="EEXIST"){console.log("目录已经存在")}})运行......
  • Redis数据结构3:REDIS_LISTNODE
    REDIS_LISTNODEREDIS_LISTNODE本质上与Java的LinkedList一致,NodeList即为链表,是基本的线性结构。C语言原生没有对链表的支持,Redis对链表进行了实现。listNodetypedefstructlistNode{structlistNode*prev;structlistNode*next;void*value;}listNode;l......
  • Http的演进
    Http的演进Http在1.1版本之前具有无状态的特点,每次请求都需要通过TCP三次握手四次挥手与服务器重新建立连接。比如某个客户端在短时间多次请求同一个资源,服务器并不能区别是否已经响应过用户请求,所以每次需要重新响应请求、耗费不必要的时间和流量。为了节省资源消耗,Http也进行了......
  • nginx upstream配置文件
    1.upstream使用upstream指定服务器组进行负载均衡userroot;worker_processes20;error_loglogs/error;pidlogs/nginx.pid;events{worker_connections1024;}http{log_formatmain'$remote_addr-$remote_user[$time_local]"$request"......
  • Netty内置的http报文解码流程
    netty解码netty通过内置处理器HttpRequestDecoder和HttpObjectAggregator对Http请求报文进行解码之后,Netty会将Http请求封装成一个FullHttpRequest实例,然后发送给下一站。Netty内置的与Http请求报文相对应的类大致有如下几个:(1)FullHttpRequest:包含整个Http请求的信息,包含对Htt......
  • Apache HTTP Server 的安装与配置
    一、概要1.环境(1)RockyLinux9.3二、安装与配置1.安装(1)安装sudodnfinstallhttpd-y(2)服务sudosystemctlstarthttpdsudosystemctlenablehttpdsystemctlstatushttpd(3)防火墙sudofirewall-cmd--add-service={http,https}--permanentsudo......
  • 前端歌谣-第四拾九课-node之http模块之fs模块
    前言我是歌谣微信公众号关注前端小歌谣一起学习前端知识今天继续给大家讲解node中fs模块的讲解创建文件constfs=require("fs")fs.mkdir("./geyao",(err)=>{console.log(err)if(err&&err.code==="EEXIST"){console.log("目录已经存在")}})运行结果重命......
  • 前端歌谣-第四拾捌课-node之http模块之event模块
    前言我是歌谣微信公众号关注前端小歌谣一起学习前端知识今天继续给大家讲解node中event的讲解案例constEventEmitter=require("events")constevent=newEventEmitter()event.on("play",()=>{console.log("事件触发了")})event.emit("play")运行结果案例1varhttp=r......
  • nvm、node、npm之间的关系和区别
    ......
  • Vue学习之node.js环境下利用Vue-cli脚手架搭建Vue项目
    目录第一个Vue-cli应用什么是vue-cli?vue-cli主要功能:搭建需要的环境1、nvm-windows下载2、安装nvm3、修改nvm环境变量4、通过nvm安装node5、安装全局npm6、一些替代npm的方式安装vue-cli第一个脚手项目配置命令行运行Vue项目IDEA运行Vue项目第一个Vue-cli应用之前练习了Vue一些......