Friday 22 September 2017

Nodejs Interview Questions and Answer 1 year Experience

Nodejs Interview Questions and Answer 1 year Expereince

Question: How do I include a JavaScript file in another file?

Suppose you have following file with hello.js which you want to include.
export function hello() {
  return "Hello, How are you?";

Use following syntax to include hello.js?
const helloFileObj = require('./hello');
console.log(helloFileObj.hello()); //It would print Hello, How are you?

Question: What's the difference between tilde(~) and caret(^) in package.json?

the tilde(~) matches the most recent minor version.
For Example ~1.4.3 will match all 1.4.x versions but will miss 1.5.0.
Benefits: Gives you bug fix releases.

the caret(^) matches the most recent major version.
For Example ^1.4.3 will match all 1.x.x versions including 1.5.0 but will hold off on 2.0.0
Benefits: Gives you bug fixes including new functionalities.

Question: How do I pass command line arguments?
node server.js one two=222 three
Here one, two=222 and three are command line arguments (1,2,3 respectively).

Following are way to access argument process.argv is array containing the command line arguments
Following are the output when we print process.argv
Script to print the arguments

0: node
1: server.js
2: one
3: two=222
4: three

Question: How to access 2nd argument when passed in command line?
process.argv.slice(3,4); //two=222

Question: How to list the packages?
List Local packages?
npm list

List Global packages?
npm list -g

Question: How to exit from node Code?

Question: Read environment variables in Node.js?

Question: How to generate Secure random token Node.js?
For this you can use crypto module in nodeJS.
require('crypto').randomBytes(48, function(err, buffer) {
  var token = buffer.toString('hex');

Question: How to output pretty html in Express?
app.locals.pretty = true;

Question: How to clean node_modules folder of packages that are not in package.json?
app.locals.pretty = true;

Question: How to format a date string in UTC Format?
new Date().toISOString(); //2017-09-04T14:51:06.157Z

new Date().toISOString().
  replace(/T/, ' ').      // replace T 
  replace(/\..+/, ''); //2017-09-04 14:55:45 

Question: How to get client IP Address in Node JS?
app.get(/save-data, function (req, res){
    var ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;   
    console.log(ipAddress); //will print the ip address of client

Question: How to force strict mode in node?
Write below code at the top of node file with enclosed with double quote.
use strict;

Question: What is promises in node js?
We use the promises for asynchronous operation and it have following 3 states.
  1. Pending
  2. Resolved
  3. Rejected

Question: What is promises in node js?
var somePromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('Operation completed');
    // reject(Operation Failed');
  }, 2500);

somePromise.then((message) => {
  console.log('Success: ', message);
}, (errorMessage) => {
  console.log('Error: ', errorMessage);

Promise contain two parameter i.e. resolve(for success), reject(for failed).

Question: What is Yargs?
Yargs helps to parsing arguments of command line tools.

Question: How to install Yargs module in node?
npm i yargs --save

Question: Give example of Yargs module?
const argv = require('yargs').argv

How to pass arguments
node test.js --name="Arun" --age="28" --city="Chandigarh"