In this post we will take a look on uploading a file on a web server created using Node.js. stream in Node.js makes this task super easy to upload files or for that matter working with any data transfer in between server and client.

To upload file we will work with two modules http and fs. So let us start with loading these two modules in application,


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

Once modules are loaded go ahead and create web server


http.createServer(function(request,response){

 }).listen(8080);

So far we are good and now we wish to perform following steps,

  1. Create a destination write stream. In this stream content of uploaded file will be written.
  2. We want to write back to client percentage of data being uploaded

First requirement can be done using pipe. pipe is event of stream in Node.js. And request is a readable stream. So we will use pipe event to write request to readable stream.


var destinationFile = fs.createWriteStream("destination.md");
 request.pipe(destinationFile);

Second requirement is to write back in response percentage of data uploaded. To do that first read total size if the file getting uploaded. That can be done by reading content-length (line no 1 in below code snippet). After that in data event of request we will update uploadedBytes which is in the beginning zero (line no 2). In data event of request we are calculating percentage and writing it back in the response.


var fileSize = request.headers['content-length'];
 var uploadedBytes = 0 ;

request.on('data',function(d){

 uploadedBytes += d.length;
 var p = (uploadedBytes/fileSize) * 100;
 response.write("Uploading " + parseInt(p)+ " %\n");

});

Putting all together your app should contain below code to upload a file and response back percentage uploaded.


var http = require('http');
var fs = require('fs');
 http.createServer(function(request,response){
 response.writeHead(200);
 var destinationFile = fs.createWriteStream("destination.md");
 request.pipe(destinationFile);

var fileSize = request.headers['content-length'];
 var uploadedBytes = 0 ;

request.on('data',function(d){

 uploadedBytes += d.length;
 var p = (uploadedBytes/fileSize) * 100;
 response.write("Uploading " + parseInt(p)+ " %\n");

});

request.on('end',function(){
 response.end("File Upload Complete");
 });

}).listen(8080,function(){

 console.log("server started");

 });

On command prompt start server as below,

clip_image002

Now let us use curl –upload-file to upload a file on server.

clip_image004

As you see that while file is being uploaded percentage of data uploaded is returned back to client. So in this way you can upload a file to server created using Node.js. I hope you find this post useful. Thanks for reading.

Advertisements

5 thoughts on “How to upload file in Node.js

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s