21. What is callback hell in Node.js ?
Node.js works on the single thread non-blocking event loop, which passes another events as a callback next to be fired after any event completion.
Example of Callback Hell:
loadReocrds(function(a){
loadFirstReocrds(a, function(b){
loadSecondReocrds(b, function(c){
loadThirdReocrds(c, function(d){
loadFoutrhReocrds(d, function(e){
...
});
});
});
});
});
This type of calling callbacks in any event loop, causes callback hell in Node.js
This chain could be so long until the event loop ends, when there is no next callback event.
22. How do you prevent/fix/avoid callback hell ?
There are multiple techniques for dealing with callback hell.
Using Async.js
Using Promises
Using Async-Await
23. What are the available Web application framworks for Node.js ?
Express.js
Koa
Strapi
Sails.js
Hapi
FeathersJs
fastify
purpleecheerio-wave
24. Name the types of API functions in Node.js ?
There are two types of API functions in Node.js:
Asynchronous -> Non-blocking functions
Synchronous -> Blocking functions
25. What is REPL in Node.js, Explain the role ?
REPL stands for Read-Eval-Print-Loop.
REPL is a Node shell - A virtual environment where a command is entered and the system responds with an output.
Node.js comes bundled with a REPL environment.
Read − Reads user's input.
Eval − evaluates the data.
Print − Prints the result.
Loop − Loops until the user presses the ctrl-c twice.
26. What are Commands & Keys for REPL ?
.break - This command (or pressing the <ctrl>-C key combination) will abort further input or processing of that expression.
.clear - Resets the REPL and clears any multi-line expression.
.exit - Close the I/O stream, causing the REPL to exit.
.help - Show this list of special commands.
.save - Save the current REPL session to a file: > .save ./file/to/save.js
.load - Load a file into the current REPL session. > .load ./file/to/load.js
.editor - Enter editor mode (<ctrl>-D to finish, <ctrl>-C to cancel).
ctrl + c Terminate the current command.
ctrl + c (twice) Terminate the Node REPL.
ctrl + d Exit from the REPL.
27. Why we use underscore variable in REPL ?
The underscore variable is used to get the last result in REPL.
> var a = 20
undefined
> var b = 40
undefined
> a + b
60
> var add = _
undefined
> console.log(add)
60
undefined
>
28. Difference between ajax and node.js ?
Ajax :
Ajax works on client-side to request on server and gets data as response from server then it renders on the client-side without refreshing the page.
It uses JavaScript to write syntax on clinet-side.
Node.js :
Nodejs is a JavaScript run time web application framework built on Google Chrome's V8 JavaScript engine.
It accepts the request sent from ajax and response with the expected web resources.
It also uses javascript to write syntax, but on server-side.
29. What is the difference between AngularJS and Node.js?
Node.js Interview Questions
Angularjs works on the client-side to handle the DOM manipulation and its events, it renders partial html views on the client-side without refreshing the page.
It uses JavaScript to write syntax on client-side.
Node.js :
Nodejs is a JavaScript run-time web application framework built on Google Chrome's V8 JavaScript engine.
It accepts the request sent from ajax and response with the expected web resources.
It also uses JavaScript to write syntax, but on server-side.
30. What are Globals in Node.js?
Global objects or variables are the one which are directly accessible to use anywhere in the whole application without importing any depndencies.
Global Objects Specific to Node.js:
- __dirname - to get the current directory
- __filename to get the file name
- exports - to make module or class accessable to anywhere in application
- module - to declare a module
- require() - to import libraries