Stream a file from NodeJS

278 views Asked by At

I am using weed-fs to store files and trying to stream file to client using below code

var fileName = [__dirname, '/uploads/', req.params.id, ".png"].join('');

try {
    var writeStream = fs.createWriteStream(fileName);
    weedfs.read(req.params.id, writeStream);
    writeStream.on("close", function () {
        var readStream = fs.createReadStream(fileName);
        response.pipe(readStream);
        response.on('error', function (err) {
            readStream.end();
        });
        response.on('end', function (err) {
            removeFile(fileName);
        });
    });

} catch (e) {
    res.send(404);
}

But I am getting error Error: connect ECONNREFUSED

If I look at the weedfs read method, I found below code

read: function(fid, stream, cb) {
    var ins = this;
    if ((typeof stream == "function") && !cb) {
        cb     = stream;
        stream = false;
    }

    this.find(fid, function(pub) {
        if (pub[0]) {
            if (stream) {
                ins.http(pub[0]).pipe(stream);
        ...... 

Am I doing something wrong. Please help me to fix this issue.

0

There are 0 answers