In this lab you will build a small server-side math quiz for Brock's server. A Python CGI script will keep a bank of 10 math questions on the server, choose 3 of them at random, send only the question text to the browser, and grade the submitted answers after the user fills in the blanks.
The main ideas here are using GETPOSTrandom.sample(...)
By the end of the lab, opening the uploaded page on Brock should show three random math questions, let the user type answers, and report which answers are correct without sending the answer key to the browser first.
You should read the Week 09 lecture notes first, especially the CGI sections. This lab assumes you already know why server-side scripting is useful and how JSON moves between the browser and the server.
You also need access to your Brock or Sandcastle account so you can upload the final files into
public_html/lab09
The project below uses plain HTML, CSS, and TypeScript. There is no Angular backend, and there is no Node.js backend running on Brock. The browser runs the generated JavaScript, while Brock runs the generated Python CGI file.
If you want the exact project folder created for you first, download the Lab 09 material ZIP. It contains the same files used in the walkthrough below.
The project structure should look like this.
The HTML page and stylesheet live at the project root. The browser TypeScript source lives in
frontend/quiz.tsbackend/quiz-api.tsdist/frontend/quiz.jsdist/backend/quiz-api.jsdist/backend/quiz-api.cgi
Start with this index.html
Add the CSS below to styles.css
Create frontend/quiz.tsGETPOST
Notice the endpoint path: dist/backend/quiz-api.cgipublic_html/lab09
Create backend/quiz-api.tsdist/backend/quiz-api.cgiGETPOST
The function random.sample(QUESTIONS, PUBLIC_QUESTION_COUNT)
Use the tsconfig.jsondist
Just like Labs 04 and 05, open a terminal inside the lab09-brock-cgi-math-quiz
That rebuilds dist/frontend/quiz.jsdist/backend/quiz-api.js
If you started from the material ZIP, the project already includes dist/backend/quiz-api.cgibackend/quiz-api.tstsc -wdist/backend/quiz-api.jsnode dist/backend/quiz-api.jsdist/backend/quiz-api.cgi
The Brock server does not use quiz-api.jsdist/backend/quiz-api.cgi.cgidist/backenddist/backend/quiz-api.js
In this lab, keep the whole uploaded site together inside public_html/lab09distfrontendbackenddist/backend/quiz-api.cgi
Your Brock or Sandcastle account should end up with this file structure. The number in parentheses is the permission you should set for that folder or file.
The browser-side JavaScript file dist/frontend/quiz.jsdist/backend/quiz-api.cgidist/backend/quiz-api.js
That means the useful backend file on Brock is dist/backend/quiz-api.cgiquiz-api.js
Important: remove dist/backend/quiz-api.jsdist/backendquiz-api.cgi.jsquiz-api.cgi755644500 Internal Server Error
If you created or edited the files on Windows, open dist/backend/quiz-api.cgiCRLFLF
After the upload, visit this URL.