Thursday, 13 July 2017

Express JS Interview Questions and Answer for 1 Year Experienced

Express JS Interview Questions and Answer for 1 Year Experienced

Question: What Are Core Features Of Express Framework?
  1. Allows to set up middlewares to respond to HTTP Requests.
  2. Defines a routing.
  3. Dynamically render HTML Pages



Question: Why I Should Use Express Js?
It is light-weight server side web application framework which work on node.js platform.


Question: How to get The full Url In Express?
var port = req.app.settings.port || cfg.port;
res.locals.requested_url = req.protocol + '://' + req.host  + ':'+port + req.path;



Question: How to download a file?
app.get('/download', function(req, res){
  var file = __dirname + '/myfile/download.txt';
  res.download(file); 
});



Question: How To enable debugging In Express App?
In windows
set DEBUG=express:* & node index.js

In Linux
DEBUG=express:* node index.js


Question: How to render plain Html?
res.sendFile();
res.render();


Question: How to use handle get request in express Js?
var app = require('express')();
var http = require('http').Server(app);

app.get('/listing', function (req, res) {   
   res.send('This is listing API'); 
});

http.listen('8080', function() {
    console.log('listening on *:8080');
});



Question: How to receive get parameter in Express Js?
app.get('/listing', function (req, res) {   
    console.log(req.query);   //Receive get data
});



Question: How to receive post parameter in express Js?
app.post('/listing', function (req, res) {   
    console.log(req.body);   //Receive post data
});



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.


Question: How to send Ajax call in NodeJS?
Install the request
npm install request

request = require('request');
request( "http://exmaple.com:8081:/users/login/?username=testing@no-spam.ws&password=73278342", function(err, res, body) {                    
          console.log(res.console);          
      });



Question: How to get cookies in expressJs?
npm install cookie-parser
var express = require('express');
var app = express();

var cookieParser = require('cookie-parser');
app.use(cookieParser());

app.get('/listing', function (req, res) {   
    console.log(req.cookies);    
});



Question: What are different methods in REST API?
  1. GET : Used to read.
  2. POST: Used to update.
  3. PUT: Used to create.
  4. DELETE: Used to delete



Question: List out some REPL commands in Node.js?
  1. Ctrl + c - For terminating the current command.
  2. Ctrl + c twice – For terminating REPL.
  3. Ctrl + d - For terminating REPL.
  4. Tab Keys - list of all the current commands.
  5. .break - exit from multiline expression.
  6. .save with filename - save REPL session to a file.



Question: How you can update NPM to new version in Node.js?
sudo npm install npm -g



Question: What do you mean by "Callback hell"?
"Callback hell" is referred to heavily nested callbacks which has become unreadable.


Question: What are "Streams" in Node.JS?
Streams are objects which will let you read the data from source and write data to destination as a continuous process.


Question: What you mean by chaining in Node.JS?
It's a mechanism in which output of one stream will be connected to another stream and thus creating a chain of multiple stream operations.


Question: What is the use of method – spawn()?
This method is used to launch a new process.
child_process.spawn(command[, args][, options])



Tuesday, 11 July 2017

Express JS Interview Questions and Answer for experienced

Express JS Interview Questions and Answer for experienced

Question: What is meaning of app.get(*) in express js?
app.get(*) means that for any GET request not already handled yet, will come in this route.
Note: this must be at the end of all route handling (Last route).

See example below:
//Login route
app.get('/login', function(req,res){ /*Login code */ });

//Register route
app.get('/register', function(req,res){/*Register code */});

//Route which is not handled yet
app.get('*', function(req, res, next) {
  var err = new Error();
  err.status = 404;
  next(err);
}

As we know, last route is for not found route, So in this we have set the status 404.
and pass the error object to the next.
When we call next() without parameter, then it run next middleware.
When we call next() with parameter, then it run differently.


Question: What is use of next() in express JS?
next is an argument provided by Express.js as a way to "pass along" control to the next middleware (or route).


Question: What is Middleware?
Middleware are functions that have access to the request object (req), the response object (res), and the next middleware function in the application's request-response cycle.
The next middleware function is commonly denoted by a variable named next.


Question: What are benefits of Middleware?
  1. Execute any code.
  2. Make changes to the request and the response objects.
  3. End the request-response cycle.
  4. Call the next middleware function in the stack.



Question: What are different Middleware?
  1. Application-level middleware
    app.use(function (req, res, next) {
      console.log('Time:', Date.now())
      next()
    })
  2. Router-level middleware
  3. Error-handling middleware
  4. Built-in middleware
  5. Third-party middleware



Question: How to setup log4js?
Install log4js
npm install log4js

Include log4js
//include the log4js file
var log4js = require('log4js');

Configure log4js
//Configure the log4js
log4js.configure({
  appenders: { 
      display: { type: 'stdout' },
      file: { type: 'file', filename: './data/chat_logs.log' }
  
    },
  categories: { 
      display: { appenders: ['display'], level: 'debug' },      
      file: { appenders: ['file'], level: 'debug' },
      default: { appenders: ['display'], level: 'debug' }
      
  }
});

Display log4js in console
var Logger = log4js.getLogger('display'); 

Logger.trace('this is message1');
Logger.info('this is message2');
Logger.warn('this is message3');
Logger.error('this is message4');
Logger.debug('this is message5');
All the data will display in console


Question: How to store the logs in file?
var Logger = log4js.getLogger('file'); 

Logger.trace('this is message1');
Logger.info('this is message2');
Logger.warn('this is message3');
Logger.error('this is message4');
Logger.debug('this is message5');