Skip to content

Instantly share code, notes, and snippets.

@CGastrell
Created June 25, 2014 22:00
Show Gist options
  • Select an option

  • Save CGastrell/2df9f86a7deee5afd223 to your computer and use it in GitHub Desktop.

Select an option

Save CGastrell/2df9f86a7deee5afd223 to your computer and use it in GitHub Desktop.
Mini server para recibir un archivo zip
//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