Ans: Node.js is a Server side scripting which is used to build scalable programs. Its multiple advantages over other server side languages, the prominent being non-blocking I/O.
Node.js can be used for the following purposes
Ans: It provides an easy way to build scalable network programs
The two types of API functions in Node.js are
Ans: A generic piece of code which runs in between several asynchronous function calls is known as control flow function.
Ans: For async processing, Node.js was created explicitly as an experiment. It is believed that more performance and scalability can be achieved by doing async processing on a single thread under typical web loads than the typical thread based implementation.
Ans: No, you cannot access DOM in node.
Ans:
Ans: Node.js is quickly gaining attention as it is a loop based server for JavaScript. Node.js gives user the ability to write the JavaScript on the server, which has access to things like HTTP stack, file I/O, TCP and databases.
Ans: Callback function is used in node.js to deal with multiple requests made to the server. Like if you have a large file which is going to take a long time for a server to read and if you don’t want a server to get engage in reading that large file while dealing with other requests, call back function is used. Call back function allows the server to deal with pending request first and call a function when it is finished.Refer our Node.js Course for an extra information.
Ans: “Express” is the most common framework used in node.js
Ans: To process and handle external events and to convert them into callback invocations an event loop is used. So, at I/O calls, node.js can switch from one request to another .
Ans: By following steps you can async Node.js
Ans:
Pros:
Cons:
Ans: Node.js solves this problem by putting the event based model at its core, using an event loop instead of threads.
Ans: The difference between Node.js and Ajax is that, Ajax (short for Asynchronous Javascript and XML) is a client side technology, often used for updating the contents of the page without refreshing it. While,Node.js is Server Side Javascript, used for developing server software. Node.js does not execute in the browser but by the server.
Ans: Emphasizing on the technical side, it’s a bit of challenge in Node.js to have one process with one thread to scale up on multi core server.
Ans: In node.js “non-blocking” means that its IO is non-blocking. Node uses “libuv” to handle its IO in a platform-agnostic way. On windows, it uses completion ports for unix it uses epoll or kqueue etc. So, it makes a non-blocking request and upon a request, it queues it within the event loop which call the JavaScript ‘callback’ on the main JavaScript thread.
Ans: Command “require” is used for importing external libraries, for example, “var http=require (“http”)”. This will load the http library and the single exported object through the http variable.
Ans: There is no right answer for this. The goal here is to understand how deeply one knows the framework she/he uses, if can reason about it, knows the pros, cons.
Ans: Operation errors are not bugs, but problems with the system, likerequest timeout or hardware failure.
On the other hand programmer errors are actual bugs.
Ans: Latest version of Node.js is - v0.10.36.
Ans: Below are the features of Node.js –
Ans: REPL stands for Read Eval Print Loop. Node.js comes with bundled REPL environment which performs the following desired tasks –
Ans: Variables are used to store values and print later like any conventional scripts. If “var” keyword is used then value is stored in variable. You can print the value in the variable using - console.log().
Eg:
$ node
> a = 30
30
> var b = 50
undefined
> a + b
80
> console.log("Hi")
Hi
undefined
Ans: Below are the list of REPL commands –
Ans: Command - ctrl + c twice is used to stop REPL.
Ans: NPM stands for Node Package Manager (npm) and there are two functionalities which NPM takes care of mainly and they are –
Ans: Below command can be used to verify the NPM version –
$ npm --version
Ans: Below commands can be used for updating NPM to new version –
$ sudo npm install npm -g
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
npm@2.7.1 /usr/lib/node_modules/npm
Ans: Callback is called once the asynchronous operation has been completed. Node.js heavily uses callbacks and all API’s of Node.js are written to support callbacks.
Ans: Node.js works good for I/O bound and not CPU bound work. For instance if there is a function to read a file, file reading will be started during that instruction and then it moves onto next instruction and once the I/O is done or completed it will call the callback function. So there will not be any blocking.
Ans: Globally installed dependencies or packages are stored in <user-directory>/npm directory and these dependencies can be used in Command Line Interface function of any node.js.
Ans: It’s a mechanism in which output of one stream will be connected to another stream and thus creating a chain of multiple stream operations.
Ans: Child process module has following three major ways to create child processes –
Ans: “exec” method runs a command in a shell and buffers the output. Below is the command –
child_process.exec(command[, options], callback)
Ans: Below are the list of parameters passed for Child Process Module –
child_process.exec(command[, options], callback)
callback – This is the function which is gets 2 arguments – stdout, stderr and error.
Ans: This method is used to launch a new process with the given commands. Below is the method signature –
child_process.spawn(command[, args][, options])
Ans: This method is a special case for method- “spawn()” for creating node processes. The method signature –
child_process.fork(modulePath[, args][, options])
Ans: This is a mechanism of connecting one stream to other and this is basically used for getting the data from one stream and pass the output of this to other stream.
Ans: There will not be any limit for piping stream.
Ans: Here FS stands for “File System” and fs module is used for File I/O. FS module can be imported in the following way –
var test = require("fs")
Ans: “Console” is a global object and will be used for printing to stderr and stdout and this will be used in synchronous manner in case of destination is either file or terminal or else it is used in asynchronous manner when it is a pipe.
Ans: This statement is used for printing to “stdout” with newline and this function takes multiple arguments as “printf()”.
Below are the useful properties of process –
Ans: The syntax to concatenate buffers in NodeJS is
var MyConctBuffer = Buffer.concat([myBuffer1, myBuffer2]);
Ans: To compare buffers in NodeJS, use following code –
Mybuffer1.compare(Mybuffer2);
Ans: Below is the syntax to copy buffers in NodeJS –
buffer.copy(targetBuffer[, targetStart][, sourceStart][, sourceEnd])
Ans:
Ans: This is the global function and it is used to run the callback after some milliseconds.
Syntax of this method –
setTimeout(callbackmethod, millisecs)