Thursday, April 13, 2017

Express js Interview questions and answers

Question: How to install express js in node?
use following command to install express js.

npm install express



Question: How to use express js in node?
use require to include express module.
var app = require('express')();



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

app.get('/', function (req, res) {
console.log("Got a GET request for the homepage"); //Shown in console
res.send('This is GET Method for Homepage'); //Display as response
})

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



Question: How to use handle post request in express Js?
/*Include require module*/
var app = require('express')();
var http = require('http').Server(app);

app.post('/user_list', function (req, res) {
console.log("Got a POST request for the user_list URL"); //Shown in console
res.send('This is POST Method for user_list URL');//Display as response
})

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



Question: How to use handle GET/POST request for same URL expressJs?
/*Include require module*/
var app = require('express')();
var http = require('http').Server(app);

//This is POST Request
app.post('/add_user', function (req, res) {
console.log("Got a POST request for the add_user URL"); //Shown in console
res.send('This is POST Method for add_user URL');//Display as response
})

//This is Get Request
app.get('/user_list', function (req, res) {
console.log("Got a GET request for the user_list URL"); //Shown in console
res.send('This is GET Method for user_list URL');//Display as response
})

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



Question: How to use handle GET request for all URL start with ab* ?
app.get('/ab*', function(req, res) {   
console.log("Got a GET request for /ab*");
res.send('Page Regex Match');
})
Question: How to send Server call in Node?
Install the "request" module with following command.
npm install request

Include the request module in page, and send the request to server.
request = require('request');
request( "http://exmaple.com:8081:/users/add/?n=test&p=2277585&email=test@gmail.com", function(err, res, body) {
console.log(res);
console.log(body);
});




Question: How to post data and get in express Js?
HTML Code
<form action="http://example.com:8081/register" method="GET">
Name: <input name="name" type="text" /> <br />
Email: <input name="email" type="text" />
Phone: <input name="phone" type="text" />
<input type="submit" value="Submit" />
</form>


Express JS Code
app.get('/register', function (req, res) {   
response = {
name:req.query.name,
email:req.query.email
phone:req.query.phone
};
console.log(response);
res.end(JSON.stringify(response));
})



Question: How to get cookie in Express js?
Install the "cookie-parser" module with following command.
npm install cookie-parser

Include the cookie-parser, in node.
app.get('/', function(req, res) {
console.log("Cookies: ", req.cookies)
})


Question: How Static Files works in Express js?
Static files are images/videos/css/js etc.
For this, first you need to set the folder path using "express.static" method. See Example
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/', function (req, res) {
res.send('Hello ');
});
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
})



Question: How to upload images in Express js?
HTML Code
<form action="http://example.com:8081/file_upload" enctype="multipart/form-data" method="POST">
<input name="file" size="50" type="file" />
<input type="submit" value="Submit" />
</form>

ExpressJS Code
var fs = require("fs");
var express = require('express');
var app = express();
var fs = require("fs");

app.post('/file_upload', function (req, res) {
console.log(req.files.file.name);
var destinationFile = __dirname + "/images/" + req.files.file.name;

fs.readFile( req.files.file.path, function (err, data) {
fs.writeFile(destinationFile, data, function (err) {
if( err ){
console.log( err );
}else{
response = {
message:'File uploaded successfully in '+destinationFile,
filename:req.files.file.name
};
}
//console.log( response );
res.end( JSON.stringify( response ) );
});
});
})



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

1 comment:

  1. Nice post. I learn something new and challenging on sites I stumbleupon everyday. It’s always helpful to read through articles from other writers and practice a little something from their websites.

    Please Read More: Download Ebook: Ultimate Guide To Job Interview Questions Answers:

    Best rgs

    ReplyDelete

lEARNING: SQL | WHERE Clause

SQL | WHERE Clause WHERE keyword is used for fetching filtered data in a result set. It is used to fetch data accord...