Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created November 18, 2015 16:37
Show Gist options
  • Save miguelmota/c2ae4b7251cf46673bac to your computer and use it in GitHub Desktop.
Save miguelmota/c2ae4b7251cf46673bac to your computer and use it in GitHub Desktop.
Response progress percentage in Node.js
http.createServer(function(request, response) {
var newFile = fs.createWriteStream('file.txt');
var fileByes = request.header['content-length'];
var uploadedBytes = 0;
request.pipe(newFile);
request.on('data', function(chunk) {
uploadedBytes += chunk.length;
var progress = (uploadedBytes / fileBytes) * 100;
response.write('progress: ' + parseInt(progress, 10) + '%\n')
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment