Node JS Basic Notes 1

Q.1 ) What is Node.js?

Ans. Node.js क्रोम v8 जावास्क्रिप्ट इंजन पर निर्मित एक जावास्क्रिप्ट रनटाइम वातावरण है। और नोड जेएस सर्वर पर चलने वाली एक जावास्क्रिप्ट है।

History Of Node.js

2009 में रयान डाहल (Google इंजीनियर) के दिमाग में एक विचार आया कि क्यों न ब्राउज़र के बाहर जावास्क्रिप्ट चलाया जाए, इसलिए उन्होंने v8 इंजन लिया और इसे C++ प्रोग्राम में एम्बेड किया और इसे Node.exe नाम दिया जो बाद में Node.js बन गया।

When to use Node JS

  1. I/O bound (input & output)

  2. Data Streaming Applications -- (like Facebook reel, Instagram reel, netflix movie app)

  3. Real-time chat Application

Q.2) How to Install Node.js in our system.

Ans. To install Node.js in our system visit Node.js official website link given below

Click to visit the Node.js site.

After clicking the link you see an interface like this.

Select LTS mode and click on Windows Installer(If you are using Windows). And if you are using another operating system then choose another Installer.

By clicking on Windows Installer your download will start automatically. After downloading you can install it in your system by following the tips.

These are stored in this link: -

Click here to see the Installation steps.

After installing Node.js successfully then open your cmd and check the Node.js is successfully installed or not

Command to check node js is installed or not: - node -v

If the Node.js current version is shown in your cmd then your node.js is installed successfully.

And if you get an error like this line 'node' is not recognized as an internal or external command operable program or batch file. Then check again the installation process.

Now we write the first code using Node.js.

Make Sure that you have VS code and another code editor to run the node.js code. If you do not install the vs code yet then download and install the code editor(VS code).

VS code download link

Download the VS code and install it.

After that create a folder on your desktop and open it into vs code.

Open the folder here and create a new file with the extension .js (like index.js)

Now, we write a Hello World program in node.js

After writing a code in VS code then open the VS code terminal by following these steps:

  1. Go to the terminal tab.

  2. Click on New terminal.

There was a shortcut keyboard key to open a terminal.

