0

I am just beginning with nodejs. The following code makes a copy of the file I send via parameter in the curl.

For example curl --upload-file 11.png localhost:8080 In good theory this file, needs to have a new name that will be 111.png according to my code How do I get this name from the headers?

var http=require('http');
var fs = require('fs');
http.createServer(function(request,response){
    var name = 'content-encoding';
    var encoding = request.headers[name];
    var newFile=fs.createWriteStream(encoding+"1"); 
    var size=request.headers['content-length'];
    var upload=0;
    request.pipe(newFile);
    request.on('data',function(chunck){
        upload+=chunck.length;
        var progress=(upload/size)*100;
        response.write("progress: "+parseInt(progress,10)+ "%\n");
    });
}).listen(8080);
console.log('Server Running');
Diego
  • 916
  • 1
  • 13
  • 36
  • possible duplicate of [get uploaded file name/path in node.js](http://stackoverflow.com/questions/12434069/get-uploaded-file-name-path-in-node-js) – Mukesh Soni Feb 21 '14 at 07:18
  • May I suggest using a web server framework like [Express](https://github.com/visionmedia/express) or [Hapi](https://github.com/spumko/hapi) which does this for you. – Hans Kristian Feb 21 '14 at 09:28
  • I'm not using no Framework. I don't want to use it. I am in learning prcess right now. How you can achieve this with out a framework – Diego Feb 21 '14 at 11:39
  • It's not the same question. The answer given is using a framework, for giving the solution. I want to get the information using pure node. – Diego Feb 21 '14 at 13:39
  • 1
    This is a pain to do in vanilla node, so nobody does. If you want to avoid frameworks, then use a module like [formidable](https://www.npmjs.org/package/formidable) or [multiparty](https://www.npmjs.org/package/multiparty), which are designed to make working with file uploads much simpler. Node is all about little modules with a single purpose, so using one of these is actually considered a good thing. – qubyte Feb 22 '14 at 01:23

0 Answers0