当前位置:网站首页>stream演示
stream演示
2022-04-22 18:41:00 【小二李】
本篇文章将介绍nodejs中stream文件流相关操作。
文件拷贝
const fs = require('fs')
const path = require('path')
// 文件路径
const filename = path.resolve(__dirname, 'data.txt')
const backFile = path.resolve(__dirname, 'back.txt')
// 创建读取流
const readStream = fs.createReadStream(filename)
// 创建写入流
const writeStream = fs.createWriteStream(backFile)
// 执行拷贝:pipe(不是一次性拷贝,而是一点点写入)
readStream.pipe(writeStream)
// 监听数据拷贝状态
readStream.on('end', () => {
console.log('拷贝完成')
})
读写网络日志
将每次访问的GET请求日志写入到文件。
const fs = require('fs')
const path = require('path')
const http = require('http')
const logFile = path.resolve(__dirname,'data.txt')
// 创建server
const server = http.createServer((req, res) => {
const method = req.method
if (method === 'GET') {
const readStream= fs.createReadStream(logFile)
// 将本地文件内容写入到res中去
readStream.pipe(res)
}
})
// 监听端口
server.listen(8000, () => {
console.log('监听端口8000的GET请求...')
})
版权声明
本文为[小二李]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43800535/article/details/124319395
边栏推荐
- Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
- [fundamentals of interface testing] Chapter 11 | detailed explanation of postman associated interface and batch execution use case set
- 对.NET未来的一点感悟
- Talk about the pit of cache Boolean value
- 【electron】文件下载卡住
- 【CIcadplayer】进度条回调
- 我国有哪些完全自研的电脑硬件?
- 【Proteus仿真】51单片机8路舵机点动±90°点动控制
- 关于.net core 中使用ActionFilter 以及ActionFilter的自动事务
- The Sandbox 与 Apex Athletes 达成合作关系
猜你喜欢

【全网最全】JSR303参数校验与全局异常处理(从理论到实践别用if判断参数了)

What kind of database products do we need

Popular grass planting product forecast, guess the top 3 to get a red envelope!

力扣-931.下降路径最小和

2022语言与智能技术竞赛再升级,推出NLP四大前沿任务

208. Implement trie (prefix tree)

jvm 组成

One click download scheme of serial port of esp32 / esp8266 series single chip microcomputer without peripheral circuit

Take you to understand the principle of highly flexible spark architecture

.net core 多项目中使用EFCore
随机推荐
Recent learning experience
How to select the mobile phone running memory?
The content exported by the rich text editor is PDF or docx
JVM composition
The balance between safety and opportunity -- the choice and thinking of St stock investment target
Kellerman Software .NET SFTP Library
高并发之——深度解析ScheduledThreadPoolExecutor类的源代码
[thinking and progress]: regret about yourself
安全与机会的平衡---st股投资标的选择与思考
Applets -- components
Recommendation of safe, fast and low-cost futures companies in 2022?
人气种草产品预测,猜中TOP 3得红包!
General mode of deploying modern applications in kubernetes cluster
山东大学项目实训树莓派提升计划二期(九)继承和多态
Pytorch Note57 Pytorch可视化网络结构
C language precompiling (preprocessing) define
leetcode - 234. 回文链表
.net core 添加中间件
C语言的预编译(预处理)#define
2022語言與智能技術競賽再昇級,推出NLP四大前沿任務