Nodejs trying to read jpg data stream from return function

I am trying to get poster data back from omdb API found on github .

I am getting all other information about the movie, but I am struggling with threads as I think you should get a poster from this function.

The code in the omdb API looks like this:

// Get a Readable Stream with the jpg image data of the poster to the movie,
// identified by title, title & year or IMDB ID.
module.exports.poster = function (options) {
    var out = new stream.PassThrough();

    module.exports.get(options, false, function (err, res) {
        if (err) {
            out.emit('error', err);
        } else if (!res) {
            out.emit('error', new Error('Movie not found'));
        } else {
            var req = request(res.poster);
            req.on('error', function (err) {
                out.emit('error', err);
            });
            req.pipe(out);
        }
    });

    return out;
};

      

How can I get a poster? I call this with omdb.poster (options), however I'm not sure what the options should be.

If anyone could help me or point me in the right direction, I would be grateful!

+3


source to share


1 answer


You need to read and then write the stream to something. The example below will write a JPEG file to your file system containing the poster.



const omdb = require('omdb');
const fs = require('fs');
const writeStream = fs.createWriteStream('test.jpg')

omdb.poster({ title: 'Saw', year: 2004 })
    .on('data', (data) => {
        writeStream.write(data)
    })
    .on('end', () => {
        writeStream.end();
    });

      

+2


source







All Articles