如何在Node.js中将缓冲区转换为流 [英] How to convert buffer to stream in Nodejs
问题描述
我遇到了在Node.js中将缓冲区转换为流的问题,这是代码:
I confront with a problem about converting buffer into stream in Nodejs.Here is the code:
var fs = require('fs');
var b = Buffer([80,80,80,80]);
var readStream = fs.createReadStream({path:b});
代码引发异常:
TypeError: path must be a string or Buffer
但是文档
fs.createReadStream(path [,options])
path< string> | < Buffer> | < URL>
选项< string> | <对象>
fs.createReadStream(path[, options])
path <string> | <Buffer> | <URL>
options <string> | <Object>
任何人都可以回答这个问题吗?非常感谢!
Anybody could answer the question? Thanks very much!
推荐答案
fs
旨在对文件系统。
fs.createReadStream()
期望将路径作为它的第一个参数,该路径可以是文件名,文件的url或文件缓冲区(如此处所述)。您正在传递对象 {path:b}
。
fs
is intended to perform operations on the file system.
fs.createReadStream()
expect a path as it first argument which could be a file name, an url of a file or a file buffer (as it is nicely explained here). You are passing an object {path:b}
.
如果尝试使用 createReadStream()
语法正确,错误变得更加明显:
If you try with the createReadStream()
correct syntax, the error become more clear:
var fs = require('fs');
var b = Buffer([80,80,80,80]);
var readStream = fs.createReadStream(b);
console.log(readStream);
// ReadStream {
// ...
// path: <Buffer 50 50 50 50>,
// ...
// }
//
// Error: ENOENT: no such file or directory, open 'PPPP'
这篇关于如何在Node.js中将缓冲区转换为流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!