Tuesday, August 26, 2014

upload file through http server with request, response using node.js

1. install node-v0.10.31-x86.msi

2. save sample.js

var http = require('http');
var fs = require('fs');

http.createServer(function (request, response) {
    var newFile = fs.createWriteStream("readme_copy.md");
    var fileBytes = request.headers['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");
    });
   
    request.on('end', function() {
        response.end('Uploaded!');
    });
}).listen(8080);
console.log("Listening on port 8080...");

3. open command prompt, write "node sample.js" then enter to run js file on jvm

4. install Git-1.9.4-preview20140815.exe

5. run "Git Bash.exe" as "C:\Program Files (x86)\Git\bin\sh.exe" --login -i

6. white "curl --upload-file readme.md http://localhost:8080" then enter to upload file through http server


result



No comments: