0% found this document useful (0 votes)
87 views5 pages

Core, User Defined and Third-Party Module

This document contains 3 code examples that demonstrate using core modules, handling URL parameters, and creating a local module in Node.js. 1. The first example creates an HTTP server that displays different messages based on the URL path. It uses core HTTP and requires modules to handle requests and responses. 2. The second example finds the greatest of two numbers passed as URL parameters. It uses the URL and querystring core modules to parse the query string. 3. The third example defines arithmetic functions in a local module file, requires it in another file, and displays the results of calling each function.

Uploaded by

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

Core, User Defined and Third-Party Module

This document contains 3 code examples that demonstrate using core modules, handling URL parameters, and creating a local module in Node.js. 1. The first example creates an HTTP server that displays different messages based on the URL path. It uses core HTTP and requires modules to handle requests and responses. 2. The second example finds the greatest of two numbers passed as URL parameters. It uses the URL and querystring core modules to parse the query string. 3. The third example defines arithmetic functions in a local module file, requires it in another file, and displays the results of calling each function.

Uploaded by

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

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.

(3) If user enter “/exam/regular_exam” then display “Regular exam”


Note: You can use either switch case or else if statement

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:

3. Create a local module which contains 4 functions for multiplication, addition,


subtraction and division. Export it in another file main.js and display their result.
Code:
Modulo.js
exports.sum=function(a,b){
    return a+b;
}
exports.subtraction=function(a,b){
    return a-b;
}

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:

You might also like