1

I'm using "express": "^4.13.3" on node 6.9.0

When i try to pipe data a jpeg image:

const path = config.storageRoot + '/' + req.params.originalFileName;
var mimetype = mime.lookup(req.params.originalFileName);
res.writeHead(200, { 'Content-Type': mimetype});  
fs.createReadStream(path).pipe(res);

i get xml data inside the result:

<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.5-c014 79.151739, 2013/04/03-12:12:15        ">

When i use res.end with the result from a fs.readFile instead, the binary content is formatted correctly.

What am i doing wrong?

1 Answers1

1

Take a look how I'm piping files in the examples in this answer:

It's something like this:

// 'type' is the MIME type
var s = fs.createReadStream(file);
s.on('open', function () {
    res.set('Content-Type', type);
    s.pipe(res);
});
s.on('error', function () {
    res.set('Content-Type', 'text/plain');
    res.status(404).end('Not found');
});

So I'm just setting the header to be posted by Express instead of posting the headers explicitly. Also I'm handling the stream events. Maybe you should try doing it similarly because the way I did it seems to work, according to Travis:

Another thing in addition to handling the stream events and errors would be to make sure that you have the correct encoding, permissions etc. I don't know what result are you expecting and what that XML means or where it comes from, but handling the stream events may tell you more about what is happening.

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177