我正在使用Node&Express和Sharp进行以下操作:
下载图像作为流->转换为webp->响应用户 带有转换后的图像
此代码可以完成工作:
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'));
});
我必须说代码可以工作(提供了webp的图像),但是提供图像后确实出现了异常:
app running on port 5000 [Error: Input buffer contains unsupported image format]
问题:
为什么会出现此错误,我该如何解决?
NB
I've also tried解决方案建议here,但未成功:
Cannot pipe, not readable