Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this task you are asked to demonstrate how a web server can be coded to serve dynamic pages. Specifically, the server application is to

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

In this task you are asked to demonstrate how a web server can be coded to serve dynamic pages. Specifically, the server application is to provide a page that displays if the visitor is a Winner of a Competition or not. The selection of a wino-win can be based on a simple random number selection (i.e., 1 in 3 chance). This page is to be served through a defined web address (or route) such as http://localhost: 3000/competition. Follow the steps below to complete this task: 1. Create your own local directory (or you can use the same local directory created in Task 8.1). 2. Use express module to create a Node.js application (i.e., using the index.js from your Task 8.1 or creating a new expressServer.js) that will set a local web server. The server listens to the port 3000. 3. Modify your index.js (or expressServer.js) file to: remove the hardcoded response for the Get on the / route (as done in Task 8.1P) and change it to serve pages form the public_html folder, as done in previous weeks (sample of this code is shown in the snippet below) add a route handler for a Get request on /competition. This would enable access to http://localhost:3000/competition using a web browser. The handler for this route would then choose a random number (say out of 3) and if it equals 3 then display a 'win', otherwise display a 'no win' message. These messages, along with a statistics (tally) of wins & losses, should be provided a new webpage. 4. Run the command node . (to run the index.js) or node expressServer.js in a Command Prompt (Windows) or Terminal (Mac OS) within your local directory to make the server work. 5. Use the provided index.html file to access your newly created /competition route handler (or access it directly through http://localhost: 3000/competition), the Home page should appear like this: > User Page e O localhost 3000 08 Incognito * Bookmarks News Deakin Cartoons Deakin CloudDeakin 30 Other bookmarks Reading list dkin Pizza Competition The dkin Pizza company are running a competition this month. All you need to do is click the link (or button) below to see if you have won! Enter Now To enter the competition, you can either: Use this link: Click to Win . Or click this button: Click to Win Both these options issue a GET request to the route /competition, alternatively you could type http://localhost:3000/competition into the browser address bar to access the page. Task8.2.1 Node.js application serving the index.html competition Home Page While, when the link or button is pressed, the dynamically created page for a WIN, also showing the win/loss statistics, could look like: Competition Page localhost 3000/competition Incognito * Bookmarks News Deakin Cartoons Deakin @ CloudDeakin Other bookmarks Reading list CONGRAULATIONS You are the lucky winner of our compeition!! This page has been visited 21 times so far, with a total of 12 wins and 9 losses. Return Task8.2.2 Node.js application serving the dynamically created WIN page Similarly, the page showing a LOSS could look like: Competition Page + o localhost:3000/competition Incognito : * Bookmarks News Deakin Cartoons Deakin CloudDeakin Other bookmarks Reading list Sorry, no win Thanks for entering our compeition... you are not a winner this time but maybe on your next visit! This page has been visited 44 times so far, with a total of 20 wins and 24 losses. Return Task8.2.3 Node.js application serving the dynamically created LOSS page 6. Go back to the Command Prompt or Terminal, and shutdown the server. Hints The structure of the file index.js (or expressServer.js) with the new code (as highlighted below) could be: // Require the express web application framework (https://expressis.com) var express = require('express'); // Create a new web application by calling the express function var app = express(); // Get port from environment and store in Express. var port = normalizePort(process.env.PORT || '3000'); app.set('port', port); // Normalize a port into a number, string, or false. function normalizeport (val) { var port = parseInt(val, 10); if (isNaN(port)) { // named pipe return val; } if (port >= 0) { Il port number return port; } return false; } // Tell our application to serve all the files under the public_html' directory app.use(express.static('public_html')); // >>>> // This is a 'route' method to handle GET request for competition app.get('/competition', function(request, response) { // Add the JS code here to send a message back to the client with the // content of the page to be displayed. }); // >>>> // Tell our application to listen to requests at port 3000 on the localhost app.listen(port, function () { // when the application starts, print to the console that our app is // running at http://localhost:3000 (where the port number is 3000 by // default). Print another message indicating how to shut the server down. console.log( web server running at: http://localhost:${port}'); console.log("Type Ctrl+C to shut down the web server"); })

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

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions

Question

Describe MRP processing.

Answered: 1 week ago