Introduction to NodeJS Coding - Part1

Here we gonna talk about main features of NodeJS runtime and JavaScript library. Hope you guys followed my previous introductory blog on NodeJS. If not click Here ,

As with any programming language, platform, or tool that doesn't come bundled with Windows, getting up and running with Node.js takes some initial setup before you can start hacking away. In my experience, though Node.js has a far better installation experience on Windows than virtually any other language, platform, or tool that I've tried to use - just run the installer, and you're good to go.

First install node on your computer,
  • Open the official page for Node.js downloads and download Node.js for Windows by clicking the "Windows Installer" option
  • Run the downloaded Node.js .msi Installer - including accepting the license, selecting the destination, and authenticating for the install.
    • This requires Administrator privileges, and you may need to authenticate
  • To ensure Node.js has been installed, run node -v in your terminal - you should get something like v6.10.1
  • Update your version of npm with npm install npm --global
    • This requires Administrator privileges, and you may need to authenticate
  • Congratulations - you've now got Node.js installed, and are ready to start building!
So,Let's do some basic coding on NodeJS,

Like always "Hello World" example,
  • I'm using  WebStorm IDE,which is a good one to use if you are JavaScript fanatic.
  • Create an Empty Project and in inside create a JavaScript file named 'app.js'.
  •  Use default console log command to print string to the console.
    •   console.log('Hello World');
  • Type the following command in Terminal to run the node file.
    •  node app.js
  •  You will be able to see the output "Hello World" on terminal.


Next will use OS system library into our node program,


  • Import the OS system module to your file.
    • const os = require('os'); 
  • Obtain System architecture, platform and number of CPUs from the OS module and print them to the console. 
    • console.log('Architecture ' + os.arch());
    • console.log('CPUs ' + os.cpus().length); 
    • console.log('OS ' + os.platform());
  •  Run application and check the output.
    • node app.js 
Let's see how NodeJS handles file operations. First we will read a file and send the output to the terminal screen
  • Create a file named test.txt and add the following content.
    • NodeJS is awesome. 
  • Import the fs system module to read the file. 
    • const fs = require('fs');
  • Use the system variable __dirname to set the file location. 
    • const fileName = __dirname + '/test.txt'; 
  •  Read the file using readFile asynchronous method and print the content of the file to console. 
    • Note,if you haven't enable ECMAScript 6 please enable that in Setting -> Languages and Frameworks -> JavaScript ,and select ECMAScript 6 as the version.


  • Use the readFileSync method to read the file synchronously. 
    • const data = fs.readFileSync(fileName); 
    • console.log(data.toString());
Next let's see how to copy a content of a file to another file.

  •  Add two variables containing path to the source and destination files.
    • const fileName = __dirname + '/test.txt';
    • const outFileName = __dirname + '/test-copy.txt';
  • Create read stream and write stream from the source file and destination file respectively.
    • const readStream = fs.createReadStream(fileName);
    • const writeStream = fs.createWriteStream(outFileName);
  • Pipe the read stream to write stream.
    • readStream.pipe(writeStream);
  • Verify the file named test-copy.txt is being created with the same content as the test.txt.
  • Optionally listen to the data event of the read stream and print the output.

You can get the full code project in this tutorial Here ,
Now you do have some basic idea of how NodeJS works, In the next blog in this series, i will be creating a simple HTTP Server with REST services.

Comments

Popular posts from this blog

Introduction to using ExpressJS with MongooseJS and NodeJS - Part 2

Using OAuth 2.0 Framework to Manipulate Files in Google Drive

Preventing Cross-Site Request Forgery (CSRF) with Double Submit Token Pattern