Created
June 25, 2014 22:00
-
-
Save CGastrell/2df9f86a7deee5afd223 to your computer and use it in GitHub Desktop.
Mini server para recibir un archivo zip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //para probar, correr con node y en otra consola consolear: | |
| //curl --request PUT 'http://localhost:8000/put' --upload-file archivo.zip | |
| var http = require('http'); | |
| var fs = require('fs'); | |
| http.createServer(function(request,response){ | |
| response.writeHead(200); | |
| var destinationFile = fs.createWriteStream("destination.zip"); | |
| request.pipe(destinationFile); // aca esta la magia, use wisely | |
| var fileSize = request.headers['content-length']; | |
| var uploadedBytes = 0 ; | |
| request.on('data',function(d){ | |
| // response.write('wait for it...'); | |
| console.log('receiving file...'); | |
| //esto es todo facha para q el client sepa que esta pasando | |
| uploadedBytes += d.length; | |
| var p = (uploadedBytes/fileSize) * 100; | |
| response.write("Uploading " + parseInt(p)+ " %\n"); | |
| }); | |
| request.on('end',function(){ | |
| response.end("File Upload Complete"); | |
| console.log("File received: ", fs.statSync('destination.zip')); | |
| }); | |
| }).listen(8000,function(){ | |
| console.log("server started"); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment