Skip to content

Instantly share code, notes, and snippets.

@jessemoon0
Created October 14, 2016 07:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessemoon0/a29031f16f153bde8fe93e93f193d3e1 to your computer and use it in GitHub Desktop.
Save jessemoon0/a29031f16f153bde8fe93e93f193d3e1 to your computer and use it in GitHub Desktop.
Testing Node: Uploading a file, see its progress % and creating an equal size .md file
var http = require('http');
var fs = require('fs');
http.createServer(function(request, response){
var newFile = fs.createWriteStream("big_copy.md"); //This is created automatically
var fileBytes = request.headers['content-length']; //Check how much bytes a file has
var uploadedBytes = 0;//Keep track of the bytes uploaded.
request.on('readable', function(){ //Each time information is read
var chunk = null;
while(null !== (chunk = request.read())){ //Reads each request's chunks
uploadedBytes += chunk.length;
var progress = (uploadedBytes/fileBytes) * 100; //Calculate upload progress
response.write("progress: " + parseInt(progress, 10) + "%\n"); //Show it in browser
}
});
request.pipe(newFile); //Takes care of the upload for us...
request.on('end', function(){
response.end('File Uploaded!');
});
}).listen(8080);
@jessemoon0
Copy link
Author

--> Test with curl --upload big.mp4 http://localhost:8080 where big.mp4 is a video named like that
--> To run curl command, must be on the container folder, file must name as in command and check copied file at the end
--> It only works with .md files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment