i installed sharp module based on the 'Read Me' document, simple test is ok, but i get the below error when excute my test code, lack of importing some other module?
My Ubuntu env(Ubuntu 14.04 LTS)
uname -a
Linux mao 3.13.0-34-generic #60-Ubuntu SMP Wed Aug 13 15:45:27 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Test Code:
var sharp = require('sharp');
var http = require('http');
require('stream');
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'image/webp'});
sharp('input.jpg').rotate().resize(200).webp().pipe(response);
}).listen(8000);
// Create HTTP server that always returns auto-rotated 'input.jpg',
// resized to 200 pixels wide, in WebP format
The error log:
sharp('input.jpg').rotate().resize(200).webp().pipe(response);
^
TypeError: Object [object Promise] has no method 'pipe'
at Server.<anonymous> (/home/len/temp/nodejis/webp.js:10:50)
at Server.emit (events.js:98:17)
at HTTPParser.parser.onIncoming (http.js:2108:12)
at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23)
at Socket.socket.ondata (http.js:1966:22)
at TCP.onread (net.js:527:27)
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_lookup: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
(.:6385): GLib-CRITICAL **: g_hash_table_insert_internal: assertion 'hash_table != NULL' failed
I read over the issue #30 a moment ago , so i think is it the problem of the sharp's version? Has not the pipe() method deployed to Ubuntu repository? i installed the sharp module using the command 'npm install sharp'
Stream support will be available in the forthcoming 0.6.0 version. The most recent version published to _npm_ is 0.5.2.
If you'd like to use the Stream API sooner, you can install this module from the Github master branch by using:
npm install lovell/sharp
or, in the dependencies section of package.json, use:
'sharp': 'lovell/sharp'
Hope this helps.
Thanks, the test above is ok after reinstalling the sharp; but another problem when i replace input.jpg to stream, the HTTP request from browser can not return, the code is below:
var sharp = require('sharp');
var http = require('http');
var inputStream = http.get('http://i.ebayimg.com/00/s/MTYwMFgxNjAw/z/C~EAAOSw~CRToq89/$_58.JPG');
var transformer = sharp().resize(200);
http.createServer(function (req, res) {
var outputStream = res;
inputStream.pipe(transformer).pipe(outputStream); // coding according to the doc
}).listen(8000);
I am newer in node dev area, before i writed a java library using im4java based on Graphicsmagick, and the function is similar to the Sharp but webp encode.
http.get() returns a request object rather than a Readable Stream response.
I recommend you use the request HTTP client.
You'll need to move inputStream and transformer inside the scope of the closure passed to createServer().
var request = require('request');
var sharp = require('sharp');
http.createServer(function (req, res) {
var inputStream = request('http://i.ebayimg.com/00/s/MTYwMFgxNjAw/z/C~EAAOSw~CRToq89/$_58.JPG');
var transformer = sharp().resize(200);
inputStream.pipe(transformer).pipe(res);
}).listen(8000);
Thanks @lovell ; It works!
Most helpful comment
http.get()returns a request object rather than a Readable Stream response.I recommend you use the request HTTP client.
You'll need to move
inputStreamandtransformerinside the scope of the closure passed tocreateServer().