Node Js Tutorial
- Node JS Introduction
- Node JS Installation
- Node JS Architecture
- Node JS Modules
- Node Js: FILE SYSTEM MODULES
- EVENTS IN NODE JS
- Node JS and Mysql
- Create Application in Node JS
- Global Object in Node JS
- Creating and loading Node Module
- EXTEND EVENT EMITTER
Questions & Answers
Node JS Question Answers
Q1-: Explain Node JS?
It supports following OS, Z/OS, Linux, macOS, Microsoft Windows, open BSD, SmartOS.
Q2- Features of Node JS?
Main features of Node JS is Productivity, Fast, Real-Time Application, asynchronous and Streaming in Node JS.
ASYNCHRONOUS- Node JS is event driven environment that you can implement asynchronous processing for user interface or users action. It helps to handle large amount of requests in short period of time. Asynchronous pattern helps to manage traffic of multiple requests.
REAL TIME APPLICATION-: With Node JS you can easily build Real-Time Application like gaming app or chat app etc. that require real-time interaction and processing.
STREAMING-: There are Streaming applications like watching Netflix or youtube in that also it’s very easy to Read and Write using Node JS. Websockets on Node JS helps to read/write. It makes it very suitable for streaming applications.
Q3-: Who developed Node JS and how to use REPL in Node JS?
Node JS was developed in 2009 by Ryan Dahl.
First if you want to know the version of node js then write following command as- “Node –v”. REPL means Read Evaluate Print Loop. When you type something in prompt and click to execute then REPL Process the prompt and give output in prompt. REPL read the prompt, evaluate it and then print the result and show the prompt with specific loop.
Just type node then you will enter in REPL.
REPL make writing scripts very easy. For example-:
Now understand ease of REPL from following different aspects-
- If you want to add two number then just write “1+1” and enter you got the result “2” you no need to define “a=1” etc.
- If you write var a; to ask for variable then output will be undefined. So you have to write-:
THEN OUTPUT WILL BE 10
And if you want to add in A then just write
Output will be 30
Same in other aspects all you have to do is define the values and just simple ask for the output by directly put the variable.
You can better understand by following code-:
>if (a>9) (
… console.log(“a is greater than 9”);
A is greater than 9
If you exit REPL once then when you again go to REPL by putting node then all variable which were created will be erased.
Q4-: REPL commands in Node JS?
Below are some REPL main commands-:]
- Ctrl+c = For terminating current command
- Ctrl+c twice = for terminating REPL.
- Ctrl+d- for terminating REPL
- Tab keys – list of all current commands.
- .break – exit from multiline expression.
- .save with filename – save REPL session to a file.
Q5-: Explain NPM in Node JS?
Another thing is when you create a project; npm is going to add one file known as “package.JSON”. Package.json contains all the dependencies. If you give the project to someone else without notify about lodash library, they take the project and check the package.json file and read all the dependencies. As they run the npm command on the project. NPM command checks the package.json file and checks the dependencies that need to get. Then they go to npm repository and download all the dependencies and make it available on the project. So, in all things many things are managing with npm as- managing libraries, managing versions and you are making sure that versions are consistent and development process is easy. So that is NPM.
Q6-: Explain callback in Node.JS and how it can be more scalable?
Callback is called when asynchronous operation has been completed. Callbacks are heavily use by Node.js and all API’S OF Node.js is written to support callbacks.
How it can be more scalable?
Node.js works well for input output bound but not CPU bound work. For instance, to read a file if there is a function then file reading will be started during that instruction and once input output is done, callback function is called.
So, no blocking will be there with callbacks.
Q8-: Explain global and local of dependencies of Node.js?
- Globally installed packages are stored in < user-directory>/npm directory and these dependencies used in CLI (command line interface) of any node.js.
- NPM will install the dependencies in local mode by default. Local mode refers to package installation in node_modules directory lying in the folder where node application is present.
Q9-: Explain child process module and why to use exec, fork, spawn() method for child process module?
To create child process module you have following three ways-:
It launches new process with command child.process.spawn.
child_process_exec method runs command in shell/console and buffers the output.
It is special case of spawn to create child processes command as child_process.fork.
“exec()” method buffers the output by run the command in a shell. Command is as below-:
“spawn()” method used to launch new process with given commands and method signature is as below-:
Child_process.spawn(command[, args] [, options])
“fork()” method is special case of “spawn()” for creating node processes. The method signature –
Child_process.fork(modulePath[, args] [, options])
Q10-: Explain Stream Pipe method in Node.js ?
Stream method denoted as stream.pipe(), this method used for taking readable stream and connect it to writeable stream. Streams in node.js used to handle asynchronous process. It also helps in buffer data, it collects the data as it comes in until you are ready to use it or until all the data has arrived from stream.