Home > node.js > node.js fs.read() example

node.js fs.read() example

May 12Hits:1
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,"") console.log('GET') 

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

Why does chrome browser not show any output?

[email protected]:~/http$ node server.js  GET ./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.

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,
            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));

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;

    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;
    function (err) {
        if (err) console.log(err);

Since you've designed your app to process files one after another (synchronously), you need to use fs.readSync() but be warned that, while your app is reading a file in this way, it cannot do anything else.

A better approach would be to process the files in the "node way", that is asynchronously.

-- node.fs - one line, no waiting


Related Articles

  • Receiving error "A new record cannot be created. Node is a CNAME DNS record." when adding an MX record to Windows Server 2003 DNSMay 19

    we're trying to add an MX record to a Windows Server 2003 DNS server and getting the following error: "A new record cannot be created. Node is a CNAME DNS record." The domain is xyz.com. We create a new MX record and leave the "Host or chil

  • puppet node update at willMay 28

    What is the preferred method (or any for that matter) to tell puppet nodes to update when I make a change to site.pp for instance and need it deployed right away? I have thought of pssh-ing to the nodes and executing 'sudo puppetd --test' on each one

  • Automatically name an XP node from DHCP/DNSJune 4

    So one of the things that many (most?) Linux distributions can do is to either accept the dhcp option telling it what its node name should be, and/or do a reverse-lookup on the ip address it has been given via dhcp and apply its initial node name fro

  • puppet - define wildcard host in nodes.ppJune 4

    Is there a way to define wildcard hosts in puppetmasterĀ“s nodes.pp say i want all the hosts in one domain to receive a set of classes can i do something like: # nodes.pp # node basenode { include admina, adminb, admic } node "*.acme.com" { inclu

  • AWS Autoscaling issue with existing nodes in ELBJune 24

    I already have a ELB setup called MyLoadBalancer. I already have 2 nodes running on it with health checks (that checks a URL on the node to see if they are up) Created an autoscaling group (min 2, Max 10) Associated launchconfig mylaunchconfig that p

  • PCI-Express hierarchical architecture - where is the weakest node

    PCI-Express hierarchical architecture - where is the weakest nodeJune 26

    I'm wondering about the fact, that PCI architecture is hierarchical. Therefore, even if I have two PCIx4 slots, it's possible that I will not be able to fully utilize it, because the slots will connect in one node, which can have bandwidth insufficie

  • EC2 Traffic Between NodesJune 29

    By default, all ports are closed in EC2 until a user opens them up. I would like to keep this behavior but also open up all ports for internal usage (that is, EC2 nodes can communicate with each other on any port but not with the outside world). The

  • File Not Found for All Nodes in DrupalJuly 10

    My Drupal installation is having all links to nodes show up with a File Not Found error. Admin pages and such are ok, it's just pages with ?q=node/# I tried using Update.php to fix anything, but that didn't do it. Any other ideas? --------------Solut

  • How can I shut down (power off) cluster nodes during low load?July 13

    I'm developing software for the energy consulting business and in monitoring energy use in datacenters, I've noticed that the typical electric load "pattern" of a datacenter is just a flat line, because all the gear runs 24/7. If you compare thi

  • Vista tree view: disabling fade-out of tree nodesJuly 20

    When using a tree view (e.g. the folders list in Explorer), there are little triangles next to each node which contains sub-nodes. Unfortunately, they fade out as soon as one leaves the tree view with the mouse. This is very annoying, because one can

  • Drupal book outline only for nodes of specific typeJuly 27

    I have a Drupal site, which allows all logged in users to add and edit book pages. On the permissions page I have enabled they may also administer book outlines. The problem is that they can do this for all nodes, even forum topics and pages, and blo

  • Extremely huge nodes in Drupal. Should I do this?July 28

    I'm making a website in Drupal, which allows users to add walkthroughs for games. However, those walkthroughs can contain over 500,000 words. Should I do this? Can both Drupal and MySQL handle that much of characters? If not, what can I use instead?

  • Node Affinity and Load-Balancing - drawbacks?July 31

    What are the main drawbacks if we choose to use Node-Affinity (sticky MAC) with a load-balanced IIS solution? We are thinking about using in-memory Session State, so need to evaluate the options. Cheers Duncan --------------Solutions------------- You

  • Change owner of LVM LV's device node in RHEL5August 7

    First time using serverfault, please excuse any breaches of etiquette. I've created several LVM2 logical volumes in local storage on a server, and would like one of the device nodes - not the filesystem or mount point - to be owned by a user/group ot

  • Adding a 3rd node to an existing 2 node Windows 2003 cluster with Dissimilar HardwareAugust 24

    We have an older cluster using 2 Dell 6850's with 64 gigs of RAM. We are going to be upgrading the cluster to use 2 Dell R900's with 128 gigs of RAM. However, the plan to upgrade is going to involve having all 4 nodes in the cluster at the same time.

  • Keeping web farm in sync with other "nodes" windows 2003August 28

    I'm shortly going to be setting up our eCommerce site on 3 web servers, all running Windows Server 2003 We also use a hardware load balancer. What is the best way of keeping all 3 in sync? Ie: our 3 servers are: Web1 Web2 Web3 How can i make web2 and

  • Will restarting the "Cluster Service" on the active cluster node cause any outages?September 14

    I have a Windows Server 2003 Enterprise active/passive cluster running SQL Server 2005. The "Cluster Service" service won't start on my passive node, and I'm pretty sure it's what's described in this MS KB article. However, it prescribes restart

  • How can I tell SGE to stop assigning work to a compute node?September 15

    I want to mark a node (or set of nodes) as "offline" in the sense that I want Sun Grid Engine to stop assigning new work to them. This would be for some kind of maintenance work on the nodes themselves. The nodes should finish whatever work they

  • Puppet: Node name seems dependent on reverse dns?September 17

    I seem to be running into a little bit of a problem understanding how to get this to work. I have a new server I'm building sitting behind the office NAT at work, its reverse dns maps to office.mydomain.com, but I want the machine to be ns2.mydomain.

  • Failure situation in a 2 node load balancerOctober 8

    I am planning to setup a 2-node active-active app server cluster for a HA scenario. Now, when one of the server goes down, the entire site load will come on one server till the other server comes up again. what is the best option in such a failure sc

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.430 s.