Project Description: Your job is to modify this Node.js program (rabbit.js) by a
ID: 3743576 • Letter: P
Question
Project Description:
Your job is to modify this Node.js program (rabbit.js) by adding another entry for 55 months population.
When you run this project, you should see something like this in the browser:
If you click any links at the bottom, you should get something like this:
In the meantime your Node.js server log should give you the number of milliseconds it takes to calculate this number. See screenshot below.
As you can see from the timers, it takes significantly longer to calculate for 44 months. To make the matter worse, when the Node.js is calculating for 44 months’ population, the whole application gets stuck. This shows that Node.js uses a single thread to process multiple requests, so if one request causes a lot of CPU consumption in Node.js server, other requests have to wait.
Now add another entry for 55 months population, then try to click on it, your Node.js may hang on you and you may never get a response back. This is thread starvation. You have to stop the Node.js server and restart to recover.
CODE
Person 1X Rabbit Population Forc xExplanation / Answer
/*******************************************************************************************
LIST OF MODIFICATIONS
1. Added the code for responding to the /55m, request url. Check it out in the else if ladder.
2. Added links to /55m across all the request urls
3. Corrected the implementation of fibonacci (you were actually calculating the factorial). If you didn't want to do that, replace the fibonacci function with the one you were using.
4. Added a faster implementation of fibonacci. It won't make the thread wait for long because it is fast. It is commented below the first fibonacci function. Uncomment it to use it. But comment the first fibonacci function first.
5. This assignment shows that Node.js is single threaded and CPU intensve tasks are blocking in nature,
*********************************************************************************************/
/**
/** * app.js
* unit 04 lab
* This program is a server-side JavaScript program that
* simulates rabbit reproduction using Fibonacci sequence.
* This project is adapted from text book example Listing 2-24 starveit.js on page 29.
*/
var http = require('http');
// function to calculate rabbit reproduction using fibonacci sequence
function fibonacci(n) {
// Changed this code from factorial calculation to fibonacci calculation
if (n == 1 || n == 2)
return 1;
else
return fibonacci(n - 2) + fibonacci(n - 1);
}
// function fibonacci(n) {
// var a = 0, b = 1, f = 1;
// for(var i = 2; i <= n; i++) {
// f = a + b;
// a = b;
// b = f;
// }
// return f;
// }
var server = http.createServer(function (request, response) {
if (request.method == 'GET' && request.url == '/') {
response.write('<!DOCTYPE html>');
response.write('<html>');
response.write('<head><title>Rabbit Population Forcast</title></head>');
response.write('<body>');
response.write('<h1>Rabbit Population Forecast</h1>')
response.write('<div><iframe width="560" height="315" src="https://www.youtube.com/embed/koFsRrJgioA" frameborder="0" allowfullscreen></iframe></div>')
response.write('<ul>');
response.write('<li><a href="/1m">1 month population</a></li>');
response.write('<li><a href="/10m">10 months population</a></li>');
response.write('<li><a href="/30m">30 months population</a></li>');
response.write('<li><a href="/44m">44 months population</a></li>');
response.write('<li><a href="/44m">44 months population</a></li>');
// Added a URL to /55m
response.write('<li><a href="/55m">55 months population</a></li>');
response.write('</ul>');
response.write('</body>');
response.write('</html>');
response.end();
} else if (request.method == 'GET' && request.url == '/1m') {
response.write('<!DOCTYPE html>');
response.write('<html>');
response.write('<head><title>Rabbit Population Forcast</title></head>');
response.write('<body>');
console.time('1m_timer');
response.write('<p>Pairs of rabbits after 1 month is: ' + fibonacci(1) + '</p>');
console.timeEnd('1m_timer')
response.write('<h1>Rabbit Population Forecast</h1>')
response.write('<div><iframe width="560" height="315" src="https://www.youtube.com/embed/koFsRrJgioA" frameborder="0" allowfullscreen></iframe></div>')
response.write('<ul>');
response.write('<li><a href="/1m">1 month population</a></li>');
response.write('<li><a href="/10m">10 months population</a></li>');
response.write('<li><a href="/30m">30 months population</a></li>');
response.write('<li><a href="/44m">44 months population</a></li>');
// Added a URL to /55m
response.write('<li><a href="/55m">55 months population</a></li>');
response.write('</ul>');
response.write('</body>');
response.write('</html>');
response.end();
} else if (request.method == 'GET' && request.url == '/10m') {
response.write('<!DOCTYPE html>');
response.write('<html>');
response.write('<head><title>Rabbit Population Forcast</title></head>');
response.write('<body>');
console.time('10m_timer');
response.write('<p>Pairs of rabbits after 10 months is: ' + fibonacci(10) + '</p>');
console.timeEnd('10m_timer');
response.write('<h1>Rabbit Population Forecast</h1>')
response.write('<div><iframe width="560" height="315" src="https://www.youtube.com/embed/koFsRrJgioA" frameborder="0" allowfullscreen></iframe></div>')
response.write('<ul>');
response.write('<li><a href="/1m">1 month population</a></li>');
response.write('<li><a href="/10m">10 months population</a></li>');
response.write('<li><a href="/30m">30 months population</a></li>');
response.write('<li><a href="/44m">44 months population</a></li>');
// Added a URL to /55m
response.write('<li><a href="/55m">55 months population</a></li>');
response.write('</ul>');
response.write('</body>');
response.write('</html>');
response.end();
} else if (request.method == 'GET' && request.url == '/30m') {
response.write('<!DOCTYPE html>');
response.write('<html>');
response.write('<head><title>Rabbit Population Forcast</title></head>');
response.write('<body>');
console.time('30m_timer');
response.write('<p>Pairs of rabbits after 30 months is: ' + fibonacci(30) + '</p>');
console.timeEnd('30m_timer');
response.write('<h1>Rabbit Population Forecast</h1>')
response.write('<div><iframe width="560" height="315" src="https://www.youtube.com/embed/koFsRrJgioA" frameborder="0" allowfullscreen></iframe></div>')
response.write('<ul>');
response.write('<li><a href="/1m">1 month population</a></li>');
response.write('<li><a href="/10m">10 months population</a></li>');
response.write('<li><a href="/30m">30 months population</a></li>');
response.write('<li><a href="/44m">44 months population</a></li>');
// Added a URL to /55m
response.write('<li><a href="/55m">55 months population</a></li>');
response.write('</ul>');
response.write('</body>');
response.write('</html>');
response.end();
} else if (request.method == 'GET' && request.url == '/44m') {
response.write('<!DOCTYPE html>');
response.write('<html>');
response.write('<head><title>Rabbit Population Forcast</title></head>');
response.write('<body>');
console.time('44m_timer');
response.write('<p>Pairs of rabbits after 44 months is: ' + fibonacci(44) + '</p>');
console.timeEnd('44m_timer');
response.write('<h1>Rabbit Population Forecast</h1>')
response.write('<div><iframe width="560" height="315" src="https://www.youtube.com/embed/koFsRrJgioA" frameborder="0" allowfullscreen></iframe></div>')
response.write('<ul>');
response.write('<li><a href="/1m">1 month population</a></li>');
response.write('<li><a href="/10m">10 months population</a></li>');
response.write('<li><a href="/30m">30 months population</a></li>');
response.write('<li><a href="/44m">44 months population</a></li>');
// Added a URL to /55m
response.write('<li><a href="/55m">55 months population</a></li>');
response.write('</ul>');
response.write('</body>');
response.write('</html>');
response.end();
} else if (request.method == 'GET' && request.url == '/55m') {
// This is the piece of code that was added to handle requests to /55m (rabbits at 55 months)
// It is the same as all the other ones
response.write('<!DOCTYPE html>');
response.write('<html>');
response.write('<head><title>Rabbit Population Forcast</title></head>');
response.write('<body>');
console.time('55m_timer');
// Calls the fibonacci fiunction with the value 55
response.write('<p>Pairs of rabbits after 55 months is: ' + fibonacci(55) + '</p>');
console.timeEnd('55m_timer');
response.write('<h1>Rabbit Population Forecast</h1>')
response.write('<div><iframe width="560" height="315" src="https://www.youtube.com/embed/koFsRrJgioA" frameborder="0" allowfullscreen></iframe></div>')
response.write('<ul>');
response.write('<li><a href="/1m">1 month population</a></li>');
response.write('<li><a href="/10m">10 months population</a></li>');
response.write('<li><a href="/30m">30 months population</a></li>');
response.write('<li><a href="/44m">44 months population</a></li>');
// Added a URL to /55m
response.write('<li><a href="/55m">55 months population</a></li>');
response.write('</ul>');
response.write('</body>');
response.write('</html>');
response.end();
} else {
response.write('<!DOCTYPE html>');
response.write('<html>');
response.write('<head><title>Rabbit Population Forcast</title></head>');
response.write('<body>');
response.write('<h1>Error: invalid request!</h1>');
response.write('</body>');
response.write('</html>');
response.end();
}
});
server.listen(3333);
console.log("Server is running at http://localhost:3333");
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.