Node.js interview questions set 2
5)What is buffer in node.js?
Buffer is mainly used to store binary data while reading from a file or receiving packets over the network.
Buffer class is a global class that can be accessed in an application without importing the buffer module.
Ex:var buf = new Buffer(10);
6)Explain the concept of URL module in Node.js?
Ans: The URL module in Node.js splits up a web address into readable parts.
Use require() to include the module:
7)Is Node.js a single thread?
Ans: Yes, it is true that Node.js processes all requests on a single thread. But it is just a part of the theory behind Node.js design. In fact, more than the single thread mechanism, it makes use of events and callbacks to handle a large no. of requests asynchronously.
To explain it practically, let’s assume there are 100s of requests lined up in the Node.js queue. As per the design, the main thread of the Node.js event loop will receive all of them and forward them to background workers for execution. Once the workers finish processing requests, the registered callbacks get notified on the event loop thread to pass the result back to the user.
8)Explain callback hell in Node.js?
An asynchronous function is one where some external activity must complete before a result can be processed; it is “asynchronous” in the sense that there is an unpredictable amount of time before a result becomes available. Such functions require a callback function to handle errors and process the result.
How to avoid callback hell?