Wednesday, 16 May 2018

Node Js Interview Questions and answer for 2 year experienced

Node Js Interview Questions and answer for 2 year experienced

Question: What is JSON.stringify()?
JSON.stringify() method converts a JavaScript object to a JSON string.
Following are three parameter of JSON.stringify.


Value (required): The value to convert to a JSON string.
replacer (optional): A function that alters the behavior of the stringification process, or an array of String and Number objects.
space (optional): A String or Number object that's used to insert white space into the output JSON string for readability purposes.



Question: Give example of JSON.stringify()?
JSON.stringify({});                    // '{}'
JSON.stringify(true);                  // 'true'
JSON.stringify('foo');                 // '"foo"'
JSON.stringify([1, 'false', false]);   // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 });              // '{"x":5}'



Question: What is JSON.parse()?
JSON.parse() method converts JSON string to JavaScript object.


Question: What is arrow function?
An arrow function expression has a shorter syntax than a regarular function and does not have its own this, arguments, super, or new.target.


Question: Give example of arrow function?
var materials = [
  'Hydrogen',
  'Helium',
  'Beryllium'
];
console.log(materials.map(material => material.length));// expected output: Array [8, 6, 9]


Question: What is Handlebars view engine in nodejs?
Handlebars.js is templating engine in NodeJS. We give the data from node and same used in the handlebar template.



Question: How to configure handlebar?
const hbs = require('hbs');
app.set('view engine', 'hbs');

Now you can place the html files in view folder with extension of .hbs



Question: How to pass the data to handlebar engine and used in view file?
Pass parameter
app.get('/aboutus', (req, res) => {
  res.render('home.hbs', {
    pageTitle: 'About Page Title',
    welcomeMessage: 'Welcome'
  });
});

aboutus.hbs (Use with variable name enclosed with double curly braces)
{{pageTitle}} //About Page Title
{{welcomeMessage}} //Welcome



Question: How to create element(partial) file in handlerbar ?
In node server file, set the following
hbs.registerPartials(__dirname + '/views/partials');

You can call a comman file from another file with following system
{{> header}}

Here header file path is /views/partials/header.hbs


Question: What is an error-first callback?
Error-first callbacks are used to pass errors and data. You have to pass the error as the first parameter, and it has to be checked to see if something went wrong.
fs.readFile(filePath, function(err, data) {  
  if (err) {
    // handle the error, the return is important here
    // so execution stops here
    return console.log(err)
  }
  // use the data object
  console.log(data)
})



Question: What are Promises?
The Promise object represents the event completion or failure of an asynchronous operation, and its resulting value.
function readFile(filename, enc){
  return new Promise(function (resolve, reject){
    fs.readFile(filename, enc, function (err, res){
      if (err) reject(err);
      else resolve(res);
    });
  });
}



Question: What's a stub? Name a use case?
Stubs are functions that simulate the behaviors of components/modules.
Stubs provide canned answers to function calls made during test cases.
var fs = require('fs')

var writeFileStub = sinon.stub(fs, 'writeFile', function (path, data, cb) {  
  return cb(null)
})

expect(writeFileStub).to.be.called
writeFileStub.restore();



Question: What is callback hell in Node.js?
Callback hell is the result of heavily nested callbacks that make the code not only unreadable but also difficult to maintain.


Question: What is the role of REPL in Node.js?
Full form of REPL is REPL Read Eval print Loop.
The REPL in Node.js is used to execute ad-hoc Javascript statements.



Question: Name the types of API functions in Node.js?
Blocking functions In a blocking operation, all other code is blocked from executing until an I/O event that is being waited on occurs. Blocking functions execute synchronously.

Non-Blocking functions
In a non-blocking operation, multiple I/O calls can be performed without the execution of the program being halted.


Question: Explain chaining in Node.js?
Chaining is a mechanism whereby the output of one stream is connected to another stream creating a chain of multiple stream operations.


Question: How to get the node version?
node -v



Question: How to access the GET parameters after ? in Express?
URL: /?name=myname&age=32&gender=m
req.query.name;//myname

req.query.age;//32

req.query.gender; //m



Question: What is the difference between __dirname and ./ in node.js?
__dirname is always the directory in which the currently executing script.
. gives you the directory from which you ran the node command in your terminal window


No comments :

Post a Comment