a blog for those who code

Sunday, 2 April 2017

Top 20 Interview Questions on Node.js

In this post we will be discussing about Top 20 Interview Questions on Nodejs. If you are a MEAN stack developer and you are looking to know some of the best interview questions on Node.js, you came to the best place. The interview questions on Node.js is just to get acquainted with the nature of questions you may encounter during your interview.

1. What is Node.js ?

Node.js is a platform for building fast, scalable network application. Its advantages over other server side languages is that it uses event-driven, non-blocking I/O model that makes it light-weight and efficient.

2. Can you explain how Node.js works ?

It uses Google V8 Javascript engine to execute code. It contains built-in asynchronous I/O library for file, socket and HTTP communication. Node.js encapsulates libuv to handle asynchronous events.

3. Is Node.js really Single-Threaded ?

Node.js operates on single-thread, but using non-blocking I/O calls allows it to support many concurrent connections. That means node doesn't process the requests in parallel but all the back-end stuffs which actually takes lot of time run in parallel.

4. Can you explain the Asynchronous approach in Node.js ?

Node.js operates asynchronously using event loop and callback functions.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 your event handlers at a proper time. So, that means while executing a single request, it does a lot of things in the back-end so that the current request or the coming request doesn't take much time.

You can read more here - Asynchronous approach in Nodejs

5. Can you explain what is Globals in Node.js ?

Global, Process and Buffer are combinedly termed as Globals.

Global : Its a global namespace object
Process : Its also a global object but it provides essential functionality to transform a synchronous function into a asynchronous callback.
Buffer : Raw data is stored in instances of the Buffer class.

You can read more here - Globals in Nodejs

6. What is the Use of underscore in Node.js?

To access the last expression, we have to use the (_) underscore/underline character.

You can read more here - Use of Underscore (_) in Nodejs

7. Can you create Http Server in Node.js, explain with code ?

Yes, we can create Http Server in Nodejs. We can use http-server command to do so.

Code :

var http = require('http');
var requestListener = function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello You\n');

var server = http.createServer(requestListener);

server.listen(8080); // The port where you want to start with.

8. How to load HTML in Nodejs ?

To load HTML in Nodejs we have to change the Content-type from text/plain to text/html.

You can read more here - Loading HTML in NodeJS

9. Can you explain the difference between Node.js vs Ajax ?

The difference between Node.js and Ajax is that Ajax is a client side technology whereas Nodejs is server side technology. Ajax is used for updating the contents of the page without refreshing it whereas Nodejs is used for developing server software. Nodejs is executed by the server whereas Ajax is executed by the browser.

10. Can you explain the difference between readFile vs createReadStream in Node.js ?

readFile - It will read the file completely into memory before making it available to the User.
createReadStream - It will read the file in chunks of the size which is specified before hand.

You can read more here - readFile vs createReadStream in Nodejs

11. What is Callback in context of Node.js ?

A callback is an asynchronous equivalent for a function which is being called when a given task is completed. Node.js makes heavy use of callbacks, all the API's are written such as way that it supports callbacks. The advantage of using callback is that it makes Node.js highly scalable i.e. processing high number of request without waiting for any function to complete. 

12. What is Callback Hell and how to avoid it ?

Callback hell means a heavily nested callbacks which make the code unreadable and difficult to maintain. To avoid callback hell one should use modularization, which means breaking the callbacks into independent functions. Another method to avoid callback hell is use to use Promises, which allows error propagation and chaining.

13. What is Event Loop and Event Emitter ?

Node.js supports concurrency with the help of events and callbacks even if it is single threaded application. Node thread keeps an event loop and whenever any task gets completed, that thread fires the corresponding event.

EventEmitter fires an event whenever any tasks gets completed, any error occurred, any new listener is added or any listener is removed. It provides properties like on and emit, where on is used to bind the function and emit is used to fire an event.

14. How many types of Streams are present in Node.js ?

There are four types of streams are present in Node.js and they are ReadableWritableDuplex and Transform. Readable stream is used for read operation, Writable for write operation, Duplex for both read and write operation and Transform is type of Duplex stream where output is computed based on input.

15. Why to use Buffers instead of binary strings to handle binary data ?

Pure JavaScript does not able to handle straight binary data very well. Since Node.js servers have to deal with TCP streams for reading and writing of data, binary strings will become problematic to work with as it is very slow and has a tendency to break. That's why it is always advisable to use Buffers instead of binary strings to handle binary data.

16. How to gracefully Shutdown Node.js Server ?

We can gracefully shutdown Node.js server by using the generic signal called SIGTERM or SIGINT which is used for program termination. We need to call SIGTERM or SIGINT which will terminate the program and clean up the resources utilized by the program.

17. What are Error First Callbacks in Node.js

The "error-first" callback is s standard protocol for Node.js Callbacks which has a simple rule that the first argument for the callback function should be an error object. If the error argument is not null, then the operations was not successful, error has occurred and if the error argument is null then operation is successful.

18. What is the difference between process.nextTick() and setImmediate() ?
The difference between process.nextTick() and setImmediate() is that process.nextTick() defers the execution of an action till the next pass around the event loop or it simply calls the callback function once the ongoing execution of the event loop is finished whereas setImmediate() executes a callback on the next cycle of the event loop and it gives back to the event loop for executing any I/O operations.

19. How you can monitor a file for modifications in Node.js ?

We can take advantage of File System watch() function which watches the changes of the file.

20. How to solve "Process out of Memory Exception" in Node.js ?

To solve the process out of memory exception in Node.js we need to increase the max-old-space-size. By default the max size of max-old-space-size is 512 mb which you can increase by the command node --max-old-space-size=1024 file.js.

Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful. 

No comments:

Post a Comment

Contact Us


Email *

Message *


Best Hosting Plans

CC License