NodeJS - Getting Started With Some Basic Functions

Today, I am explaining about getting started with NodeJS. Node.js is a powerful JavaScript-based framework/platform built on Google Chrome's JavaScript Engine.

For getting started with Node.js, follow the below steps.

  1. Download NodeJS from here and Install it.
  2. Download Git and install it.
  3. Now, we are checking if the Node.js is installed successfully or not. So, we have made one folder in “D:\” drive, and gave it a name as“NodeJSExamples”. Right-click this folder and go to “Git Bash here”.  As shown in the below image.

Node.js
  1. It will open one window as shown in the below image.

    Node.js

  2. Now, type “node” in the opened window as shown in the below image.

    Node.js
  1. Now, type the below command and hit the Enter key.

    console.log("Hello World!");

    Node.js

As you see in the above image, it will give output as “Hello World!”. Now, you can say that your NodeJS is installed successfully.

There are some basic functions that are used in NodeJS.

First, we make a NodeJS file in “D:/NodeJSExamples/” folder and give it a name as “NodeJSBasicFuction.js”. And, write some basic function code and check the output of them.

For running any NodeJS file, open ‘Git Bash’ and type “node filename.js”.

  • __Filename

This gives a path to the current executing NodeJS file.

Example - console.log(__filename);

Output  -    D:\NodeJSExamples\NodeJSBasicFunction.js

Node.js 

  • __dirname

This will give the name of the directory in witch current NodeJS script is executing.

Example -  console.log(__filename);

Output  -  D:\NodeJSExamples

Node.js 

  • Read file

For any file related operation, you have to use ‘fs’ module. The below function is used to read any file.

Example

  1. var fs=require('fs');  
  2. var data=fs.readFileSync('Exetext.txt');  
  3. console.log(data.toString());  

Output

Text file content is start

This articel givve infor about callback in node.js

This is article text line-1

This is article text line-2

This is article text line-3

This is article text line-4

This is article text line-5

This is article text line-6

This is article text line-7

This is article text line-8

Text file content is end

Node.js 

  • Create new file

For creating any new file, the “writeFile” function of ‘fs’ module is used. This function creates a new file with the given name and writes the given content in file. If file name does already exist, then it deletes its all content and overwrites the given text.

Example

  1. var fs = require('fs');  
  2. fs.writeFile('mynewfile3.txt''Hello content!'function(err) {  
  3.     if (err) throw err;  
  4.     console.log('Saved!');  
  5. });  

Output

Node.js 

  • Update the file

For updating any existing file in node.js, the “appendFile” method of the ‘fs’ module is used. This function updates the given content to the existing file. This will append a content to the end of the existing content of file.

Example

  1. var fs = require('fs');  
  2. fs.appendFile('mynewfile3.txt'' This is my text.'function(err) {  
  3.     if (err) throw err;  
  4.     console.log('Updated!');  
  5. });  

Node.js 

  • SetTimeout

This method is used to call the function after given specific time. In the below example, you can say that ‘Hello World’ is printed after 2 seconds.

Example

  1. function fun1() {  
  2.     console.log('Hello World');  
  3. }  
  4. setTimeout(fun1, 2000);  
  • clearTimeout

This method is used to remove the timeout set by ‘setTimeout’ method that you have seen above.

Exmaple

  1. function fun1() {  
  2.     console.log('Hello World');  
  3. }  
  4. var t = setTimeout(fun1, 5000);  
  5. clearTimeout(t); 
  • setInterval

This method is used to run any method after every specific time period.

Example

  1. function fun1() {  
  2.     console.log('Hello World');  
  3. }  
  4. setInterval(fun1, 5000);  

Thank you for reading my article. If you have any query, then write me in the comment box.

Up Next
    Ebook Download
    View all
    Learn
    View all