Pyetras
Pyetras

Reputation: 1502

req.pause() on a finished request pauses the next request

I have a really simple file upload example

var http = require('http');
http.createServer(function(req, res){
  console.log(req.method);
  if (req.method == 'GET'){
    res.writeHead(200);
    res.write('<html><head></head><body><form method="POST" enctype="multipart/form-data"><input type="file" id="f" name="f"><input type="submit"></body></html>')
    res.end();
  }else if(req.method == 'POST'){
    req.pause();
    res.writeHead(200);
    res.end();
  }else{
    res.writeHead(404).end();
  }
}).listen('8081');

What I want to do is pause the upload. While it works fine with large files, the small ones (<= 100kB), that are probably sent along with the request in a single part are not paused (that's fine and understandable), but instead the next request to the server is paused (i.e. when I try to load the page, it never reaches the console.log(req.method) part, but when I refresh again it's back to normal), which is definitely not fine.

It seems like an error that would pop out once in a while, so I was suprised when I didn't find any complaints about it. What are your thoughts/possible explanation/workaround/fix suggestions? For now I check whether the file's size is below a certain threshold, but it doesn't look very safe or ellegant.

Upvotes: 1

Views: 2005

Answers (1)

dhruvbird
dhruvbird

Reputation: 6189

I am of the opinion (could be wrong) that req.pause causes the underlying socket to be paused. A way to verify this claim would be to add the header Connection: Close in the res.writeHead(200); which is just after the line req.pause();.

Now, if everyting start working fine (next request is not paused), you can say that the underlying socket was paused and was being reused for the next request.

Upvotes: 1

Related Questions