This doesn't seem to work.
app.get('/image.jpg', function(req, res) {
res.contentType('image/jpeg');
res.end(data);
});
It returns a 200 but the response is empty. Thoughts? Thanks
ah, i think i know what it is. try res.contentType('jpeg'). the reason we have that special-case for a field, is that it does:
return this.header('Content-Type', mime.lookup(type));
so lookup() for 'image/jpeg', or any canonical mime type passed will become the default of "application/octet-stream"
i'll fix this though so you can pass a valid mime type to it
nvm it'll have to stay as-is I suppose, since even foo/barcould technically be a file path. i'd use res.header() directly, contentType is used in res.sendfile() etc that guess the content type based on the file's path, though it would be nice if contentType was a short-hand
Where does the mime package come from? None of the above propositions worked for me. To give you a bit more info, I'm trying to convert an image and serve it on the fly (i.e. without the need to save it to disk first).
I figured it out!
This line:
res.end(data);
needs to have the encoding parameter set:
res.end(data, 'binary');
This worked! Thanks!
weird :s you shouldn't have to do that if you have a buffer
You can use res.send() instead of res.end() as suggested by expressjs documentation
res.contentType('image/jpeg');
res.send(data);
Most helpful comment
I figured it out!
This line:
needs to have the
encodingparameter set:This worked! Thanks!