Node.js
Tags: javascript, node, object
Published: 2020-06-15 14:19:28

# Core things in Node.js

## proceses object

- In the process object, there is an **argv** property. Which gives us arguments, those we have passed via console.
  <pre>
  console.log(process.argv);
  </pre>

  ![process object](https://dev-to-uploads.s3.amazonaws.com/i/q5datytzg145whuy1ss6.png)

- We can pass any number of arguments as we want, and access it into the script.
  <pre>
  var index = process.argv.indexOf("Hello");
  console.log(process.argv[index]);
  </pre>

  ![process object](https://dev-to-uploads.s3.amazonaws.com/i/q04g90quavtnxizmsbo8.png)

- We can also pass arguments as a flag.
  <pre>
  var index = process.argv.indexOf("--words") + 1;
  console.log(process.argv[index]);
  </pre>

  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/78fy9i6eveeeilprk27d.png)

- Write on the console using **process object**.
  <pre>
  process.stdout.write("Hello");
  process.stdout.write("World\n\n");
  </pre>

  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eeoaglp8pg7w7kipbp8q.png)

- Take input from console using **process object**.

  - But in this case program execution will never stop.
  - We have to stop it manually.
    <pre>
    process.stdin.on("data", (data) => {
      process.stdout.write(data);
    });
    </pre>

    ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pxezw0fhgrh9s8tqivzj.png)

  - In below example stop manually process.
    <pre>
    process.stdin.on("data", (data) => {
      process.stdout.write(data);
      process.exit(); // this function call stop the process
    });
    </pre>
    ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wyszsx143axf51r2wq80.png)

- Handle console logging inside the script.
  - In this example console will clear before each and every print statement.
    <pre>
    var index = 1;
    const interval = setInterval(() => {
      process.stdout.clearLine();
      process.stdout.cursorTo(0);
      process.stdout.write(`${index++}`);
    }, 500);
    setTimeout(() => {
      clearInterval(interval);
    }, 3000);
    </pre>
    ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/srww4xk32eranbsx5x1e.png)

## util module

- Util module use for logging data on the console like **console.log()**.
  - It displays data with time information.
    <pre>
    const util = require("util");
    util.log("Node.js process object");
    </pre>
    ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3hbr1hcwfmfh9vi26b1f.png)

## v8 module

- Showing **heap** data of program execution.
  <pre>
  const v8 = require("v8");
  const util = require("util");
  util.log(v8.getHeapStatistics());
  </pre>
  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i58dxk518culbps7o1uv.png)

## readline module

- **readline** has _createInterface_.

  - We have to provide **_read stream_** and **_output stream_** in the createInterface.
  <pre>
  const readline = require("readline");
  const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  });
  rl.question("Where are you living? ", (answer) => {
  rl.write(`You are living in ${answer}\n`);
  process.exit();
  });
  </pre>
  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dml6mvg5addho2aiijmj.png)

## events module

- event emtier allow us to create our own Event that we can call any time we want throughout the script's execution.
  <pre>
  const events = require("events");
  const emiter = new events.EventEmitter();
  emiter.on("customEvent", (message, sender) => {
    console.log("Inside the CustomEvent");
    console.log(`${sender} sent you: ${message}`);
  });
  emiter.emit("customEvent", "Hii, there", "User");
  </pre>

  ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8q15fox6p3pgmmzmjyx1.png)