Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js fs.read() example

Tags:

node.js

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>
like image 504
Gert Cuykens Avatar asked May 12 '11 23:05

Gert Cuykens


People also ask

What is read in node JS?

Node. js is used for server-side scripting. Reading and writing files are the two most important operations that are performed in any application. Node. js offers a wide range of inbuilt functionalities to perform read and write operations.

How do I read a file in node JS?

Node.js as a File Server To include the File System module, use the require() method: var fs = require('fs'); Common use for the File System module: Read files.

What is the reason to choose fs readFile () method to read a file in node JS?

readFile() method is used to read the file. This method read the entire file into buffer. To load the fs module, we use require() method. It Asynchronously reads the entire contents of a file.

What fs read returns?

readFile. Returns the contents of the file named filename. If encoding is specified then this function returns a string. Otherwise it returns a buffer.


3 Answers

I know this question is not the newest, but I'm going to chuck this up here because when I was getting issues how to open (and read) a filesystem object, a quick search always seemed to direct me here.

Anyhow, this should help with the OP, and others in the future.

(filepath is the actual filename, including path)

fs.open(filepath, 'r', function(err, fd) {
    fs.fstat(fd, function(err, stats) {
        var bufferSize=stats.size,
            chunkSize=512,
            buffer=new Buffer(bufferSize),
            bytesRead = 0;

        while (bytesRead < bufferSize) {
            if ((bytesRead + chunkSize) > bufferSize) {
                chunkSize = (bufferSize - bytesRead);
            }
            fs.read(fd, buffer, bytesRead, chunkSize, bytesRead);
            bytesRead += chunkSize;
        }
        console.log(buffer.toString('utf8', 0, bufferSize));
        fs.close(fd);
    });
});
like image 186
JD Byrnes Avatar answered Sep 23 '22 13:09

JD Byrnes


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.

like image 28
Geoff Chappell Avatar answered Sep 23 '22 13:09

Geoff Chappell


I used the @user1256169 example from above to create what I needed. Here I'm using async.whilst to handle the async control flow more cleanly. At the top of the example I'm reading the file and its stats synchronously, but that can be changed if there is a need to.

var fs = require('fs');
var async = require('async');

var fd = fs.openSync('/path/to/cat.png', 'r');
var stats = fs.fstatSync(fd);


var bufferSize = stats.size,
    chunkSize = 512,//bytes
    buffer = new Buffer(bufferSize),
    bytesRead = 0;

async.whilst(
    function () {
        return bytesRead < bufferSize;
    },
    function (done) {
        if ((bytesRead + chunkSize) > bufferSize) {
            chunkSize = (bufferSize - bytesRead);
        }
        // fd, buffer, offset, length, position, callback
        fs.read(fd, buffer, bytesRead, chunkSize, bytesRead,
        function (err, bytes, buff) {
            if (err) return done(err);
            var buffRead = buff.slice(bytesRead, bytesRead+chunkSize);
            // do something with buffRead
            bytesRead += chunkSize;
            done();
        });
    },
    function (err) {
        if (err) console.log(err);
        fs.close(fd);
    }
);
like image 40
simo Avatar answered Sep 25 '22 13:09

simo