Core, User Defined and Third-Party Module
Core, User Defined and Third-Party Module
1. Using HTTP and other required core modules, create HTTP server and display the
following message to console and on the webpage for different URL entered by the
user.
(1) If user enter “/ ” then display “This is test”
(2) If the user enters ”/exam” then display the following table.
Code:
var http=require('http');
http.createServer(function(req,res){
var a=req.url
res.writeHead(200,{'content-Type':'text/html'})
if(a=='/')
{
res.write('<h1>This is Test</h1>');
res.end()
}
else if(a=='/exam')
{
res.write('<a href="https://www.ganpatuniversity.ac.in/exams/exam-schedule">Visit He
re</a>');
res.write('<style>table, th, td {border: 2px solid black;border-collapse: collapse;}th, td {
background-color: white;}</style><table><tr><th>Semester</th><th>Subject</th></tr><tr>
1
<td>5th</td><td>Software Packages</td></tr><tr><td>4th</td><td>NOSQL</td></tr></ta
ble>')
res.end()
}
else if(a=='/exam/regular_exam')
{
res.write('Regular exam');
res.end()
}
else
{
res.write('Error\nUse only /, /exam and /exam/regular_exam');
res.end()
}
res.end();
}).listen(8080);
console.log("running on http://localhost:8080/") // http://localhost:8080/
Output:
2
2. In a given query string of URL, give two parameters num1 and num2 and assign
integers values to them and then find the greatest number among those two
numbers.[URL: localhost:8080/?num1=49&num2=34]
Code:
const http = require('http')
const url = require('url')
http.createServer((req,res)=>{var queryData=url.parse(req.url,true).query
if(queryData.num1>queryData.num2){
res.write("number 1 is greater:"+ queryData.num1)
}
else if(queryData.num1 == queryData.num2){
3
res.write("both are equal")
}
else{
res.write("number 2 is greater:"+ queryData.num2)
}
res.end();
}).listen(8080);
Output:
4
exports.multiplication=function(a,b){
return a*b;
}
exports.division=function(a,b){
return a/b;
}
P3_q3.js
var m=require('./module');
console.log('Sum of 5 and 10 is '+m.sum(5,10));
console.log('Subtraction of 5 and 10 is '+m.subtraction(5,10));
console.log('Multiplication of 5 and 10 is '+m.multiplication(5,10));
console.log('Division of 5 and 10 is '+m.division(5,10));
Output: