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.
- Download NodeJS from here and Install it.
- Download Git and install it.
- 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.
- It will open one window as shown in the below image.
- Now, type “node” in the opened window as shown in the below image.
- Now, type the below command and hit the Enter key.
console.log("Hello World!");
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”.
This gives a path to the current executing NodeJS file.
Example - console.log(__filename);
Output - D:\NodeJSExamples\NodeJSBasicFunction.js
This will give the name of the directory in witch current NodeJS script is executing.
Example - console.log(__filename);
Output - D:\NodeJSExamples
For any file related operation, you have to use ‘fs’ module. The below function is used to read any file.
Example
- var fs=require('fs');
- var data=fs.readFileSync('Exetext.txt');
- 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
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
- var fs = require('fs');
- fs.writeFile('mynewfile3.txt', 'Hello content!', function(err) {
- if (err) throw err;
- console.log('Saved!');
- });
Output
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
- var fs = require('fs');
- fs.appendFile('mynewfile3.txt', ' This is my text.', function(err) {
- if (err) throw err;
- console.log('Updated!');
- });
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
- function fun1() {
- console.log('Hello World');
- }
- setTimeout(fun1, 2000);
This method is used to remove the timeout set by ‘setTimeout’ method that you have seen above.
Exmaple
- function fun1() {
- console.log('Hello World');
- }
- var t = setTimeout(fun1, 5000);
- clearTimeout(t);
This method is used to run any method after every specific time period.
Example
- function fun1() {
- console.log('Hello World');
- }
- setInterval(fun1, 5000);
Thank you for reading my article. If you have any query, then write me in the comment box.