node.js fs.read() 示例 [英] node.js fs.read() example

查看:52
本文介绍了node.js fs.read() 示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

app=function(req,res){res.writeHead(200,{'Content-Type':'text/plain'})var 缓冲区=新缓冲区(100)var fs=require('fs')fs.open('.'+req.url,'r',function(err,fd){fs.fstat(fd,function(err, stats){变量 i=0var s=stats.sizeconsole.log('.'+req.url+' '+s)for(i=0;i

为什么这只会多次显示 979 字节文件中的最后 100 字节?

为什么 chrome 浏览器不显示任何输出?

gert@node:~/http$ node server.js获取 http://127.0.0.1:8000/appwsgi/www/index.htm./appwsgi/www/index.htm 9791002003004005006007008009001000vi/vi.htm">vi</a>编辑服务器上的在线文件.

</html><br/>

</html><br/>

</html><br/>

</html><br/>

</html><br/>

</html><br/>

</html><br/>

</html><br/>

</html>

所有读取都使用相同的缓冲区异步发出(即 fs.read 立即返回并继续循环).到第一次调用异步回调时,显然所有十次读取都已完成(因此缓冲区包含最后一次读取的结果).由于您调用了 fs.read 10 次,因此您将被调用 10 次.所以你得到你所看到的.

浏览器什么都不显示,因为您在第一个回调返回之前结束了响应.

app=function(req,res)
{
 res.writeHead(200,{'Content-Type':'text/plain'})
 var buffer=new Buffer(100)
 var fs=require('fs')
 fs.open('.'+req.url,'r',function(err,fd){
  fs.fstat(fd,function(err, stats){
   var i=0
   var s=stats.size
   console.log('.'+req.url+' '+s)
   for(i=0;i<s;console.log(i)){
    i=i+buffer.length
    fs.read(fd,buffer,0,buffer.length,i,function(e,l,b){
     res.write(b.toString('utf8',0,l))
     console.log(b.toString('utf8',0,l))
    })
   }
   res.end()
   fs.close(fd)
  })
 })
}
http = require('http')
server = http.createServer(app)
server.listen(8000,"127.0.0.1")
console.log('GET http://127.0.0.1:8000/appwsgi/www/index.htm')

Why does this only show the last 100 bytes multiple times from a 979 bytes file?

Why does chrome browser not show any output?

gert@node:~/http$ node server.js 
GET http://127.0.0.1:8000/appwsgi/www/index.htm
./appwsgi/www/index.htm 979
100
200
300
400
500
600
700
800
900
1000
"vi/vi.htm">vi</a> Edit online files on the server.
  </div>
 </body>
</html>

oad.<br/>
   <a href=
"vi/vi.htm">vi</a> Edit online files on the server.
  </div>
 </body>
</html>

oad.<br/>
   <a href=
"vi/vi.htm">vi</a> Edit online files on the server.
  </div>
 </body>
</html>

oad.<br/>
   <a href=
"vi/vi.htm">vi</a> Edit online files on the server.
  </div>
 </body>
</html>

oad.<br/>
   <a href=
"vi/vi.htm">vi</a> Edit online files on the server.
  </div>
 </body>
</html>

oad.<br/>
   <a href=
"vi/vi.htm">vi</a> Edit online files on the server.
  </div>
 </body>
</html>

oad.<br/>
   <a href=
"vi/vi.htm">vi</a> Edit online files on the server.
  </div>
 </body>
</html>

oad.<br/>
   <a href=
"vi/vi.htm">vi</a> Edit online files on the server.
  </div>
 </body>
</html>

oad.<br/>
   <a href=
"vi/vi.htm">vi</a> Edit online files on the server.
  </div>
 </body>
</html>

解决方案

All of the reads are issued asynchronously using the same buffer (i.e. fs.read returns immediately and the loop continues). By the time the async callback is called the first time, apparently all ten reads have completed (so the buffer contains the results of the last read). Since you called fs.read 10 times, you'll get called back 10 times. So you get what you see.

The browser shows nothing because you've ended the response before the first callback returns.

这篇关于node.js fs.read() 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