Due: Wednesday, November 15, 2017, 2:10 pm (start of the class)
Since this is not an exercise in web page development, the HTML for the solution is provided and should not be modified. A static (the AJAX request in the page results in a fixed response regardless of its parameters) example of the page can be found here. The page served in the trace can be downloaded from a4/formnjs.html. Using
wget is the easiest way to get the file to your account on agate.
node.js server that: serves the provided page
formnjs.html, responds to the AJAX GET
/prefcalc request from the page by returning JSON-encoded results of the prefix calculation, and returns HTTP 404 Page Not Found status code for any other request.
A sample run of your code may look as follows: my server was started on my machine on port 8000 so in a browser window I typed localhost:8000/formnjs.html (if your server runs on a remote host such as agate, you obviously need to enter a hostname or IP address of that machine). My server served the page and it was displayed in the browser window.
After clicking on the Submit button, the page contacts the server again with a GET request for /prefcalc with URL-encoded values from the form. The server returns the result in JSON format. The received values are displayed in the page.
To see what is going on under the hood, take a look at the network trace of this session: https://www.cloudshark.org/captures/30efb055cef5. In CloudShark, select Analysis Tools and then Follow Stream, to see the data exchanged during the session. Pay attention to the details of the two requests being sent, especially the URL-encoded form data, and the server responses (
Content-Type: application/json in the header and the JSON-formatted data being sent back). Your server must be able to handle such requests and must send responses in the way.
As with any servers, it is very likely that your implementation contains significant security holes that can be easily exploited. For that reason, do not leave your code running longer than it is necessary for testing. This is also the reason why the example page uses no active components on the server side.
© 2017 Radim Bartos.