Nodejs Interview Questions 2020| Learn TuT




Node.js was initially developed by Ryan Dahl and saw its first release in May 2009.

it's a JS environment won’t to execute JavaScript code outside the browser. 

Supported by Chrome's V8 engine, it represents the JavaScript anywhere and

everywhere programming paradigm, unifying the online app development to one

language instead of different languages for server and client-side scripting.

 

Many corporations like LinkedIn, IBM, GoDaddy, Groupon, Netflix, and PayPal

use Node.js. Its increasing popularity has sparked a requirement for Node.js developers.



 1. What is Node.js?

Node.js may be a web application framework built on Google Chrome's   JavaScript Engine

(V8 Engine). Node.js comes with a runtime environment on which a Javascript based script is

often interpreted and executed (It is analogous to JVM to JAVA byte code). This runtime allows

the execution of JavaScript code on any machine outside a browser. Due to this runtime of

Node.js, JavaScript is now often executed on the server also . Node.js also provides an

upscale library of varied javascript modules which eases the development of web applications

using Node.js to great extents.

                       

                        Node.js = Runtime Environment + JavaScript Library


2. What does one mean by Asynchronous API?

All APIs of Node.js library are asynchronous; that's non-blocking. It essentially means a

Node.js based server never waits for a API to return data. Server moves to next API after

calling it and a notification mechanism of Events of Node.js helps server to urge response

from the previous API call.


  3. What are the advantages of using Node.js?


Following are main benefits of using Node.js

Asynchronous and Event DrivenAll APIs of Node.js library are asynchronous; that's non-blocking. It essentially means a Node.js based server never waits for an API to return data. Server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to urge response from the previous API call.

 Very Fast Being built on Google Chrome's V8 JavaScript Engine, Node.js library is extremely fast in code execution.

 Single Threaded but highly Scalable − Node.js uses one threaded model with event looping. Event mechanism helps servers to reply during non-blocking ways and makes servers highly scalable as against traditional servers which create limited threads to handle requests. Node.js uses one threaded program and same program can services much larger number of requests than traditional server like Apache HTTP Server.

 No Buffering − Node.js applications never buffer any data. These applications simply output the info in chunks.

 4. Is Node one threaded application?

Yes! Node uses one threaded model with event looping.


5. What is REPL in the context of Node?


REPL stands for Read Eval Print Loop and it represents a computer environment sort of a window

console or unix/linux shell where a command is entered and the system responds with an output.

Node.js or Node comes bundled with a REPL environment. It performs the subsequent desired

tasks.

  •   Read − Read user's input, parse the input into JavaScript data-structure & storein memory.

  •  Eval − Takes and evaluates the info structure

  •  Print − Prints the result

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


 6. What is the difference of using var and not using var in REPL while handling

variables?


Use variables to store values and print later. if var keyword isn't used then value is stored

within the variable and printed. Whereas if var keyword is employed then value is stored

but not printed. you'll use both variables later.


 7. What is npm?


npm stands for Node Package Manager. npm provides following two main functionalities:

• Online repositories for node.js packages/modules which are searchable on search.nodejs.org

• Command line utility to put in packages, do version management and dependency management

of Node.js packages.


8. What is global installation of dependencies?


Globally installed packages/dependencies are stored in /npm directory. Such dependencies are

often utilized in CLI (Command Line Interface) function of any node.js but can't be imported

using require() in Node application directly. to put in a Node project globally use -g flag.

                                              

C:\Nodejs_WorkSpace>npm install express -g


9. What is local installation of dependencies?


By default, npm installs any dependency within the local mode. Here local mode refers to the

package installation in node_modules directory lying within the folder where Node application

is present. Locally deployed packages are accessible via require(). to put in a Node project

locally following is that the syntax.


                                  C:\Nodejs_WorkSpace>npm install express --save


How to check the already installed dependencies which are globally installed using npm?


Use the subsequent command −


                                   C:\Nodejs_WorkSpace>npm ls -g


 10. What is Package.json?


package.json is present within the root directory of any Node application/module and is

employed to define the properties of a package.


11.  Name a number of the attributes of package.json?


Following are the attributes of Package.json

• name − name of the package