ctrl + `

Q.3) REPL in Node.js.

Ans. The REPL feature in Node.js is very useful in experiments in Node.js code and to debug the JavaScript codes.

Read - Read user's input, parse the input into JavaScript data-Structure, and store it in memory

Eval - Takes and evaluates the data structure

Print- Print the result

Loop - Loops the above command until the user presses ctrl-c twice

REPL is just like a browser console. we can Do many code activities on it.

  1. JS Expression

  2. Use variables

  3. Multiline code/loops

  4. use (_) to get the last result

  5. we can use editor mode.

💡
write a node in vs code terminal to start using REPL

Q.4) File System in node.js

Ans. List of content:-

  1. Write a file system

  2. Opening a file

  3. Reading a file

  4. Writing to a file

  5. Appending to a file

  6. Closing a file

  7. Deleting a file

File System:

  • Node.js एक प्लेटफ़ॉर्म है जो क्रोम के V8 जावास्क्रिप्ट इंजन का उपयोग करता है, जो डेवलपर्स को सर्वर-साइड एप्लिकेशन बनाने के लिए जावास्क्रिप्ट का उपयोग करने की अनुमति देता है जो वेब क्लाइंट के लिए गतिशील सामग्री उत्पन्न करता है। जो चीज Node.js को अलग करती है, वह इसकी इवेंट-संचालित और नॉन-बॉकिंग I/O विशेषताएं हैं।

  • Node.js में FS (फ़ाइल सिस्टम) नामक एक अंतर्निहित मॉड्यूल शामिल है जो उपयोगकर्ताओं को अन्य फ़ाइल संचालन बनाकर, पढ़ने, हटाने और निष्पादित करके फ़ाइलों को प्रबंधित करने की अनुमति देता है।

  • इस मॉड्यूल का उपयोग करने के लिए, एक डेवलपर "require()" विधि को कॉल कर सकता है, जो उपयोगकर्ता की आवश्यकताओं के आधार पर, सिंक्रोनस और एसिंक्रोनस फ़ाइल सिस्टम संचालन दोनों को सक्षम करने के लिए Node.js द्वारा लिपटे POSIX फ़ंक्शन तक पहुंच प्रदान करता है।

  • var fs = require("fs");

Difference between Synchronous and Asynchronous:

  • The Synchronous approach involves blocking functions that wait for each operation to complete before executing the next one. This means that the command won't execute unit the query has finished executing and all the results from previous commands have been Obtained.

  • On the other hand, the Asynchronous approach involves a non-blocking function that executes all operations at once, without waiting for each operation to complete. The results of each operation are handled when they become available, and each command is executed after the previous one. If the operation involves querying large amounts of data from the databases, the Asynchronous approach is recommended.

💡
Let's see an example to read the file in an Asynchronous way.

In this method, we start reading data (In between lines 3 to 10). This may take a few seconds to read. But In the Asynchronous way if the data take time to read then the code moves to the next statement like (line no 12). See the output in the terminal.

💡
Now, see the example of a Synchronous way to read the files.

Now, we saw the second trick to open the file.

.....Open file

In the upper example first we require("fs") .After that, we open the required file with the help of the open command and gave the path of the file like "input.txt" in the next step which is "r+" "r+" basically permits the opening file to read and write the file inner text.

Read the upper image text.

Now we read the opening file.

....Read file

Read these instructions carefully which are 👇

Now we write into the file.

....write file

Now see Appending file in node js

....Appending file

The result after appending the file...

Now we see Deleting a file in node.js

....Delete file

Q.5) OS Module in Node.js

Ans. OS means - OPERATING SYSTEM. This module helps to check the system information that we are using. There are some basic modules in OS which are given below with examples.

  1. OS.arch()

  2. OS.freemem()

  3. OS.totalmem()

  4. OS.hostname()

  5. OS.tmpdir()

We can check these commands by using require("os").

OS.arch()

If we want to check the architecture of the CPU (32-bit and 64-bit) then we use this command.

const variable_name = require("os");

console.log(variable_name.arch());

OS freemem()

If we want to check free memory in our cpu then we can use this command.

const variable_name = require("os");

console.log(variable_name.freemem());

This output 2549821440 is shown in bytes. If we see this output in GB then we use a simple formula which is given below.

OS.totalmem()

If we want to check the total memory of our CPU then we use this command.

const variable_name = require("os");

console.log(variable_name.totalmem());

OS hostname()

If we want to check the hostname of our system we use this command.

const variable_name = require("os");

console.log(variable_name.hostname());

OS tmpdir()

If we want to see our tmp folder in the system then we use this command.

const varable_name = require("os");

console.log(variable_name.tmpdir())

There are so many commands in OS if you practice all of these then visit this link :

OS Modules.

Q.6) HTTP Modules in node.js

What is HTTP Module?

Ans. The full form of HTTP is HyperText Transfer Protocol. It is a protocol used for transmitting data over the internet. HTTP defines how client and server communicate with each other and how requests and responses should be formatted and transformed. It is the foundation of data communication for the world wide web or the Internet.

HTTP Module in Node.js

Ans. Node.js HTTP module is an internal component that facilities various aspects of the HTTP protocol. It helps to create the layer of the server. To use the HTTP module, start by creating a new file named "server.js" and then import the http module using the require() function.

Second, create an HTTP server using the createServer() method of the HTTP object.

Finally, the incoming HTTP request is monitored on the port.

Now we create our server using Node.js

  • Line no.1:- We require an HTTP and stored it into a variable name.

  • Line no.2:- By using the variable name we use a method that is createServer and the method takes a callback function with two parameters first request and the second response.

  • Line no.3:- It checks the condition if the client requests a specific path and then gave to the response. we can use many conditions like lines no.3 to 7.

  • Line no.8:- After checking the condition we use the end method to end the response. It is compulsory to end the response.

  • Line no.10:- we create our server successfully and now our final work is to run it into a specific port. Which is shown in line no.10

Following Steps to see our server on the browser

  • After creating our server then we move into our VS code terminal and run it like (node filename.js).

  • Now we open our Chrome browser URL and type there (localhost: port listen) like see the image

Did you find this article valuable?

Support Notes by becoming a sponsor. Any amount is appreciated!