Question-31. How do you create a simple server in Node.js that returns Hello World?
- Import the HTTP module
- Use createServer function with a callback function using request and response as parameters.
- Type “hello world.”
- Set the server to listen to port 8080 and assign an IP address
Question-32. Explain asynchronous and non-blocking APIs in Node.js.
- All Node.js library APIs are asynchronous, which means they are also non-blocking
- A Node.js-based server never waits for an API to return data. Instead, it moves to the next API after calling it, and a notification mechanism from a Node.js event responds to the server for the previous API call
Question-33. How do we implement async in Node.js?
Answer- As shown below, the async code asks the JavaScript engine running the code to wait for the request.get() function to complete before moving on to the next line for execution.
Question-34. What is a callback function in Node.js?
Answer- A callback is a function called after a given task. This prevents any blocking and enables other code to run in the meantime.
In the last section, we will now cover some of the advanced-level Node.js interview questions.
Question-35. What is REPL in Node.js?
Answer- REPL stands for Read Eval Print Loop, and it represents a computer environment. It’s similar to a Windows console or Unix/Linux shell in which a command is entered. Then, the system responds with an output
Question-36. What is the control flow function?
Answer- The control flow function is a piece of code that runs in between several asynchronous function calls.
Question-37. How does control flow manage the function calls?
Question-38. What is the difference between fork() and spawn() methods in Node.js?
fork() | spawn() |
fork() is a particular case of spawn() that generates a new instance of a V8 engine. | Spawn() launches a new process with the available set of commands. |
Multiple workers run on a single node code base for multiple tasks. | This method doesn’t generate a new V8 instance, and only a single copy of the node module is active on the processor. |
Question-39. What is the buffer class in Node.js?
Answer- Buffer class stores raw data similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. Buffer class is used because pure JavaScript is not compatible with binary data.
Question-40. What is piping in Node.js?
Answer- Piping is a mechanism used to connect the output of one stream to another stream. It is normally used to retrieve data from one stream and pass output to another stream