• version − version of the package

• description − description of the package

• homepage − homepage of the package

• author − author of the package

• contributors − name of the contributors to the package

• dependencies − list of dependencies. npm automatically installs all the dependencies

mentioned here within the node_module folder of the package.

• repository − repository type and url of the package

• main − entry point of the package

• keywords − keywords


12. How to uninstall a dependency using npm?


Use the following command to uninstall a module.

C:\Nodejs_test>npm uninstall dependency-name



 13. How to update a dependency using npm?


Update package.json and change the version of the dependency which to be updated and run

the subsequent command.

C:\Nodejs_LEARNTUT>npm update


  14. What is Callback?


Callback is an asynchronous equivalent for a function. A callback function is named at the

completion of a given task. Node makes heavy use of callbacks. All APIs of Node are written

in such a way that they support callbacks. For instance , a function to read a file may start

reading a file and return the control to the execution environment immediately in order that

next instructions are often executed. Once file I/O is complete, it'll call the callback function

while passing the callback function, the content of the file as parameter. So there's no blocking

or await File I/O. This makes Node.js highly scalable, because it can process a high number of

requests without expecting any function to return a result.


15.  What is a blocking code?

If an application has got to await some I/O operation so as to finish its execution any longer then

the code liable for waiting is understood as blocking code.


16. How Node prevents blocking code?


By providing a callback function. Callback function gets called whenever a corresponding event

is triggered.


                     Node.js Interview Questions 2020


17. What is an Event Loop?


Node js may be a single threaded application but it supports concurrency via concept of event

and callbacks. As every API of Node js are asynchronous and being one thread, it uses async

function calls to take care of the concurrency. Node uses observer patterns. Node thread keeps

an occasion loop and whenever any task gets completed, it fires the corresponding event which

signals the event listener function to get executed.


18. What is the purpose of Buffer class in Node?


Buffer class may be a global class and may be accessed in application without importing buffer

module. A Buffer may be quite an array of integers and corresponds to a raw memory allocation

outside the V8 heap. A Buffer can't be resized.


19. What is Piping in Node?


Piping may be a mechanism to attach output of 1 stream to a different stream. it's normally wants

to get data from one stream and to pass output of that stream to a different stream. there's no

limit on piping operations. Consider the above example, where we've read test.txt using

readerStream and write test1.txt using writerStream. Now we'll use the piping to simplify

our operation or reading from one file and writing to a different file.


20. Which module is employed for file based operations?


fs module is employed for file based operations.

var fs = require("fs")


21. Which module is used for buffer based operations?


buffer module is used for buffer based operations.

                                                         var buffer = require("buffer")


22. Which module is used for web based operations?


http module is used for web based operations.

                                                        var http = require("http")

fs module provides both synchronous also as asynchronous methods.

          

23. What is the difference between synchronous and asynchronous methods of

the fs module?


Every method in the fs module has synchronous also asynchronous form.

Asynchronous methods take a final parameter as completion function callback and the first

parameter of the callback function is error. It's preferred to use asynchronous method rather

than synchronous method as the former never blocks the program execution where the latter

one does.


24. What are streams? How many sorts of streams are present in Node.


Streams are objects that permit you to read data from a source or write data to a destination in

continuous fashion.


In Node.js, there are four sorts of streams.


• Readable − Stream which is used for read operation.

• Writable − Stream which is used for write operation.

• Duplex − Stream which may be used for both read and write operation.

• Transform − a kind of duplex stream where the output is computed supported input.



25. Name a number of the events fired by streams.

Each sort of Stream is an EventEmitter instance and throws several events at different instances

of time. for instance , a number of the commonly used events are:

• data − This event is fired when there's data out there to read.

         • end − This event is fired when there's no more data to read.

         • error − This event is fired when there's any error receiving or writing data.

        • finish − This event is fired when all data has been flushed to the underlying system.



26. How will you write a file using Node?

Following is that the syntax of 1 of the methods to write down into a file:


                 fs.writeFile(filename, data[, options], callback)


This method will overwrite the file if the file already exists. If you would like to write down into

an existing file then you ought to use another method available.


Parameters:

Here is that the description of the parameters used:

• path − this is often string having filename including path.

