0% found this document useful (0 votes)
48 views

Unit 4:node JS& Mongodb Name: V Anirruth SRN: PES2UG20CS478 Section: H Date: 23/11/2021 Unit 4 Assignment Exercise

1. The student created a MongoDB database with a collection of employee documents containing fields like employee ID, name, date of birth, type, and department. 2. A Node.js server listens on port 8081 and retrieves employee data from MongoDB based on parameters passed in the URL, displaying employee ID and name. 3. The student created a custom module to generate random numbers, imported it into their application, and displayed the random numbers.

Uploaded by

lucifer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Unit 4:node JS& Mongodb Name: V Anirruth SRN: PES2UG20CS478 Section: H Date: 23/11/2021 Unit 4 Assignment Exercise

1. The student created a MongoDB database with a collection of employee documents containing fields like employee ID, name, date of birth, type, and department. 2. A Node.js server listens on port 8081 and retrieves employee data from MongoDB based on parameters passed in the URL, displaying employee ID and name. 3. The student created a custom module to generate random numbers, imported it into their application, and displayed the random numbers.

Uploaded by

lucifer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

AUG-

DEC
Unit 4:NODE JS& MONGODB 2021

Name: V ANIRRUTH SRN: PES2UG20CS478 Section: H


Date: 23/11/2021 Unit 4 Assignment
Exercise
PROBLEM STATEMENT(Even SRN’s)

1. Create a MongoDB database that has a collection of employees having


different documents (such as emp_id, emp_name, emp_dob,emp_type,
emp_dept etc)for each employee. Create a server listening to 8081 that
checks the query string submitted to it and retrieves the Employee ID and
full names of all employees sorted based on the parameter (either dept or
eid only) passed to it. For instance, the
URLhttp://localhost:8081/?dept=Salesshould display Employee ID and
names of employees working in sales. The URLhttp://localhost:8081/?eid=2
should display Employee ID and names of employees with Employee ID 2.
2. Create a custom module to generate random numbers and import it in your
application and display the random numbers.
OBJECTIVE

The objective of this exercise is to test the student on NodeJS and MongoDB.It
evaluates the student’s knowledge ofhttp server creation using Node, Reading
from MongoDB and NodeJS MongoDB driver.
PREREQUISITE

In order to complete this exercise, the student needs to understand the


fundamentals of HTML,CSS, and JavaScript
AUG-
DEC
Unit 4:NODE JS& MONGODB 2021

DATABASE CREATION

> use employee_db switched to db empdb > db.createCollection("Employee")


{ "ok" : 1 } >
db.student.insert({"emp_id":"101","emp_name":"john","emp_dob":"12/12/1
989", "emp_type":"night-shift","emp_dept":"sales"})
WriteResult({ "nInserted" : 1 })
>
db.student.insert({"emp_id":"102","emp_name":"jane","emp_dob":"12/2/1
999" ,"emp_type":"day-shift","emp_dept":"sales"})
WriteResult({ "nInserted" : 1 })
>
db.student.insert({"emp_id":"103","emp_name":"ram","emp_dob":"1/8/19
87","emp_type":"day-shift","emp_dept":"HR"})
WriteResult({ "nInserted" : 1 })
>
db.student.insert({"emp_id":"104","emp_name":"mohan","emp_dob":"
11/8/2000 ","emp_type":"day-shift","emp_dept":"PR"})
WriteResult({ "nInserted" : 1 })
>
db.student.insert({"emp_id":"105","emp_name":"ryan","emp_dob":"
1/10/1990 ","emp_type":"night-shift","emp_dept":"marketing"})
WriteResult({ "nInserted" : 1 })
>
db.student.insert({"emp_id":"2","emp_name":"rohan","emp_dob":"
25/8/1987 ","emp_type":"night-shift","emp_dept":"marketing"})
WriteResult({ "nInserted" : 1 })

SAMPLE SCREENSHOT OF OUTPUT (Just for your reference)


AUG-
DEC
Unit 4:NODE JS& MONGODB 2021

PROGRAM

var http = require('http');


var url = require('url');
var fs = require('fs');
var qs = require('querystring');
var MongoClient = require('mongodb').MongoClient;
var dburl = "mongodb://localhost:27017/";
http.createServer(function (request, response) {
if (request.method == "GET") {
var myurl = url.parse(request.url)
var pathname = myurl.pathname;
MongoClient.connect(dburl, function (err, db) {
var dbo = db.db('employee_db');
var query = myurl.query;
var qobj = qs.parse(query);
dbo.collection('Employee').find(qobj).toArray(function (err, result)
{
if (err) throw err;
AUG-
DEC
Unit 4:NODE JS& MONGODB 2021

response.writeHead(200, { 'Content-Type': 'text/html' });


for (var i = 0; i < result.length; i++) {
response.write('<h1> Employee name : ' +
JSON.stringify(result[i].emp_name) + ' Employee_ID : ' +
JSON.stringify(result[i].emp_id) + '</h1>');
}
response.end();
db.close();
})

})
}
}).listen(8081);

console.log("Server started");

Second url

var http = require('http');


var url = require('url');
var fs = require('fs');
var qs = require('querystring');
var MongoClient = require('mongodb').MongoClient;
var dburl = "mongodb://localhost:27017/";
http.createServer(function (request, response) {
if (request.method == "GET") {
var myurl = url.parse(request.url)
var pathname = myurl.pathname;
MongoClient.connect(dburl, function (err, db) {
var dbo = db.db('employee_db');
var query = myurl.query;

var q2obj = qs.parse(query);


dbo.collection('Employee').find(q2obj).toArray(function (err, result)
{
if (err) throw err;
response.writeHead(200, { 'Content-Type': 'text/html' });
response.write("<h1> Second part </h1>");
for (var i = 0; i < result.length; i++) {
response.write('<h1> Employee name : ' +
JSON.stringify(result[i].emp_name) + ' Employee_ID : ' +
AUG-
DEC
Unit 4:NODE JS& MONGODB 2021

JSON.stringify(result[i].emp_id) + '</h1>');
}
response.end();
db.close();
})

})
}
}).listen(8081);

console.log("Server started");

2)

Main.js
const fs = require("fs");
const result = require("./module.js");
console.log(result.re);
console.log(result.re);
console.log(result.re);
console.log(result.re);

module.js
const result = Math.random() * (20 - 17) + 12
exports.re = result;
AUG-
DEC
Unit 4:NODE JS& MONGODB 2021

SCREENSHOT OF YOUR OUTPUT

2)
AUG-
DEC
Unit 4:NODE JS& MONGODB 2021

You might also like