3

I'm having trouble serving binary data from node. I worked on a node module called node-speak which does TTS (text to Speech) and return a base64 encoded audio file.

So far I'm doing this to convert from base64 to Buffer/binary and then serve it:

// var src = Base64 data
var binAudio = new Buffer(src.replace("data:audio/x-wav;",""), 'base64');

Now I'm trying to serve this audio from node with the headers like so:

res.writeHead(200, {
  'Content-Type': 'audio/x-wav',
  'Content-Length': binAudio.length
});

And serving it like so:

res.end(binAudio, "binary");

But its not working at all. Is there something I havnt quite understood or am I doing something wrong, because this is not serving a valid audio/x-wav file.

Note: The Base64 data is valid i can serve it like so [see below] and it works fine:

// assume proper headers sent and "src" = base64 data
res.end("<!DOCTYPE html><html><body><audio src=\"" + src + "\"/></body></html>");

So why can I not serve the binary file, what am I doing wrong?

Christopher
  • 1,358
  • 17
  • 32

1 Answers1

3

Two things are wrong.

  1. not Conetnt-Length, it's Content-Length
  2. res.end(binAudio, "binary"); is wrong. Use res.end(binAudio);. With "binary", it expects a string - binary is a deprecated string encoding in node, use no encoding if you already have a buffer.
thejh
  • 44,854
  • 16
  • 96
  • 107
  • Thanks for noticing the typo, i should have seen that when pasting the code into SO. and thanks for the answer. I'm still having issues but at elast its serving a valid `audio/x-wav` file now. – Christopher Nov 30 '11 at 17:00
  • thanks for your help, if you have a moment could you have a look at this question: http://stackoverflow.com/q/8354936/371040 – Christopher Dec 02 '11 at 10:30