• data − this is often the String or Buffer to be written into the file.

• options − The third parameter is an object which can hold {encoding, mode, flag}.

By default encoding is utf8, mode is octal value 0666 and flag is 'w'

• callback − this is often the callback function which gets one parameter error and wants

  to return error just in case of any writing error.


27. How will you shut a file using Node?


Following is that the syntax of 1 of the methods to shut an opened file:

                                               

 fs.close(fd, callback)


Parameters

Here is that the description of the parameters used:

• fd − this is often the file descriptor returned by file fs.open() method.

• callback − this is often the callback function which gets no arguments aside from

a possible exception being given to the completion callback.


28.  How will you truncate a file using Node?


Following is that the syntax of the tactic to truncate an opened file −


                                            fs.ftruncate(fd, len, callback)


Parameters

Here is that the description of the parameters used:

• fd − this is often the file descriptor returned by file fs.open() method.

• len − this is often the length of the file after which files are going to be truncated.

• callback − this is often the callback function which gets no arguments aside from

a possible exception are given to the completion callback.


29. How will you delete a file using Node?


Following is that the syntax of the tactic to delete a file −


                                                  fs.unlink(path, callback)


Parameters

Here is that the description of the parameters used:

• path − this is often the file name including path.

• callback − this is often the callback function which gets no arguments aside from

a possible exception being given to the completion callback.



30. How will you create a directory?


Following is that the syntax of the tactic to make a directory:


fs.mkdir(path[, mode], callback)


Parameters

Here is that the description of the parameters used:

• path − this is often the directory name including path.

• mode − this is often the directory permission to be set. Defaults to 0777.

• callback − this is often the callback function which gets no arguments aside

from a possible exception are given to the completion callback.




31. What is the aim of __filename variable?


The __filename represents the filename of the code being executed. This is often the resolved

absolute path of this code file. For a main program this is often not necessarily an equivalent

filename utilized in the instruction . the worth inside a module is that the path thereto module file.


32. How are ‘Child Threads’ handled in Node.js?


In its core sense, Node.js may be a single thread process. Also, it doesn't expose any child

threads and therefore the modes of thread management to the developer. However, child threads

could also be generated in Node.js during a sort of processes, one among them being

asynchronous I/O.

Although the kid threads spawned through these processes run within the backdrop, they don’t

block the most code or execute any application code. But if you need threading support in an

application powered by Node.js, multiple tools are available for utilization.


 33. What is a test pyramid in Node.js?

In Node.js, a test pyramid may be a figure which explains the proportion of unit tests,integrations

tests, and end-to-end tests are required for the fruitful development of a project. The components

of a test pyramid are given below:

    Unit Tests: They test the individual units of code in isolation. 

            they're fast and you would possibly perform tons of those tests
    

Integrations Tests: They test the mixing among dissimilar units.

End-to-End (E2E) Tests: They test the system as an entire , right from the interface to the info store, and back.


34. Is Node.js the simplest platform for CPU-heavy applications?

Even though Node.js can help in creating server-side applications, CPU-incentive applications

aren't the strong suit of Node.js. The CPU-heavy operations pave the way for the blockage of

incoming requests and push the thread into critical situations.


 35. Why is NODE_ENV used?

When any Node.js project is within the stage of production, Node.js promotes the principle

to use NODE_ENV variables to flag it. When the NODE-ENV is about to be produced, your

application will perform at a speed 2 to three times faster than usual. The variable also improves

judgment during the event phase of projects.


36. State the steps to write down an Express JS application.

To line up an ExpressJs application, you would like to follow the subsequent steps:

 

  • Create a folder with the project name

  • Create a file named package.json inside the folder

  • Run the ‘npm install’ command on the prompt put in the libraries present the package file

  • Create a file named server.js

  • Create the ‘router’ file inside the package consisting of a folder named as index.js

  • The application is made inside the package containing the index.html file.


With this, we come to an end of the highest Node.js interview questions. Does one think we missed a crucial question or have any doubts that require to be cleared? allow us to know within the comments below.


People are also reading:

Keywords: node js interview question, nodejs, jsnode, node-js, interview question node js.

Post a Comment

Previous Post Next Post