0

I (as the client) am trying to post an image with restify, and the server just needs to save it.

req.pipe(fs.createWriteStream('test.jpg'));

is not working. An empty file is created but nothing more. It works when I copy req.body into a buffer and then fs.writeFile(...). I have also tried req.body.pipe, but this throws an error.

MForMarlon
  • 865
  • 9
  • 24
sil0r
  • 51
  • 1
  • 1
  • 2
  • Just wondering, is this for gridfs? Also, are you listening for the [finish](http://nodejs.org/docs/v0.10.36/api/stream.html#stream_event_finish) event on the writeStream? You have to make sure that all of the data passes through the stream before sending a response. – MForMarlon Mar 04 '15 at 18:08

2 Answers2

1

You're probably using a body parser middleware that is already reading all of the data from the request so there is nothing left to read. Try adjusting the placement of your route handler and/or body parsing middleware if you want to read directly from the request object.

However, that will only work if the request contains only the image data. Typically a request is formatted as multipart/form-data if it contains at least one file, so you cannot just pipe the request and expect image data only.

mscdex
  • 104,356
  • 15
  • 192
  • 153
0

So something else in your middleware chain, probably restify.bodyParser(), is already streaming the request body into a buffer or string as req.body and you can't stream something twice. Find the middleware and disable it for this route if you want to handle the streaming straight to the filesystem yourself.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274