Thursday, 6 July 2017

Node Js Interview Questions and answer for Experienced

Node Js Interview Questions and answer for Experienced

Question: What is objectId in MongoDB?
It is 12 byte object consist of following
  1. a 4-byte value representing the seconds since the Unix epoch
  2. a 3-byte machine identifier
  3. a 2-byte process id
  4. a 3-byte counter, starting with a random value.



Question: How to get timestamp from objectId?
ObjectId.getTimestamp();



Question: What is difference between npm install vs npm update?
npm install:
1. installs all modules that are listed on package.json file and their dependencies.
2. module without version mention in package.json, npm install will be ignore if already installed.

npm update:
1. updates all packages in the node_modules directory and their dependencies.
2. module without version mention in package.json, will update to latest version (If not then installed the latest version).


Question: How to set npm start with start the application?
  1. Open package.json which is located on root.
  2. Add following code in package.json (If not added)
      "scripts": {
        "start": "node index.js"
      }
    

    Please repace "index.js" with your application file.
  3. Now, you can run npm start from command console.



Question: How to destroy the session in node?
You can use req.session.destroy(), See Exampe below:
exports.logout = function(req, res) {        
    req.session.destroy();
    res.send({logout:1});
}



Question: How to redirect to 404 errors page in ExpressJS?
After difining all the URL route, add the following code
app.use(function(req, res, next) {
    res.status(404).json({errorCode: 404, errorMsg: "route not found"});
});

When someone trying to access not available pages, it will show 404 error code.


Question: What is CORS?
Cross Origin Resource Sharing.


Question: What use use of CORS?
Suppose your are website is at mywebiste.com and want to access the javascript/API which is on mywebiste2.com. You can't access the data/api from mywebiste2.com unless they allow you. This is known as Cross Origin Resource Sharing.


Question: How we can achieve CORS?
Add the following code in your node application, it will allow everyone to access your code. In Access-Control-Allow-Origin, you can specify the domain name.
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});



Question: What do you mean by Asynchronous API?
All APIs of Node.js library are aynchronous, means all are non-blocking.


Question: What is package.json?
package.json is present in the root directory of any Node application/module and is used to define the properties of a package.


Question: What is Piping in Node?
Piping is a mechanism to connect output of one stream to input of another stream. It means get data from one stream and to pass output of that stream to another stream and there is no limit to piping operations.


Question: What is Callback in node.js?
Callback function is used in node.js to deal with multiple requests made to the server. Node is single thread based application and works on the behalf of events.


No comments :

Post a Comment