Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MonaAghili/60646a7a4a163cb15fbfdc24b5501bb1 to your computer and use it in GitHub Desktop.
Save MonaAghili/60646a7a4a163cb15fbfdc24b5501bb1 to your computer and use it in GitHub Desktop.
# Interview Questions
* Node.js
## Node.js
### Q1: What do you mean by Asynchronous API? ☆☆
**Answer:**
All APIs of Node.js library are aynchronous that is 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 get response from the previous API call.
**Source:** _tutorialspoint.com_
### Q2: What are the benefits of using Node.js? ☆☆
**Answer:**
Following are main benefits of using Node.js
* **Aynchronous and Event Driven** - All APIs of Node.js library are aynchronous that is 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 get response from the previous API call.
* **Very Fast** - Being built on Google Chrome's V8 JavaScript Engine, Node.js library is very fast in code execution.
* **Single Threaded but highly Scalable** - Node.js uses a single threaded model with event looping. Event mechanism helps server to respond in a non-bloking ways and makes server highly scalable as opposed to traditional servers which create limited threads to handle requests. Node.js uses a single 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 data in chunks.
**Source:** _tutorialspoint.com_
### Q3: Is Node a single threaded application? ☆☆
**Answer:**
Yes! Node uses a single threaded model with event looping.
**Source:** _tutorialspoint.com_
### Q4: What is global installation of dependencies? ☆☆
**Answer:**
Globally installed packages/dependencies are stored in **<user-directory>**/npm directory. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but can not be imported using require() in Node application directly. To install a Node project globally use -g flag.
**Source:** _tutorialspoint.com_
### Q5: What is an error-first callback? ☆☆
**Answer:**
*Error-first callbacks* are used to pass errors and data. The first argument is always an error object that the programmer has to check if something went wrong. Additional arguments are used to pass data.
```js
fs.readFile(filePath, function(err, data) {
if (err) {
//handle the error
}
// use the data object
});
```
**Source:** _tutorialspoint.com_
### Q6: What's the difference between operational and programmer errors? ☆☆
**Answer:**
Operation errors are not bugs, but problems with the system, like _request timeout_ or _hardware failure_. On the other hand programmer errors are actual bugs.
**Source:** _blog.risingstack.com_
### Q7: What is the difference between Nodejs, AJAX, and jQuery? ☆☆
**Answer:**
The one common trait between Node.js, AJAX, and jQuery is that all of them are the advanced implementation of JavaScript. However, they serve completely different purposes.
* Node.js –It is a server-side platform for developing client-server applications. For example, if we’ve to build an online employee management system, then we won’t do it using client-side JS. But the Node.js can certainly do it as it runs on a server similar to Apache, Django not in a browser.
* AJAX (aka Asynchronous Javascript and XML) –It is a client-side scripting technique, primarily designed for rendering the contents of a page without refreshing it. There are a no. of large companies utilizing AJAX such as Facebook and Stack Overflow to display dynamic content.
* jQuery –It is a famous JavaScript module which complements AJAX, DOM traversal, looping and so on. This library provides many useful functions to help in JavaScript development. However, it’s not mandatory to use it but as it also manages cross-browser compatibility, so can help you produce highly maintainable web applications.
**Source:** _techbeamers.com_
### Q8: How to make Post request in Node.js? ☆☆
**Answer:**
Following code snippet can be used to make a Post Request in Node.js.
```js
var request = require('request');
request.post('http://www.example.com/action', {
form: {
key: 'value'
}
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
});
```
**Source:** _techbeamers.com_
### Q9: What are the key features of Node.js? ☆☆
**Answer:**
Let’s look at some of the key features of Node.js.
* **Asynchronous event driven IO helps concurrent request handling –** All APIs of Node.js are asynchronous. This feature means that if a Node receives a request for some Input/Output operation, it will execute that operation in the background and continue with the processing of other requests. Thus it will not wait for the response from the previous requests.
* **Fast in Code execution –** Node.js uses the V8 JavaScript Runtime engine, the one which is used by Google Chrome. Node has a wrapper over the JavaScript engine which makes the runtime engine much faster and hence processing of requests within Node.js also become faster.
* **Single Threaded but Highly Scalable –** Node.js uses a single thread model for event looping. The response from these events may or may not reach the server immediately. However, this does not block other operations. Thus making Node.js highly scalable. Traditional servers create limited threads to handle requests while Node.js creates a single thread that provides service to much larger numbers of such requests.
* **Node.js library uses JavaScript –** This is another important aspect of Node.js from the developer’s point of view. The majority of developers are already well-versed in JavaScript. Hence, development in Node.js becomes easier for a developer who knows JavaScript.
* **There is an Active and vibrant community for the Node.js framework –** The active community always keeps the framework updated with the latest trends in the web development.
* **No Buffering –** Node.js applications never buffer any data. They simply output the data in chunks.
**Source:** _techbeamers.com_
### Q10: What is control flow function? ☆☆
**Answer:**
It is a generic piece of code which runs in between several asynchronous function calls is known as control flow function.
**Source:** _lazyquestion.com_
### Q11: What are Event Listeners? ☆☆
**Answer:**
**Event Listeners** are similar to call back functions but are associated with some event. For example when a server listens to http request on a given port a event will be generated and to specify http server has received and will invoke corresponding event listener. Basically, Event listener's are also call backs for a corresponding event.
Node.js has built in event's and built in event listeners. Node.js also provides functionality to create Custom events and Custom Event listeners.
**Source:** _lazyquestion.com_
### Q12: If Node.js is single threaded then how it handles concurrency? ☆☆
**Answer:**
Node provides a single thread to programmers so that code can be written easily and without bottleneck. Node internally uses multiple POSIX threads for various I/O operations such as File, DNS, Network calls etc.
When Node gets I/O request it creates or uses a thread to perform that I/O operation and once the operation is done, it pushes the result to the event queue. On each such event, event loop runs and checks the queue and if the execution stack of Node is empty then it adds the queue result to execution stack.
This is how Node manages concurrency.
**Source:** _codeforgeek.com_
### Q13: What is Callback Hell? ☆☆
**Answer:**
The asynchronous function requires callbacks as a return parameter. When multiple asynchronous functions are chained together then callback hell situation comes up.
**Source:** _codeforgeek.com_
### Q14: Could we run an external process with Node.js? ☆☆
**Answer:**
Yes. *Child process module* enables us to access operating system functionaries or other apps. Scalability is baked into Node and child processes are the key factors to scale our application. You can use child process to run system commands, read large files without blocking event loop, decompose the application into various “nodes” (That’s why it’s called Node).
Child process module has following three major ways to create child processes –
* spawn - child_process.spawn launches a new process with a given command.
* exec - child_process.exec method runs a command in a shell/console and buffers the output.
* fork - The child_process.fork method is a special case of the spawn() to create child processes.
**Source:** _codeforgeek.com_
### Q15: List out the differences between AngularJS and NodeJS? ☆☆
**Answer:**
AngularJS is a web application development framework. It’s a JavaScript and it is different from other web app frameworks written in JavaScript like jQuery. NodeJS is a runtime environment used for building server-side applications while AngularJS is a JavaScript framework mainly useful in building/developing client-side part of applications which run inside a web browser.
**Source:** _a4academics.com_
### Q16: How you can monitor a file for modifications in Node.js ? ☆☆
**Answer:**
We can take advantage of File System `watch()` function which watches the changes of the file.
**Source:** _codingdefined.com_
### Q17: What are the core modules of Node,js? ☆☆
**Answer:**
* EventEmitter
* Stream
* FS
* Net
* Global Objects
**Source:** _github.com/jimuyouyou_
### Q18: What is V8? ☆☆
**Answer:**
The V8 library provides Node.js with a JavaScript engine (a program that converts Javascript code into lower level or machine code that microprocessors can understand), which Node.js controls via the V8 C++ API. V8 is maintained by Google, for use in Chrome.
The Chrome V8 engine :
* The V8 engine is written in C++ and used in Chrome and Nodejs.
* It implements ECMAScript as specified in ECMA-262.
* The V8 engine can run standalone we can embed it with our own C++ program.
**Source:** _nodejs.org_
### Q19: What is libuv? ☆☆
**Answer:**
**libuv** is a C library that is used to abstract non-blocking I/O operations to a consistent interface across all supported platforms. It provides mechanisms to handle file system, DNS, network, child processes, pipes, signal handling, polling and streaming. It also includes a thread pool for offloading work for some things that can't be done asynchronously at the operating system level.
**Source:** _nodejs.org_
### Q20: What is the difference between returning a callback and just calling a callback? ☆☆
**Answer:**
```js
return callback();
//some more lines of code; - won't be executed
callback();
//some more lines of code; - will be executed
```
Of course returning will help the context calling async function get the value returned by callback.
```js
function do2(callback) {
log.trace('Execute function: do2');
return callback('do2 callback param');
}
var do2Result = do2((param) => {
log.trace(`print ${param}`);
return `return from callback(${param})`; // we could use that return
});
log.trace(`print ${do2Result}`);
```
Output:
```sh
C:\Work\Node>node --use-strict main.js
[0] Execute function: do2
[0] print do2 callback param
[0] print return from callback(do2 callback param)
```
**Source:** _stackoverflow.com_
### Q1: What is REPL in context of Node? ☆☆☆
**Answer:**
**REPL** stands for Read Eval Print Loop and it represents a computer environment like a window console or unix/linux shell where a command is entered and system responds with an output. Node.js or Node comes bundled with a REPL environment. It performs the following desired tasks.
* **Read** \- Reads user's input, parse the input into JavaScript data-structure and stores in memory.
* **Eval** \- Takes and evaluates the data structure
* **Print** \- Prints the result
* **Loop** \- Loops the above command until user press ctrl-c twice.
**Source:** _tutorialspoint.com_
### Q2: What is Callback? ☆☆☆
**Answer:**
**Callback** is an asynchronous equivalent for a function. A callback function is called at the completion of a given task. Node makes heavy use of callbacks. All APIs of Node are written is such a way that they supports callbacks.
For example, a function to read a file may start reading file and return the control to execution environment immediately so that next instruction can be executed. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as parameter. So there is no blocking or wait for File I/O.
This makes Node.js highly scalable, as it can process high number of request without waiting for any function to return result.
**Source:** _tutorialspoint.com_
### Q3: What is a blocking code? ☆☆☆
**Answer:**
If application has to wait for some I/O operation in order to complete its execution any further then the code responsible for waiting is known as blocking code.
**Source:** _tutorialspoint.com_
### Q4: How Node prevents blocking code? ☆☆☆
**Answer:**
By providing callback function. Callback function gets called whenever corresponding event triggered.
**Source:** _tutorialspoint.com_
### Q5: What is Event Loop? ☆☆☆
**Answer:**
Node.js is a single threaded application but it support concurrency via concept of event and callbacks. As every API of Node js are asynchronous and being a single thread, it uses async function calls to maintain the concurrency. Node uses observer pattern. Node thread keeps an event loop and whenever any task get completed, it fires the corresponding event which signals the event listener function to get executed.
**Source:** _tutorialspoint.com_
### Q6: What is Event Emmitter? ☆☆☆
**Answer:**
All objects that emit events are members of EventEmitter class. These objects expose an `eventEmitter.on()` function that allows one or more functions to be attached to named events emitted by the object.
When the EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously.
```js
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');
```
**Source:** _tutorialspoint.com_
### Q7: What is purpose of Buffer class in Node? ☆☆☆
**Answer:**
**Buffer** class is a global class and can be accessed in application without importing buffer module. A Buffer is a kind of an array of integers and corresponds to a raw memory allocation outside the V8 heap. A Buffer cannot be resized.
**Source:** _tutorialspoint.com_
### Q8: What is difference between synchronous and asynchronous method of fs module? ☆☆☆
**Answer:**
Every method in `fs` module has synchronous as well as asynchronous form. Asynchronous methods takes a last parameter as completion function callback and first parameter of the callback function is error. It is preferred to use asynchronous method instead of synchronous method as former never block the program execution where the latter one does.
**Source:** _tutorialspoint.com_
### Q9: What are streams? ☆☆☆
**Answer:**
Streams are objects that let you read data from a source or write data to a destination in continuous fashion. In Node.js, there are four types of streams.
* **Readable** \- Stream which is used for read operation.
* **Writable** \- Stream which is used for write operation.
* **Duplex** \- Stream which can be used for both read and write operation.
* **Transform** \- A type of duplex stream where the output is computed based on input.
**Source:** _tutorialspoint.com_
### Q10: What is Chaining in Node? ☆☆☆
**Answer:**
**Chanining** is a mechanism to connect output of one stream to another stream and create a chain of multiple stream operations. It is normally used with piping operations.
**Source:** _tutorialspoint.com_
### Q11: What is the purpose of setTimeout function? ☆☆☆
**Answer:**
The `setTimeout(cb, ms)` global function is used to run callback `cb` after at least `ms` milliseconds. The actual delay depends on external factors like OS timer granularity and system load. A timer cannot span more than 24.8 days.
**Source:** _tutorialspoint.com_
### Q12: How can you avoid callback hells? ☆☆☆
**Answer:**
To do so you have more options:
* **modularization**: break callbacks into independent functions
* use _Promises_
* use `yield` with _Generators_ and/or _Promises_
**Source:** _tutorialspoint.com_
### Q13: What's the event loop? ☆☆☆
**Answer:**
**The event loop** is what allows Node.js to perform non-blocking I/O operations — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible.
<div class="text-center">
<img src="https://i.stack.imgur.com/Lbs9z.png" class="img-fluid">
</div>
Every I/O requires a callback - once they are done they are pushed onto the event loop for execution. Since most modern kernels are multi-threaded, they can handle multiple operations executing in the background. When one of these operations completes, the kernel tells Node.js so that the appropriate callback may be added to the poll queue to eventually be executed.
**Source:** _blog.risingstack.com_
### Q14: How to avoid callback hell in Node.js? ☆☆☆
**Answer:**
Node.js internally uses a single-threaded event loop to process queued events. But this approach may lead to blocking the entire process if there is a task running longer than expected.
Node.js addresses this problem by incorporating callbacks also known as higher-order functions. So whenever a long-running process finishes its execution, it triggers the callback associated.
sometimes, it could lead to complex and unreadable code. More the no. of callbacks, longer the chain of returning callbacks would be.
There are four solutions which can address the callback hell problem.
**Make your program modular**
It proposes to split the logic into smaller modules. And then join them together from the main module to achieve the desired result.
**Use async mechanism**
It is a widely used Node.js module which provides a sequential flow of execution.
The async module has <async.waterfall> API which passes data from one operation to other using the next callback.
Another async API <async.map> allows iterating over a list of items in parallel and calls back with another list of results.
With the async approach, the caller’s callback gets called only once. The caller here is the main method using the async module.
**Use promises mechanism**
Promises give an alternate way to write async code. They either return the result of execution or the error/exception. Implementing promises requires the use of <.then()> function which waits for the promise object to return. It takes two optional arguments, both functions. Depending on the state of the promise only one of them will get called. The first function call proceeds if the promise gets fulfilled. However, if the promise gets rejected, then the second function will get called.
**Use generators**
Generators are lightweight routines, they make a function wait and resume via the yield keyword. Generator functions uses a special syntax <function* ()>. They can also suspend and resume asynchronous operations using constructs such as promises or <thunks> and turn a synchronous code into asynchronous.
**Source:** _techbeamers.com_
### Q15: Explain how does Node.js work? ☆☆☆
**Answer:**
A Node.js application creates a single thread on its invocation. Whenever Node.js receives a request, it first completes its processing before moving on to the next request.
Node.js works asynchronously by using the event loop and callback functions, to handle multiple requests coming in parallel. An Event Loop is a functionality which handles and processes all your external events and just converts them to a callback function. It invokes all the event handlers at a proper time. Thus, lots of work is done on the back-end, while processing a single request, so that the new incoming request doesn’t have to wait if the processing is not complete.
<div class="text-center"/>
<img src="https://csharpcorner-mindcrackerinc.netdna-ssl.com/article/node-js-event-loop/Images/1.png" class="img-fluid" style="max-width: 800px" />
</div>
While processing a request, Node.js attaches a callback function to it and moves it to the back-end. Now, whenever its response is ready, an event is called which triggers the associated callback function to send this response.
**Source:** _techbeamers.com_
### Q16: When should we use Node.js? ☆☆☆
**Answer:**
**Node.js** is well suited for applications that have a lot of concurrent connections and each _request only needs very few CPU cycles_, because the event loop (with all the other clients) is blocked during execution of a function. I believe Node.js is best suited for real-time applications: online games, collaboration tools, chat rooms, or anything where what one user (or robot? or sensor?) does with the application needs to be seen by other users immediately, without a page refresh.
**Source:** _techbeamers.com_
### Q17: How does Node.js handle child threads? ☆☆☆
**Answer:**
Node.js, in its essence, is a single thread process. It does not expose child threads and thread management methods to the developer. Technically, Node.js does spawn child threads for certain tasks such as asynchronous I/O, but these run behind the scenes and do not execute any application JavaScript code, nor block the main event loop.
If threading support is desired in a Node.js application, there are tools available to enable it, such as the ChildProcess module.
**Source:** _lazyquestion.com_
### Q18: What is the preferred method of resolving unhandled exceptions in Node.js? ☆☆☆
**Answer:**
Unhandled exceptions in Node.js can be caught at the `Process` level by attaching a handler for `uncaughtException` event.
```js
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
});
```
However, `uncaughtException` is a very crude mechanism for exception handling and may be removed from Node.js in the future. An exception that has bubbled all the way up to the `Process` level means that your application, and Node.js may be in an undefined state, and the only sensible approach would be to restart everything.
The preferred way is to add another layer between your application and the Node.js process which is called the [domain](http://nodejs.org/api/domain.html).
Domains provide a way to handle multiple different I/O operations as a single group. So, by having your application, or part of it, running in a separate domain, you can safely handle exceptions at the domain level, before they reach the `Process` level.
**Source:** _lazyquestion.com_
### Q19: What is stream and what are types of streams available in Node.js? ☆☆☆
**Answer:**
**Streams** are a collection of data that might not be available all at once and don’t have to fit in memory. Streams provide chunks of data in a continuous manner. It is useful to read a large set of data and process it.
There is four fundamental type of streams:
* Readable.
* Writeable.
* Duplex.
* Transform.
Readable streams as the name suggest used in reading a large chunk of data from a source. Writable streams are used in writing a large chunk of data to the destination.
Duplex streams are both readable and writable ( Eg socket). Transform stream is the duplex stream which is used in modifying the data (eg zip creation).
**Source:** _codeforgeek.com_
### Q20: What are the global objects of Node.js? ☆☆☆
**Answer:**
These objects are available in all modules:
* **process** - The process object is a global that provides information about, and control over, the current Node.js process.
* **console** - Used to print to stdout and stderr.
* **buffer** - Used to handle binary data.
**Source:** _github.com/jimuyouyou_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment