Search for question
Question

Writing a Server Application Tasks

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

win/no-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:

o 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)

o 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: