1. What ate the types of application you can build using Node Js ?
Using Node Js we can build the applications like:
Web Applications
Microservices / API’s
Internet of Things Applications
Real-Time Chats Applications
Complex Single-Page Applications
Real-Time Collaboration Tools
Streaming Applications
2. How to set the default node version using nvm?
By running the following command on the terminal to set default node version along multiple installed versions of node.
You can list all install versions of the node by running
nvm ls
nvm alias default v7.3.0
3. How can we generate unique UUIDs/ guid in Node Js ?
Use node-uuid package to generate unique UUIDs/ guid in Node Js. Below code demonstrates how to generate it.
var uuid = require('node-uuid');
// Generate a v1 (time-based) id
uuid.v1();
// Generate a v4 (random) id
uuid.v4();
4. What is libuv in Node Js ?
libuv is Cross-platform I/O abstraction library that supports asynchronous I/O based on event loops.
It is written in C and released under MIT Licence.
It supports Windows IOCP, epoll(4), kqueue(2), and Solaris event ports.
Initially, it was designed for Node.js but later it is also used by other software projects.
5. How Node js read the content of a file?
NodeJs reads the content of a file in non-blocking, asynchronous way.
Node Js uses its fs core API module to deal with files.
The easiest way to read the entire content of a file in nodeJs is with fs.readFile method.
Example to read a file in NodeJs asynchronously and synchronously.
Reading a file in node asynchronously/ non-blocking
var fs = require('fs');
fs.readFile('DATA', 'utf8', function(err, contents) {
console.log(contents);
});
console.log('after calling readFile');
Reading a file in node asynchronously/blocking
var fs = require('fs');
var contents = fs.readFileSync('DATA', 'utf8');
console.log(contents);
6. What is the revealing module pattern?
Revealing module pattern is similar to Module Pattern.
IT Exposes only the properties and methods you want via an returned Object.
var greeting = 'Hello world!'
function greet() {
console.log(greeting)
}
module.exports = {
greet: greet
}
7. What is JIT in Node JS ?
JIT stands for Just-in-time.
A JIT compiler is a program which is used to send bytecode (it consists of instruction that can be interpreted) to the processor by converting it into instruction.
After you have done with writing a program, the compiler compiles the source language statements into bytecode instead of compiling it into the code that carries the information which is similar to the specific hardware platform's processor.
Virtual machine of Nodejs has JIT compilation which improves the execution speed of the code.
The virtual machine takes the source code and converts to machine code in runtime.
By this, the hot functions which are called very often are compiled to machine code and, hence increasing speed.
8. How does Promise and Queue work?
Once a promise settles, or if it has already settled, it queues a microtask for its reactionary callbacks.
This ensures that promise callbacks are async even if the promise has already been settled.
Promise keeps its callback to the queue for microtasks.
Promise itself is not kept in the stack, but the Promise then (callback) is added to the queue of Microtasks, which are processed after the stack becomes empty.
Promise are not "kept in the message queue", which contains only micro-tasks. The event loop knows nothing about promises. It merely picks micro-tasks off the queue for execution.
9. What does emitter do and what is dispatcher?
Emitter fires the events for the defined action.
Dispatcher sends the action
10. What Is Package.Json? Who Uses It?
It keeps the metadata relevant to the project.
npm uses this package.json file to specify the version of a package that your app depends on.
This file contains the custom scripts to be run with npm command.
It can be generated by:
npm init