如何使用Node.js 0.10中的读取流立即链接写入流?

时间:2022-06-01 22:02:05

The following line will download an image file from a specified url variable:

以下行将从指定的url变量下载图像文件:

var filename = path.join(__dirname, url.replace(/^.*[\\\/]/, ''));
request(url).pipe(fs.createWriteStream(filename));

And these lines will take that image and save to MongoDB GridFS:

这些行将获取该图像并保存到MongoDB GridFS:

 var gfs = Grid(mongoose.connection.db, mongoose.mongo);
 var writestream = gfs.createWriteStream({ filename: filename });
 fs.createReadStream(filename).pipe(writestream);

Chaining pipe like this throws Error: 500 Cannot Pipe. Not Pipeable.

链接管道像这样抛出错误:500无法管道。不可管。

request(url).pipe(fs.createWriteStream(filename)).pipe(writestream);

This happens because the image file is not ready to be read yet, right? What should I do to get around this problem?Error: 500 Cannot Pipe. Not Pipeable.

发生这种情况是因为图像文件尚未准备好进行读取,对吧?我该怎么做才能解决这个问题?错误:500无法管道。不可管。

Using the following: Node.js 0.10.10, mongoose, request and gridfs-stream libraries.

使用以下内容:Node.js 0.10.10,mongoose,request和gridfs-stream库。

3 个解决方案

#1


9  

request(url).pipe(fs.createWriteStream(filename)).pipe(writestream);

is the same as this:

与此相同:

var fileStream = fs.createWriteStream(filename);
request(url).pipe(fileStream);
fileStream.pipe(writestream);

So the issue is that you are attempting to .pipe one WriteStream into another WriteStream.

所以问题是你试图将一个WriteStream压缩到另一个WriteStream中。

#2


5  

// create 'fs' module variable
var fs = require("fs");

// open the streams
var readerStream = fs.createReadStream('inputfile.txt');
var writerStream = fs.createWriteStream('outputfile.txt');

// pipe the read and write operations
// read input file and write data to output file
readerStream.pipe(writerStream);

#3


3  

I think the confusion in chaining the pipes is caused by the fact that the pipe method implicitly "makes choices" on it's own on what to return. That is:

我认为链接管道的混乱是由于管道方法隐含地“自行选择”返回管道而引起的。那是:

readableStream.pipe(writableStream) // Returns writable stream
readableStream.pipe(duplexStream) // Returns readable stream

But the general rule says that "You can only pipe a Writable Stream to a Readable Stream." In other words only Readable Streams have the pipe() method.

但是一般规则说“你只能将可写流传输到可读流”。换句话说,只有Readable Streams具有pipe()方法。

#1


9  

request(url).pipe(fs.createWriteStream(filename)).pipe(writestream);

is the same as this:

与此相同:

var fileStream = fs.createWriteStream(filename);
request(url).pipe(fileStream);
fileStream.pipe(writestream);

So the issue is that you are attempting to .pipe one WriteStream into another WriteStream.

所以问题是你试图将一个WriteStream压缩到另一个WriteStream中。

#2


5  

// create 'fs' module variable
var fs = require("fs");

// open the streams
var readerStream = fs.createReadStream('inputfile.txt');
var writerStream = fs.createWriteStream('outputfile.txt');

// pipe the read and write operations
// read input file and write data to output file
readerStream.pipe(writerStream);

#3


3  

I think the confusion in chaining the pipes is caused by the fact that the pipe method implicitly "makes choices" on it's own on what to return. That is:

我认为链接管道的混乱是由于管道方法隐含地“自行选择”返回管道而引起的。那是:

readableStream.pipe(writableStream) // Returns writable stream
readableStream.pipe(duplexStream) // Returns readable stream

But the general rule says that "You can only pipe a Writable Stream to a Readable Stream." In other words only Readable Streams have the pipe() method.

但是一般规则说“你只能将可写流传输到可读流”。换句话说,只有Readable Streams具有pipe()方法。