I'm trying to send an image by HTTP with node.js.
I can send HTML, text but, not images.
Here's a sample of what I'm trying to do:
request(options, function(error, response, body) {
http.createServer(function(req, res) {
res.statusCode = response.statusCode;
res.writeHead(res.statusCode, { 'Content-Type': response.headers['content-type'] });
res.write(body);
res.end();
}
});
and all I get is:
The same happens when I try an icon...
If I wget
the image I get the right content I guess, but it doesn't show up in the image viewer either... I don't know how to fix this.
Any idea?
Thanks!
Make sure body
is a buffer and not an encoded representation of the image (unless it's encoded in such a way that common browsers can decode it if you send out an appropriate Content-Encoding
header, in which case you should (you guessed it!) send out the appropriate header.
In particular, don't try to read the image with fs.*
methods where you asked for the responses to be UTF-8 and then try to send it "raw".