Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • fs module in nodejs

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 408
    Comment on it

    Welcome to FindNerd,

    Today we are going to discuss an example of nodejs modules. We have already discussed the nodejs in-built modules in our previous blog. click here  to check the blog. We are going to take an example of fs module in nodejs.

    Please take a look.

     

    // file name control.js
    
    var mod_fs = require('fs');  //loaded fs module
    
    var mod_stream = mod_fs.createReadStream('nerd.json'); // created read stream for file nerd.json
    
    mod_stream.pause();  // pasued the stream
    
    console.log('Stream has been paused'); //Message for pasue action
    
    setTimeout(function() {
    	console.log('Stream resuming now...');       // After three seconds resume the stream 
    	mod_stream.resume();
    }, 3000);
    
    mod_stream.on('data', function (material) {
    	console.log('----------------started----------------');
    	console.log(material.toString());                       // get the content
    	console.log('----------------ended----------------');
    });
    
    
    mod_stream.on('data', function (material) {
    	console.log('CONTENT LENGTH WAS: ' + material.length);  // get the length of file
    });
    
    mod_stream.on('end', function  () {
    	console.log('----------------ended----------------');  // End the process
    });

     

    // nerd.js
    {
    	"347": {
    		"number": 347,
    		"origin": "SRE",
    		"destination": "UMA",
    		"departs": "12:00 AM",
    		"arrives": "12:00 PM"
    	},
    	"734": {
    		"number": 734,
    		"origin": "DDN",
    		"destination": "BRN",
    		"departs": "9:00 AM",
    		"arrives": "7:30 PM"
    	}
    }

     

    Output

     

    A) First of all we have loaded the fs module. After that, we have created read stream and we are reading nerd.json file.

     

    B) Then we have paused the stream and showing a message for it. After that, we are resuming stream after 3 minutes.

     

    C) We are getting content and its length using toString and length function inside on event.

     

    D) We have also shown a content of nerd.js file and you can see the screenshot of output as well.


    Thank you for being with us!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: