Thursday 2 July 2020

Node JS Interview Questions and Answers

Node JS Interview Questions and Answers

Question: How to write in files synchronous in Node.js?
const fs = require('fs');
fs.writeFileSync('/fold/test.txt', 'Write in file');



Question: What is the difference between --save and --save-dev?
--save-dev is used to save the package for development purpose. Example: unit tests, minification
--save is used to save the package required for the application to run.



Question: is node single threaded or multithreaded?
It is a single threaded language which in background uses multiple threads to execute asynchronous code. Also its is non-blocking which means that all functions ( callbacks ) are delegated to the event loop and they are ( or can be ) executed by different threads.


Question: How to print console without a trailing newline?
    process.stdout.write("hello: ");



Question: How to create a directory if it doesn't exist using Node.js?
var fs = require('fs');
var dir = './tmp';
if (!fs.existsSync(dir)){
    fs.mkdirSync(dir);
}



Question: How can we execute binary/exe files in Node ?
const { exec } = require('child_process');
exec('wc -l', (err, stdout, stderr) => {
  if (err) {
    // node couldn't execute the command
    return;
  }
});



Question: Is there a way to get version from package.json?
var pjson = require('./package.json');
console.log(pjson.version);



Question: How can I update npm on Windows?
Simple way
Download and run the latest MSI from nodejs.org. The MSI will update your installed node and npm.


Question: How can node in background?
nohup node server.js &



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



Question: How to get the full url in Express?
var url = require('url');
function fullUrl(req) {
  return url.format({
    protocol: req.protocol,
    host: req.get('host'),
    pathname: req.originalUrl
  });
}




Wednesday 1 July 2020

Node JS Interview Questions and Answers for 3 Years experience



Question: What is the difference between tilde(~) and caret(^) in package.json?
~ update you to all future patch versions, without incrementing the minor version. ~2.2.3 will use releases from 2.2.3 to 2.3.0.
^ update you to all future minor/patch versions, without incrementing the major version. ^2.3.3 will use releases from 2.3.3 to 3.0.0.



Question: How do I pass command line arguments to a Node.js program?
In Node - How to pass arguent
node process-2.js one two=three four
In Node - How to get argument value
process.argv



Question: How do I update each dependency in package.json to the latest version?
npm i -g npm-check-updates
ncu -u
npm install



Question: How can I update NodeJS and NPM to the next versions?
npm install -g npm



Question: How to write in files synchronous in Node.js?
const fs = require('fs');
fs.writeFileSync('/fold/test.txt', 'Write in file');



Question: How to read environment variables?
var mode   = process.env.NODE_ENV;
var apiKey = process.env.apiKey; 



Question: How to parse JSON using Node.js?
//Parse JSON Data
var bodyParser = require('body-parser')
app.use(bodyParser.json());       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({// to support URL-encoded bodies
    extended: true
}));



Question: How  an HTTP POST request made?
var request = require('request');
request.post(
    'https://www.example.com',
    { json: { key: 'value' } },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        }
    }
);



Question: How can I get the full object in Node.js's console.log(), rather than Object?
const util = require('util');
console.log(util.inspect(myObject, {showHidden: false, depth: null}));

OR
console.log(JSON.stringify(myObject, null, 4));




Question: How to encode a text using base64?
Buffer.from("Hello World").toString('base64'); //SGVsbG8gV29ybGQ=


Question: How to decoded a text which is encoded with base64?
Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'); //Hello World