Tuesday, 15 May 2018

004 Node.js File Handling Part 2

In this chapter , we will discuss the following topics related to File Handling
  1. Delete File
  2. Rename File
  3. Truncate File
  4. Copy File
Delete File

To delete a file with the File System module, you will use  fs.unlink() method.The fs.unlink() method deletes file.


Example

var fs = require("fs");

fs.unlink('C:\myTest.txt', function (err) {
  if (err) throw err;
  console.log('File deleted!');
}); 


Output :
'File deleted!'


Rename File

To rename file with the File System module, you will use  fs.
rename() method.The fs.rename() method rename file.

Example

var fs = require("fs");

fs.rename('myFile.txt', 'myFile2.txt', function (err) {
  if (err) throw err;
  console.log('File Renamed!');
})
 


Output :
'File Renamed!'



Truncate File

To rename file with the File System module, you will use fs.
ftruncate() method.The fs.ftruncate() method rename file.

Example

var fs = require('fs')
fs.truncate('input.txt', 0, function()

{
        console.log('File Truncated')
})
 


Output :
'File Truncated!'

Copy File

To Copy file with the File System module, you will use fs.
copyFile() method.The fs.copyFile() method Copy file.

Example

var fs = require('fs'); 
     
fs.copyFile('input.txt', 'DestinationFile.txt', (err) => { 
     if (err) throw err; 
      console.log('input.txt was copied to DestinationFile.txt'); 
}); 


Output :
'input.txt was copied to DestinationFile.tx'



https://nodejs.org/api/fs.html#fs_fs_readfile_file_options_callback



003 Node.js File Handling Part 1

File Handling 
The Node.js file system handle file in a smart way. Below is the operation that Node.js can handle on file.
  1. Create File
  2. Open File
  3. Read File
  4. Writing File
  5. Append File
  6. Delete File
  7. Rename File
  8. Upload File
  9. Truncate File
  10. Copy File
You need to add  Node File System (fs) module can be imported by  the following syntax.

var fs = require("fs")


Create Files


You can Create a new file using appendFile,open,writeFile method.

Below example show how to create a new file using the appendFile() method.

Example
var fs = require('fs');


fs.appendFile('myTest.txt', 'Hello World!', function (err)
{
  console.log('Your file created and content written!');
});


Output :'Your file created and content written!'
You have noticed that a new file created 'myTest.txt' , the file content
a string 'Hello World!'. 




Below example show how to create a new file using the open() method.
.open() method takes an extra argument for file opening mode.Here are some of those argument.

r:Open for reading.
r+:Open for reading and writing.
rs:Open for reading in synchronous mode.
rs+:Open for reading and writing in synchronous mode.
w:Open for writing.
w+:Open for reading and writing.



Example
var fs = require('fs');

var path = "C:\\Users\\myTest.txt";


fs.open(path, "w+", function(error, fd) {
  if (error) {
    console.error("open error:  " + error.message);
  } else {
    console.log("Successfully opened " + path);
  }
});


Output :'Successfully opened C:\\Users\\myTest.txt'



Below example show how to create a new file using the writeFile() method.

Example
var fs = require('fs');
fs.writeFile('myTest.txt', 'Hello World!', function (err)
{
  console.log('Your file created and content written!');
});


Output :'Your file created and content written!'


Open Files

You can open file by "open" method.

Method Signature: fs.open(path, flags[, mode], callback)
Parameters:

    path: file path with name.
    Flag: The flag to perform operation
    Mode: The mode for read, write or read write. 


Here are some of common Mode for file opening.


r:Open for reading.
r+:Open for reading and writing.
rs:Open for reading in synchronous mode.
rs+:Open for reading and writing in synchronous mode.
w:Open for writing.
w+:Open for reading and writing.


Example
var fs = require('fs');

var path = "C:\\Users\\myTest.txt";


fs.open(path, "w+", function(error, fd) {
  if (error) {
    console.error("open error:  " + error.message);
  } else {
    console.log("Successfully opened " + path);
  }
});


Output :'Successfully opened C:\\Users\\myTest.txt'

Read Files
You can read file from your computer using  fs.readFile() method.

Example
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
   console.log(data.toString());
});

 

Output :
Hello World 


Writing Files
You can write data to a file from your computer using  fs.readFile() method.If file already exists then it overwrites the existing content otherwise it creates a new file and writes data into it. 


Example
var fs = require('fs');
fs.writeFile('myTest.txt', 'Hello World!', function (err)
{
  console.log('Your file created and content written!');
});


Output :'Your file created and content written!'

Append Files
You can append data to a file from your computer using   
fs.appendFile() method.


Example  
 

var fs = require("fs");
fs.appendFile('input.txt','Hello World!', function (err, data) {
   console.log("Your string appended sucessfully");
});


Output :'Your string appended sucessfully'

Synchronous and Asynchronous
      Node file system  module  content several methods. Each methods can be of two type , synchronous and asynchronous. A method is called and the control is move to the next method call, after call, do not wait for  response . The architecture of Node.js is context switching, so that it can handle a large number of processing in a single thread. The synchronous and asynchronous, both method are available in the Nodejs File System module. The difference between synchronous and asynchronous is that of last parameter. An asynchronous method accept an extra parameter call back function as error. Below is example of synchronous and asynchronous method.

