What happens in detail in client server situations
Your computer | < Network > | Another computer |
---|---|---|
Browser | Web Server | |
test.html | Folder “www” with files |
You type a URL in the address bar of the browser, or click on a hyperlink in the HTML you are reading. The URL is http://server.com/test2.html
Your computer | < Network > | Another computer |
---|---|---|
Browser | –> | Web Server |
test.html | Folder “www” with files |
The server receives the URL, tests if it corresponds to a file, reads the file, send the data
Your computer | < Network > | Another computer |
---|---|---|
Browser | <– | Web Server |
test.html | Folder “www” with files |
The browser receives the new data and shows it
Your computer | < Network > | Another computer |
---|---|---|
Browser | Web Server | |
test2.html | Folder “www” with files |
The simplest web server receives a request and send a response. So it is a function like:
function webserver( request, response ) {
...
}
To be able to write the code, we need:
There is extra stuff you do not need to know in detail (TCP, HTTP text exchanged, encryption, compression, etc)
Logic of a web server serving only files:
function webserver( request, response ) {
const filepath = request.url.substring(1); // remove initial /
if (fs.existsSync(filepath)) {
response.setHeader("Content-Type", mime.getType(filepath));
response.end(fs.readFileSync(filepath));
} else {
response.statusCode = 404;
response.end("the file " + filepath + " does not exist on the server");
}
}
Logic of a web server serving only generated data:
Logic of a web server serving both:
So simple web server code is a list of ifs with conditions on the URL.
function webserver( request, response ) {
if (request.url === '/addmodule') {
... code for add module ...
} else if (request.url === '/changegrade') {
... code to change grade ...
} else ...
}
Your computer | < Network > | Another computer |
---|---|---|
Browser | Web Server | |
test.html | Folder “www” with file “test.txt” | |
Ajax code |
You press on a button that triggers the Ajax code
Your computer | < Network > | Another computer |
---|---|---|
Browser | Web Server | |
test.html | Folder “www” with file “test.txt” | |
var xhr = new XMLHttpRequest(); |
||
xhr.open("GET", "test.txt"); |
||
xhr.onload = function() {...} |
||
xhr.send(); |
–> |
Server gets request for “test.txt”, sends file content
Your computer | < Network > | Another computer |
---|---|---|
Browser | Web Server | |
test.html | <– | Folder “www” with file “test.txt” |
Ajax code |
The code in the xhr.onload
function is called with this.responseText
equal to the content of the test.txt file.
Your computer | < Network > | Another computer |
---|---|---|
Browser | Web Server | |
test.html | Folder “www” with file “test.txt” | |
alert(this.responseText); |
There are five delays:
Because of the delays, this is called asynchronous programming. Synchronous programming is when one program executes from beginning to end. Asynchronous programming has many bits of code which get activated when they called or when their data is ready.
Warning: JS looks synchronous, but is asynchronous, e.g. the onload function is between open and send, but is executed later.
Choice: the code can be placed in the onload function on the client, or on the web server.
When programming a web application server, there are many steps.
It is important to distinguish 1. and 3. even though these are two requests to the server. The first request is for an interface. The second request carries the action.
The important part of the first request is to create the interface capable of sending actions. This means HTML and CSS for the semantics and visual part of the interface, plus client-side javascript for the potential to send actions.
The important part of the second request is the action itself. In a travel reservation system, the action is to create the reservation and buy the ticket. There is also an interface part, the reporting of success or failure to the user, but it is only a consequence.
I assume that the server is programmed in JavaScript with Express.
An HTTP server listens to possibly many HTTP requests. To process a request, the server prepares a request object, with all the received information, and a response object that can be used to create the response. For each URL there will be one main function computing a response.
The application object is the one which implements all the configuration, server creation, server starting actions.
The server object(s) is(are) the one(s) which implements HTTP, so you tell them the options you want: security, encryption, ports, subprotocols like WebSockets…
The middleware needs a definition. There are actions to do on one/some/all requests, before or after the main processing.
Among such actions, some will do something and let the process continue and some will check something and either let the process continue or interrupt it.
A middleware is a function which receives a request and a response object as well as a next
function. A middleware does its processing with request and response, then calls next()
to allow the other middleware to be executed, then the main processing to happen, then possibly does more of its processing after.
Express has a list of middlewares to call, the list you gave it upon configuration.
A logging middleware gets the URL from the request, writes it in a log and passes the buck.
A hacking detection middleware checks the origin IP, passes the buck if the IP is OK, and stops everything if the IP is not OK.
A performance checking middleware would check the time, pass the buck, and when all the processing is finished, check the time again and log the execution time somewhere.
A route is a pairing between a URL and the processing function for that URL. Sometimes the URL is constant, sometimes the URL has parameters.
From the previous slides, what Express does upon receiving a request is:
If the matching route creates an interface, then the job is to prepare the required objects then render the pug template.
If the matching route is one of action, then the job is to implement the action and possibly report the results.
Pug is a template language to easily create HTML pages. It is a mixture of simplified HTML and JavaScript code. Pug is like python: indentation determine the inclusion in the above tag. Pug templates are simpler if you adopt the following structure:
render
on your pug
view, passing all the objects as inputSeparating the object computation makes for cleaner code and simpler templates.
What you can do in pug:
So you have many possible places where to put your JS code:
Step 5. is typically Ajax code. Step 4. is optional. Steps 2. and 6. are similar but apply to different data.
It is difficult to pass information from the server/express context to the client-side javascript context. This is information you have access to in step 3. and you need it in step 5. One way to do it is:
obj
to transmitobj
to puglet info = JSON.parse("#{JSON.stringify(obj)}");
Then you will be able to use the variable info
in the client-side JavaScript.