Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

Node.JS Interview Questions and Answers For Freshers Part-4

Be First!

nodejs31.What are streams?
Streams are objects that let you read data from a source or write data to a destination in continous fashion.

32.How many types of streams are present in Node.
In Node.js, there are four types of streams.

Readable – Stream which is used for read operation.

Writable – Stream which is used for write operation.

Duplex – Stream which can be used for both read and write operation.

Transform – A type of duplex stream where the output is computed based on input.

33.Name some of the events fired by streams.
Each type of Stream is an EventEmitter instance and throws several events at different instance of times. For example, some of the commonly used events are:

data – This event is fired when there is data is available to read.

end – This event is fired when there is no more data to read.

error – This event is fired when there is any error receiving or writing data.

finish – This event is fired when all data has been flushed to underlying system

34.What is Chaining in Node?
Chanining is a mechanism to connect output of one stream to another stream and create a chain of multiple stream operations. It is normally used with piping operations.

35.How will you open a file using Node?
Following is the syntax of the method to open a file in asynchronous mode:

fs.open(path, flags[, mode], callback)
Parameters
Here is the description of the parameters used:

path – This is string having file name including path.

flags – Flag tells the behavior of the file to be opened. All possible values have been mentioned below.

mode – This sets the file mode (permission and sticky bits), but only if the file was created. It defaults to 0666, readable and writeable.

callback – This is the callback function which gets two arguments (err, fd).

36.How will you read a file using Node?
Following is the syntax of one of the methods to read from a file:

fs.read(fd, buffer, offset, length, position, callback)
This method will use file descriptor to read the file, if you want to read file using file name directly then you should use another method available.

Parameters
Here is the description of the parameters used:

fd – This is the file descriptor returned by file fs.open() method.

buffer – This is the buffer that the data will be written to.

offset – This is the offset in the buffer to start writing at.

length – This is an integer specifying the number of bytes to read.

position – This is an integer specifying where to begin reading from in the file. If position is null, data will be read from the current file position.

callback – This is the callback function which gets the three arguments, (err, bytesRead, buffer).

37.How will you write a file using Node?
Following is the syntax of one of the methods to write into a file:

fs.writeFile(filename, data[, options], callback)
This method will over-write the file if file already exists. If you want to write into an existing file then you should use another method available.

Parameters
Here is the description of the parameters used:

path – This is string having file name including path.

data – This is the String or Buffer to be written into the file.

options – The third parameter is an object which will hold {encoding, mode, flag}. By default encoding is utf8, mode is octal value 0666 and flag is ‘w’

callback – This is the callback function which gets a single parameter err and used to to return error in case of any writing error.

38.How will you close a file using Node?
Following is the syntax of one of the methods to close an opened file:

fs.close(fd, callback)
Parameters
Here is the description of the parameters used:

fd – This is the file descriptor returned by file fs.open() method.

callback – This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

39.How will you get information about a file using Node?
Following is the syntax of the method to get the information about a file:

fs.stat(path, callback)
Parameters
Here is the description of the parameters used:

path – This is string having file name including path.

callback – This is the callback function which gets two arguments (err, stats) where stats is an object of fs.Stats type which is printed below in the example.

40.How will you truncate a file using Node?
Following is the syntax of the method to truncate an opened file:

fs.ftruncate(fd, len, callback)
Parameters
Here is the description of the parameters used:

fd – This is the file descriptor returned by file fs.open() method.

len – This is the length of the file after which file will be truncated.

callback – This is the callback function which gets no arguments other than a possible exception are given to the completion callback.

(151)

Previous
Next

Leave a Reply