Asynchronous method

Example 1
var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
   console.log(data.toString());
});


Example2
var fs = require('fs');
fs.writeFile('myTest.txt', 'Hello World!', function (err)
{
  console.log('Your file created and content written!');
});


Synchronous method
Example 1

var fs = require("fs");
var data = fs.readFileSync('input.txt');
console.log(data.toString());


Example 2

var fs = require("fs");
var data = fs.writeFileSync('C:\myTest.txt', 'Hello World');
console.log("file written sucessfully");



Monday, 14 May 2018

001 Node.js Introduction

Node.js Introduction

      Node.js is a free open source server environment . Node.js is a very powerful JavaScript base framework built on Google JavaScript V8 engine. Node.js runs on various platform like Windows, Linux, Max Unix etc. Node.js is a cross platform client side environment for scripting JavaScript when embedded in web page.Node.js was developed by  Rian Dahl in 2010. Initial release supported by Linux and Mac OS X  and was maintained by Dahl. Node.js runs only single thread using non blocking I/O call , large number of concurrent connection can be handled by the technique context switch.


Some point of Node.js
  • Fast: Node.js is built on Google Chrome's V8 JavaScript Engine and context switch technique ,that result fast execution .
  • Single Threaded: Node.js runs a single threaded (context switch ) with event looping.
  • License: Node.js is free and comes under the MIT license.
  • No Buffering : Node.js applications does not buffer data. Application output the data in chunks of data.
  • Highly Scalable: Node.js runs a single threaded in a non-blocking way.This made Node.js highly scalable.
  • Open source: Node.js is open source , have open source community , many excellent modules to add excellent capabilities to Node.js applications.
  • I/O is Asynchronous and Event Driven: API of Node.js library are asynchronous (non-blocking) and never wait for server to return data.After a call , the server moves to the next API call.When a response from previous API call code , there exists a notification mechanism to handle it.This makes Node.js very fast.
  • Extendable : Node.js is open source. you can extend feature it as per your need.
  • Large Streaming : Streaming large content is also possible with Node.js.
  • Code Support : Node.js community is very active , large number of code is the in the GitHub.You can get it easily.
  • Module Caching : Node.js supports caching of modules. When a Node.js modules is requested first time, it is cached into the application memory. The next calls for loading the same module would not executed again.


Some disadvantage of Node.js
  • Node.js doesn’t support multi-threading.
  • Node.js doesn’t support very high computational intensive tasks.long running task, it will queue all the incoming requests to wait for execution.
  • Exception handling is difficult
Where to use Node.js
 

   1) Streaming Applications , like data streaming ,video streaming ect.
   2) Real-time Applications (DIRT)
   3) Node.js can add, delete, modify data in database.
   4) File handling ,like create, open, read, write, delete, and close ect.
   5) Generating dynamic page content.



Who use Node.js

The following famous web site use node.js on their site 
  1. Paypal
  2. Yahoo!
  3. Groupon
  4. LinkedIn
  5. Walmart
  6. Uber
  7. Ebay
  8. NASA


028 AngularJS Animation

AngularJS Animation
Animation is a process to achieve some visual effect on the view.
To achieve Animation , you have to do

1) Add “angular-animate.js”  to your application.


2) You have to add “ngAnimate” to your application.


Example :

<!DOCTYPE html>
<html>
              <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
              <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-animate.js"></script>

<style>
div {
         transition: all linear 0.5s;
         background-color : Green;
         height: 100px;
         width: 100px;
}

.ng-hide {
            height: 0;
            width: 0;
            background-color: transparent;
           top:-200px;
           left: 200px;
}
</style>
<script>
          var app = angular.module('myApp', ['ngAnimate']);
</script>

<body ng-app="myApp">

<h1>Check / Uncheck to show hide div : <input type="checkbox" ng-model="mldShowHide"></h1>

<div ng-hide="mldShowHide">
</div>


</body>
</html>


Output

ngAnimate does not do animation , ngAnimate directive are added to notice event related to animation.The folling is the directive commonly used during animation programmig.

  1. ng-show
  2. ng-hide
  3. ng-class
  4. ng-view
  5. ng-include
  6. ng-repeat
  7. ng-if
  8. ng-switch
  9. ng-animate
  10. ng-hide-animate
  11. ng-hide-add
  12. ng-hide-remove
  13. ng-hide-add-active
  14. ng-hide-remove-active





025 Bootstrap Typeahead


Bootstrap Typeahead

Typeahead is a type of control , which show text before type is complete.You have to add "typeahead.js"  JavaScript library for this purpose.



Example

<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.9.3/typeahead.min.js"></script>
<script type="text/javascript">
                $(document).ready(function () 
         {
                  $('input.typeahead').typeahead({
                   name: 'accounts',
                   local: ['India', 'China', 'Brazil', 'USA', 'US', 'JAPAN', 'Bangladesh', 'Russia''Sweden','Alaska']
               });
           });
</script>
</head>
<body>
<div class="bs-example">
              <input type="text" class="typeahead" autocomplete="off" spellcheck="false">
</div>
</body>
</html>

Live Demo

Type :India/China/Brazil/USA/US/JAPAN/Bangladesh/Russia/Sweden/Alaska

Enter text below 


বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...