Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot pipe request after body-parser #94

Closed
fschwic opened this issue Apr 24, 2015 · 5 comments
Closed

cannot pipe request after body-parser #94

fschwic opened this issue Apr 24, 2015 · 5 comments
Assignees
Labels

Comments

@fschwic
Copy link

fschwic commented Apr 24, 2015

I'm using body-parser to limit the size of a POST body. Adding the following line a request with some 3MB stucks on '100 continue'. No data is written.

app.use(bodyParser.raw({ limit: '5mb' }));

A request with 400kB is successful. Removing the above line the 3MB request (and larger requests) work fine.
I'm using version 1.12.3. Maybe I have overseen something!?

@dougwilson dougwilson self-assigned this Apr 24, 2015
@dougwilson
Copy link
Contributor

Hm, I'm not sure. I just made the following app and it works fine with a Expect: 100-continue:

var bodyParser = require('body-parser')
var express = require('express')

var app = express()
.use(bodyParser.raw({ limit: '5mb' })
use(function (req, res) {
  res.end('I saw ' + rew.body.length + ' bytes in the body')
})

express.listen(3000)

It's very important that you do not have a checkContinue listener on your server (https://nodejs.org/api/http.html#http_event_checkcontinue), because that is most likely what would cause your request to hang on a 100 continue. You can also see if the request even gets into this module but running your app with the environment DEBUG=body-parser:* and pasting what it prints to STDERR here.

@fschwic
Copy link
Author

fschwic commented Apr 24, 2015

With DEBUG set I get the following (incl. request headers).

body-parser:raw content-type "application/octet-stream" +33s
body-parser:raw content-encoding "identity" +0ms
body-parser:raw read body +0ms
body-parser:raw parse body +8ms
{ 'user-agent': 'curl/7.35.0',
  host: 'localhost:8081',
  accept: '*/*',
  'content-type': 'application/octet-stream',
  'x-allowed-downloads': '30',
  'content-length': '3744928',
  expect: '100-continue' }

The +33s is after several requests. After starting the node app there appear 0ms with the first request.

The request I do with curl:

curl -i -X POST -H "Content-Type: application/octet-stream" -H "X-Allowed-Downloads: 30" 'http://localhost:8081/uploads' --data-binary @Downloads/some.zip 

Reading from the request, writing to a file in this way:

var writeable = fs.createWriteStream("./uploads/" + file);
  writeable.on('finish', function(){
    res.status(204).end();
  });
  var readable = req;
  readable.pipe(writeable);

Thanks for the quick reply!

@dougwilson
Copy link
Contributor

Ah, thanks, that fss usage is where your problem is. You can only read a stream one time in Node.js. req was already read into req.body ny this module, so your req.pipe is what's hanging. You just need to do writable.end(req.body) and remove readable.pipe(writeable).

@dougwilson dougwilson changed the title bodyParser.raw() breaks 100-continue cannot pipe request after body-parser Apr 24, 2015
@fschwic
Copy link
Author

fschwic commented Apr 24, 2015

With the answer it is so obvious. But I never had found it alone.
:-)
Very special thanks!!!

@dougwilson
Copy link
Contributor

My pleasure :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants