Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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:

image text in transcribed

In the meantime your Node.js server log should give you the number of milliseconds it takes to calculate this number. See screenshot below.

image text in transcribed

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

/**  * 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) { if (n return 1; else  return fibonacci(n - 2) + fibonacci(n - 1); } var server = http.createServer(function (request, response) { if (request.method == 'GET' && request.url == '/') { response.write(''); response.write(''); response.write('Rabbit Population Forcast'); response.write(''); response.write('

Rabbit Population Forecast

') response.write('
') response.write('
    '); response.write('
  • 1 month population
  • '); response.write('
  • 10 months population
  • '); response.write('
  • 30 months population
  • '); response.write('
  • 44 months population
  • '); response.write('
'); response.write(''); response.write(''); response.end(); } else if (request.method == 'GET' && request.url == '/1m') { response.write(''); response.write(''); response.write('Rabbit Population Forcast'); response.write(''); console.time('1m_timer'); response.write('

Pairs of rabbits after 1 month is: ' + fibonacci(1) + '

'); console.timeEnd('1m_timer') response.write('

Rabbit Population Forecast

') response.write('
') response.write('
    '); response.write('
  • 1 month population
  • '); response.write('
  • 10 months population
  • '); response.write('
  • 30 months population
  • '); response.write('
  • 44 months population
  • '); response.write('
'); response.write(''); response.write(''); response.end(); } else if (request.method == 'GET' && request.url == '/10m') { response.write(''); response.write(''); response.write('Rabbit Population Forcast'); response.write(''); console.time('10m_timer'); response.write('

Pairs of rabbits after 10 months is: ' + fibonacci(10) + '

'); console.timeEnd('10m_timer'); response.write('

Rabbit Population Forecast

') response.write('
') response.write('
    '); response.write('
  • 1 month population
  • '); response.write('
  • 10 months population
  • '); response.write('
  • 30 months population
  • '); response.write('
  • 44 months population
  • '); response.write('
'); response.write(''); response.write(''); response.end(); } else if (request.method == 'GET' && request.url == '/30m') { response.write(''); response.write(''); response.write('Rabbit Population Forcast'); response.write(''); console.time('30m_timer'); response.write('

Pairs of rabbits after 30 months is: ' + fibonacci(30) + '

'); console.timeEnd('30m_timer'); response.write('

Rabbit Population Forecast

') response.write('
') response.write('
    '); response.write('
  • 1 month population
  • '); response.write('
  • 10 months population
  • '); response.write('
  • 30 months population
  • '); response.write('
  • 44 months population
  • '); response.write('
'); response.write(''); response.write(''); response.end(); } else if (request.method == 'GET' && request.url == '/44m') { response.write(''); response.write(''); response.write('Rabbit Population Forcast'); response.write(''); console.time('44m_timer'); response.write('

Pairs of rabbits after 44 months is: ' + fibonacci(44) + '

'); console.timeEnd('44m_timer'); response.write('

Rabbit Population Forecast

') response.write('
') response.write('
    '); response.write('
  • 1 month population
  • '); response.write('
  • 10 months population
  • '); response.write('
  • 30 months population
  • '); response.write('
  • 44 months population
  • '); response.write('
'); response.write(''); response.write(''); response.end(); } else { response.write(''); response.write(''); response.write('Rabbit Population Forcast'); response.write(''); response.write('

Error: invalid request!

'); response.write(''); response.write(''); response.end(); } }); server.listen(3333); console.log("Server is running at http://localhost:3333"); Person 1X Rabbit Population Forc x

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

International Baccalaureate Computer Science HL And SL Option A Databases Part I Basic Concepts

Authors: H Sarah Shakibi PhD

1st Edition

1542457084, 978-1542457088

More Books

Students also viewed these Databases questions