Nodejs Interview Questions
Nodejs Interview Questions
NodeJS is one of the important framework in MEAN stack. NodeJS is used to program server side part of
applications. In this article, we present a set of NodeJS interview questions that could be helpful during
interview process. The interview questions are divided into three categories based on the difficulty.
What is Node.js ?
Node.js is :
Open Source (Source code of Node.js is available openly and can be modified and built)
Cross Platform (Works on different operating systems)
Asynchronous
Event Driven (Web Requests are considered as events)
JavaScript Runtime Environment (JavaScript is run outside the browser)
1. MongoDB
2. Express.js
3. Angular.js or Angular
4. Node.js
What is npm ?
npm stands for Node Package Manager. npm is the package manager for JavaScript.
var fs = require('fs');
var fs = require('fs');
//
// add a new
include thefunction,
module that you like extend
printMessage(), to the
var fs = require('fs');
module
fs.printMessage =
// add a new function, printMessage(), to the module
function(str){
fs.printMessage = function(str){
console.log("Message
console.log("Message from newly added function to the module");
from newly added
function to the
console.log(str);
module");
} console.log(str);
}// re-export the module for changes to take effect
module.exports
// re-export the = fs
module for changes
// you may use the to
newly added function
take effect
module.exports
fs.printMessage("Success");
= fs
What is a callback ?
Callback function is used in Node.js to deal with event handling.
When an asynchronous function is called upon a resource for some task, the control is let immediately to
continue with the subsequent statements after the function. The task on the resource would start in parallel. This
helps Node.js continue with other tasks while the function is working with the resource. Once the task with the
resource is completed, Node.js resumes with the callback function. Callback function is called with arguments :
data object, result object and (or) error object containing information regarding the task.
var fs = require('fs');
Async
async.waterfall()
async.series()
Promises
var fs = require('fs');
var fs = require('fs');
fs.readFile()
fs.writeFile()
fs.appendFile()
fs.open()
fs.rename()
fs.unlink()
The above mentioned functions are asynchronous. Synchronous variants for these are also available.
var.someFunction().anotherFunction().someAnotherFunc();
process.exit(-1);
process.exit(-1);
In the following example, we created an HTTP server. When the server gets a request for a resource, we can
send a redirect response back to the browser using response.writeHead() function. The response code
should be 301 for a redirect and the redirect url is provided as well in writeHead() function.
redirectUrlExample.js
var http =
require('http');
var fs = require('fs');
if (req.url ==
'/page-c.html') {
// redirect to
page-b.html with 301
(Moved Permanently)
HTTP code in the
response
res.writeHead(301, {
"Location": "http://" +
req.headers['host'] +
'/page-b.html' });
return res.end();
} else {
// for other URLs,
try responding with the
page
console.log(req.url)
// read
requested file
fs.readFile(req.url.subs
tring(1),
var http = require('http');
var fs = require('fs');
// create a http server
http.createServer(function (req, res) {
if (req.url == '/page-c.html') {
// redirect to page-b.html with 301 (Moved Permanently) HTTP code in the response
res.writeHead(301, { "Location": "http://" + req.headers['host'] + '/page-b.html' });
return res.end();
} else {
// for other URLs, try responding with the page
console.log(req.url)
// read requested file
fs.readFile(req.url.substring(1),
function(err, data) {
if (err) throw err;
res.writeHead(200);
res.write(data.toString('utf8'));
return res.end();
});
}
}).listen(8085);
// create a server
// include http module in the file
var http = require('http');
// create a server
http.createServer(function (req, res) {
// http header
// 200 - is the OK message
// to respond with html content, 'Content-Type' should be 'text/html'
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Node.js says hello!'); //write a response to the client
res.end(); //end the response
}).listen(9000); //the server object listens on port 9000
//
// create
includeamysql
connection
module
variable with the
var mysql
details = require('mysql');
required
var con =
// create a connection variable with the details required
mysql.createConnectio
var con = mysql.createConnection({
n({
host: "localhost", //
host: "localhost", // ip address of server running mysql
ip address of server
running mysql // user name to your mysql database
user: "arjun",
user: "arjun", //
password:
user name to"password"
your // corresponding password
mysql
}); database
password: "password"
// corresponding
// connect to the database.
password
con.connect(function(err) {
});
if (err) throw err;
// connect to the
console.log("Connected!");
database.
con.connect(function(
});
err) {
if (err) throw err;
console.log("Connected
Reference : Node.js – Connect to MySQL Database
!");
});
The data in result object depends on the type of query that is made to MySQL Server.
Node.js
⊩ Node.js Tutorial
⊩ Node.js - Modules
⊩ Node.js - forEach
Express.js
⊩ Express.js Tutorial
⊩ What is Express.js?
⊩ Install Express.js
⊩ Express.js Routes
⊩ Express.js Middleware
⊩ Express.js Router
Node.js Buffers
Node.js HTTP
Node.js MySQL
⊩ Node.js MySQL
Node.js MongoDB
⊩ Node.js MongoDB
Node.js Mongoose
Node.js URL
⊩ Node FS
Node.js JSON
Node.js Examples
⊩ Node.js Examples
Useful Resources