Search This Blog

2018/09/23

Node.js:Global objects

Global objects in node.js are accessible in all module without need to include them.those can be
modules, functions, strings and object.

a) __filename:absolute path of the current module file

b) _dirname:absolute path of the dir in which executable resides

    for test.js inside /home/sangram/workspace/node/global folder.
        console.log(__filename);
        console.log(__dirname);


    sangram@sangram-HP-Laptop-15-bs0xx:~/workspace/node/global$ node test.js
    /home/sangram/workspace/node/global/test.js
    /home/sangram/workspace/node/global

c) setTimeout(cb, ms):its a global function used to run callback after at least said milliseconds.A timer cannot span more than 24.8 days

    inside our test.js
        console.log(__filename);
        console.log(__dirname);

        function Hello(){
               console.log( "Hello, World!");
            }

        setTimeout(Hello, 5000);
        console.log("Reached End of File");

    running test.js
        sangram@sangram-HP-Laptop-15-bs0xx:~/workspace/node/global$ node test.js
        /home/sangram/workspace/node/global/test.js
        /home/sangram/workspace/node/global
        Reached End of File
        Hello, World!

d) clearTimeout(t): global function used to stop a timer that was previously created with setTimeout()

        inside test.js

        console.log(__filename);
        console.log(__dirname);

        function Hello(){
               console.log( "Hello, World!");
            }

        timer = setTimeout(Hello, 5000);
        clearTimeout(timer)

        console.log("Reached End of File");

    running test.js

        sangram@sangram-HP-Laptop-15-bs0xx:~/workspace/node/global$ node test.js
        /home/sangram/workspace/node/global/test.js
        /home/sangram/workspace/node/global
        Reached End of File

    we can see "Hello World!" is not printed on console as settimeout timer is cleared in.

e) setInterval:global function used to run callback cb repeatedly after at least ms milliseconds.timer cannot span more than 24.8 days.
   
    inside test.js
        console.log(__filename);
        console.log(__dirname);

        function Hello(){
               console.log( "Hello, World!");
            }

        //timer = setTimeout(Hello, 1);
        //clearTimeout(timer)

        setInterval(Hello,2000)
        console.log("Reached End of File");

    running test.js
        sangram@sangram-HP-Laptop-15-bs0xx:~/workspace/node/global$ node test.js
        /home/sangram/workspace/node/global/test.js
        /home/sangram/workspace/node/global
        Reached End of File
        Hello, World!
        Hello, World!
        Hello, World!
        Hello, World!
        ^C
f) clearInterval: global function used to stop a timer that was previously created with setInterval()

    inside test.js
        console.log(__filename);
        console.log(__dirname);

        function Hello(){
               console.log( "Hello, World!");
            }

        //timer = setTimeout(Hello, 1);
        //clearTimeout(timer)


        timer2 = setInterval(Hello,2000)
        clearInterval(timer2)

        console.log("Reached End of File");

    running test.js
        sangram@sangram-HP-Laptop-15-bs0xx:~/workspace/node/global$ node test.js
        /home/sangram/workspace/node/global/test.js
        /home/sangram/workspace/node/global
        Reached End of File

    no "Hello World!" is printed on console.
 
Global Objects:
    Console, Process & Buffer are commonly used global objects in node.js.Console used to print message on stdout and stderr.Process used to get information on current process.
    Console has three methods console.log(),console.error() & console.warn() for printing output.

Global modules:

    a) OS:
        Provides basic operating-system related utility functions.
     b) Path:
        Provides utilities for handling and transforming file paths.
    c) Net :
        Provides both servers and clients as streams. Acts as a network wrapper.
     d) DNS :
        Provides functions to do actual DNS lookup as well as to use underlying operating system name resolution functionalities.
    e)Domain:
        Provides ways to handle multiple different I/O operations as a single group.

No comments:

Post a Comment