storykim
storykim

Reputation: 93

what's wrong with my node.js stream code?

I have a m4a audio file in my server, and I want to get it on my device. I can get a file with this code, but I cannot play it on my phone and PC.

I think that the file was transferred incorrectly. Can you give me some advice? (there are no error or warning messages on my server)

app.get('/upload/:file',function(req,res){
  console.log('download');
  var filename = req.params.file;
  var path = './disk/'+filename;

  var stat = fs.statSync(path);

  res.writeHead(200, {
    'Content-Type': 'audio/m4a',
    'Content-Length': stat.size
  });

  var readStream = fs.createReadStream(path);
  readStream.setEncoding('utf8');
  readStream.pipe(res);
  console.log('done');
})

Upvotes: 0

Views: 341

Answers (1)

mscdex
mscdex

Reputation: 106726

Remove this line:

readStream.setEncoding('utf8');

and the binary data will pass through un-modified (with this line it's currently being interpreted as utf-8 textual data).

Upvotes: 3

Related Questions