3

I'm using Node & Express & Sharp to:

Download an image as stream -> convert to webp -> response the user with the converted image

This code does the job :

import express   from 'express';
import * as url  from 'url';
const sharp = require('sharp');
var request = require('request');

app.get('*', function (req, res1)
{
    let imgPath = url.resolve('http://localhost', req.originalUrl);

    request({
                url     : imgPath,
                encoding: null
            }, function (err, res, bodyBuffer)
            {
                sharp(bodyBuffer)
                    .webp({quality: 90})
                    .toBuffer()
                    .then((myBuffer) =>
                          {
                              res1.writeHead(200, {'Content-Type': 'image/webp'});
                              res1.end(myBuffer, 'binary');
                          }).catch(error=>console.log(error));;
            });

});

app.listen(app.get('port'), function ()
{
    console.log('app running on port', app.get('port'));
});

I must say that code does work (an image as webp is served) , but I do get an exception after image is served :

app running on port 5000
[Error: Input buffer contains unsupported image format]

Question:

Why do I get this error and how can I solve it ?

NB
I've also tried the solution suggested here, Without success : Cannot pipe, not readable

Royi Namir
  • 144,742
  • 138
  • 468
  • 792

0 Answers0