博客搬家了,欢迎大家关注,https://bobjin.com

Node.js写文件的三种方式:

1、通过管道流写文件

  采用管道传输二进制流,可以实现自动管理流,可写流不必当心可读流流的过快而崩溃,适合大小文件传输(推荐)

  var readStream = fs.createReadStream(decodeURIComponent(root + filepath.pathname)); // 必须解码url
readStream.pipe(res); // 管道传输
res.writeHead(200,{
'Content-Type' : contType
}); // 出错处理
readStream.on('error', function() {
res.writeHead(404,'can not find this page',{
'Content-Type' : 'text/html'
});
readStream.pause();
res.end('404 can not find this page');
console.log('error in writing or reading ');
});

2、手动管理流写入

  手动管理流,适合大小文件的处理

 var readStream = fs.createReadStream(decodeURIComponent(root + filepath.pathname));
res.writeHead(200,{
'Content-Type' : contType
}); // 当有数据可读时,触发该函数,chunk为所读取到的块
readStream.on('data',function(chunk) {
res.write(chunk);
}); // 出错时的处理
readStream.on('error', function() {
res.writeHead(404,'can not find this page',{
'Content-Type' : 'text/html'
});
readStream.pause();
res.end('404 can not find this page');
console.log('error in writing or reading ');
}); // 数据读取完毕
readStream.on('end',function() {
res.end();
});

3、通过一次性读完数据写入

  一次性读取完文件所有内容,适合小文件(不推荐)


 fs.readFile(decodeURIComponent(root + filepath.pathname), function(err, data) {
if(err) {
res.writeHead(404,'can not find this page',{
'Content-Type' : 'text/html'
});
res.write('404 can not find this page'); }else {
res.writeHead(200,{
'Content-Type' : contType
});
res.write(data);
}
res.end();
});
博客搬家了,欢迎大家关注,https://bobjin.com
 

Node.js写文件的三种方法的更多相关文章

  1. 前端js,css文件合并三种方式,bat命令

    前端js,css文件合并三种方式,bat命令 前端js文件该如何合并三个方式如下:1. 一个大文件,所有js合并成一个大文件,所有页面都引用它.2. 各个页面大文件,各自页面合并生成自己所需js的大文 ...

  2. JAVA写JSON的三种方法,java对象转json数据

    JAVA写JSON的三种方法,java对象转json数据 转自:http://www.xdx97.com/#/single?bid=5afe2ff9-8cd1-67cf-e7bc-437b74c07a ...

  3. VC中加载LIB库文件的三种方法

    VC中加载LIB库文件的三种方法 在VC中加载LIB文件的三种方法如下: 方法1:LIB文件直接加入到工程文件列表中   在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中&quo ...

  4. Logstash处理json格式日志文件的三种方法

    假设日志文件中的每一行记录格式为json的,如: {"Method":"JSAPI.JSTicket","Message":"JS ...

  5. java将doc文件转换为pdf文件的三种方法

    http://feifei.im/archives/93 —————————————————————————————————————————————— 项目要用到doc转pdf的功能,一番google ...

  6. python面对对象编程------3:写集合类的三种方法

    写一个集合类的三种方法:wrap,extend,invent 一:包装一个集合类 class Deck: def __init__( self ): self._cards = [card6(r+1, ...

  7. Python实现下载文件的三种方法

    下面来看看三种方法是如何来下载zip文件的:方法一: import urllib print "downloading with urllib" url = 'http://www ...

  8. Viewing the interface of your Swift code,查看Swift代码的头文件的三种方法

      Technical Q&A QA1914 Viewing the interface of your Swift code Q:  How do I view the interface ...

  9. python下载文件的三种方法

    Python开发中时长遇到要下载文件的情况,最常用的方法就是通过Http利用urllib或者urllib2模块. 当然你也可以利用ftplib从ftp站点下载文件.此外Python还提供了另外一种方法 ...

随机推荐

  1. Android 工具-adb

    Android 工具-adb 版权声明:本文为博主原创文章,未经博主允许不得转载. Android 开发中, adb 是开发者经常使用的工具,是 Android 开发者必须掌握的. Android D ...

  2. Java列表

    Java列表踩过的坑 其中subList是RandomAccessSubList,不是序列化的列表,不可以加入tair. 加入tair测试代码 @Autowired private CacheMana ...

  3. trigger事件模拟

    事件模拟trigger 在操作DOM元素中,大多数事件都是用户必须操作才会触发事件,但有时,需要模拟用户的操作,来达到效果. 需求:页面初始化时触发搜索事件并获取input控件值,并打印输出(效果图如 ...

  4. 关于MJRefresh的下拉加载数据bug

    当没有更多数据的时候显示NoMoreData 我的理解是先结束刷新再显示没有更多 今天之前一直没发现有问题 贴之前的代码 [self.collectionView reloadData]; [self ...

  5. iOS 10 跳转系统设置

    苦心人天不负, 为了项目终于把 iOS 10 跳转系统设置的方法给搞定了, 很欣慰. http://www.cnblogs.com/lurenq/p/6189580.html iOS 10 跳转系统设 ...

  6. 使用git进行源代码管理

    git是一款非常流行的分布式版本控制系统,使用Local Repository追踪代码的修改,通过Push和Pull操作,将代码changes提交到Remote Repository,或从Remote ...

  7. 简历生成平台项目开发-STEP1问卷设计

    周五课程结束完后,小组建立QQ群和微信群,着手讨论项目问题.一开始的大概想法:就业信息平台,收集企业招聘信息和就业生资料,提供给学生和企业的校企对接平台.后来发现群里谭卓同学也有个相关的思路,经过商量 ...

  8. mysql-5.6.34 Installation from Source code

    Took me a while to suffer from the first successful souce code installation of mysql-5.6.34. Just pu ...

  9. PHP5.4~7.1新特性总结

    http://note.youdao.com/noteshare?id=7273b858fc12873ad092979e4ba173a7&sub=WEB334fdcf50b507ad93549 ...

  10. 【Java并发编程实战】-----“J.U.C”:Exchanger

    前面介绍了三个同步辅助类:CyclicBarrier.Barrier.Phaser,这篇博客介绍最后一个:Exchanger.JDK API是这样介绍的:可以在对中对元素进行配对和交换的线程的同步点. ...