如何通过设置正确的偏移量和位置来读取文件并使用手动缓冲写入Nodejs中的响应?

我想以64字节的时间间隔读取一个文件。 我也不想使用任何interanlly实现缓冲的function。 我想手动缓冲。 所以我开始使用fs.read()。 我努力尝试,但我真的不知道如何设置位置 ,告诉从哪里读取文件和缓冲区中的偏移量以开始写入。
所以我find了一些资源,并开始自己实施。 但是我所做的似乎是错误的。 请在下面find我的代码。

app.get('/manualBufferAnother', function (req, res, next) { var filePath = path.join(__dirname, 'Koala.jpg'); console.log("FilePath is: "+filePath); var fileName = path.basename(filePath); var mimeType = mime.lookup(filePath); var stat = fs.statSync(filePath); res.writeHead(200, { "Content-Type": mimeType, "Content-Disposition" : "attachment; filename=" + fileName, 'connection': 'keep-alive', "Content-Length": stat.size, "Transfer-Encoding": "chunked" }); fs.open(filePath, 'r', function(err, fd) { var completeBufferSize = stat.size; var offset = 0; //is the offset in the buffer to start writing at var length = 511; //is an integer specifying the number of bytes to read var position = 0; //is an integer specifying where to begin reading from in the file. If position is null, data will be read from the current file position var buffer = new Buffer(completeBufferSize); buf(res,fd,offset,position,length,buffer,stat); }); }); var buf = function(res,fd,offset,position,length,buffer,stat) { if(position+buffer.length < length) { fs.read(fd,buffer,offset,length,position,function(error,bytesRead,bufferr { res.write(bufferr.slice(0,bytesRead)); console.log("Bytes Read: "+bytesRead); position=position+bufferr.length; buf(res,fd,offset,position,length,bufferr,stat); }) } else { fs.read(fd,buffer,offset,length,position,function(error,bytesRead,bufferr) { console.log("Bytes Read in else: "+bytesRead); res.end(bufferr.slice(0,bytesRead)); fs.close(fd) }) } } 

我知道这个代码是做了这么多错误的事情。 但我不知道正确的方法。 我应该使用任何循环来设置和存储位置和偏移值吗? 如果你提供给我很好的参考,会有帮助吗?

这里是一个例子:

 res.writeHead(...); var SIZE = 64; // 64 byte intervals fs.open(filepath, 'r', function(err, fd) { fs.fstat(fd, function(err, stats) { var bufferSize = stats.size; var buffer = new Buffer(bufferSize), var bytesRead = 0; while (bytesRead < bufferSize) { var size = Math.min(SIZE, bufferSize - bytesRead); var read = fs.readSync(fd, buffer, bytesRead, size, bytesRead); bytesRead += read; } res.write(buffer); }); }); 

我应该使用任何循环来设置和存储位置和偏移值吗?

是的,你可以但要小心。 在Node.js中,大多数文件系统函数是asynchronous的 (非阻塞的)。 正如你可能已经意识到的那样,将一个asynchronous函数放在循环中会导致问题。 您可以通过查看Node.js文档并检查是否存在callback参数来判断函数是否asynchronous。 所以在循环中使用read是不好的。 我们可以改为使用readSync 。 这是同步(阻塞),类似于C的read()函数(也是阻塞)。

我真的不知道如何设置位置,告诉从哪里读取文件和缓冲区中的偏移量以开始写入

readSync函数的参数控制从文件中读取的位置以及要写入目标缓冲区的位置。

 // /----- where to start writing at in `buffer` fs.readSync(fd, buffer, offset, length, position) // \------- where to read from in the // file given by `fd` 

注意:上面的风格对于C来说是惯用的,但是在Javascript中被认为是不好的做法 – 代码不能很好地扩展。 一般来说,你不需要使用同步函数,因为它阻塞了Javascript使用的单线程执行(也就是“ 阻塞事件循环 ”)。
从Express.js开始 :

同步函数和方法将执行过程捆绑在一起直到它们返回。 对同步函数的单个调用可能会在几微秒或几毫秒内返回,但是在高stream量的网站中,这些调用会加起来并降低应用程序的性能。 避免在生产中使用它们。 虽然Node和许多模块提供了其function的同步和asynchronous版本,但在生产中始终使用asynchronous版本

使用.pipe()Streams (asynchronous样式)通常是如果你需要最常用的和高性能代码的方法。 对不起,这样说:没有官方的资源/stream行网站会通过C风格的阻塞和缓冲来描述使用同步函数的文件操作,因为在Node.js中是不好的做法。