Monday 14 December 2015

Express Js Interview Questions and Answers

Express Js Interview Questions and Answers


Question: What is Express Js?
Express JS is a framework which helps to develop web and mobile applications. Its works on nodejs plateform. Its sub part of node.js.



What type of web application can built using Express JS?
you can build single-page, multi-page, and hybrid web applications.



Question: What are core features of Express framework?
  1. Allows to set up middlewares to respond to HTTP Requests
  2. Defines a routing table which can works as per HTTP Method and URL.
  3. Dynamically render HTML Pages


Question: Why I should use Express JS?
Express 3.x is a light-weight web application framework to help organize your web application into an MVC architecture on the server side.



Question: How to install expressjs?
http://expressjs.com/en/starter/installing.html


Question: How to get variables in Express.js in GET Method?
var express = require('express');
var app = express();

app.get('/', function(req, res){
    /* req have all the values **/  
  res.send('id: ' + req.query.id);  
});
app.listen(3000);



Question: How to get POST a query in Express.js?
var bodyParser = require('body-parser')
app.use( bodyParser.json() );       // to support JSON-encoded 
app.use(bodyParser.urlencoded({     // to support URL-encoded 
  extended: true
})); 



Question: How to output pretty html in Express.js?
app.set('view options', { pretty: true }); 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 == 80 || port == 443 ? '' : ':'+port ) + req.path;



Question: How to remove debugging from an Express app?
var io = require('socket.io').listen(app, { log: false });
io.set('log level', 1); 



Question: How to to 404 errors?
app.get('*', function(req, res){
  res.send('what???', 404);
});



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



Question: What is the parameter “next” used for in Express?
app.get('/userdetails/:id?', function(req, res, next){
 });

req and res which represent the request and response objects
nextIt passes control to the next matching route.