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>
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.
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.
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.
readFile. Returns the contents of the file named filename. If encoding is specified then this function returns a string. Otherwise it returns a buffer.
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);
});
});
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 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);
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With