WebFund 2015W Lecture 7: Difference between revisions
Created page with "==Resources== ==Video== The video from the lecture given on January 26, 2015 is available now [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w..." |
No edit summary |
||
Line 1: | Line 1: | ||
==Resources== | ==Resources== | ||
<ul> | |||
<li>[[WebFund 2015W: Tutorial 3|Tutorial 3]]</li> | |||
<li>[[WebFund 2015W: Assignment 3|Assignment 3]]</li> | |||
</ul> | |||
==Video== | ==Video== | ||
Line 6: | Line 10: | ||
==Notes== | ==Notes== | ||
<h3>Tutorial 3 / Assignment 3</h3> | |||
<ul> | |||
<li>Assignment 3 is about tinywebserver.js</li> | |||
<li>170 lines of code (40 lines are comments)</li> | |||
<li>tinywebserver.js is a static web server in Node</li> | |||
<li>The understanding of this code leads to a better understanding of: | |||
<ul> | |||
<li>JavaScript</li> | |||
<li>Callbacks</li> | |||
<li>What is a web server?</li> | |||
</ul> | |||
</li> | |||
<li>The tutorial consists of exercises involving changing things around in the code</li> | |||
<li>The assignment consists of more in-depth questions such as: | |||
<ul> | |||
<li>Breaking lines of code and see what happens</li> | |||
<li>Explaining what various functions do</li> | |||
</ul> | |||
</li> | |||
<li>Download some web sites and try to serve them from tinywebserver.js</li> | |||
</ul> | |||
<h3>Tinywebserver</h3> | |||
<ul> | |||
<li>Running tinywebserver.js for the first time and immediately trying to access a web page yields a 404 error because there is no index.html file in the directory of tinywebserver.js</li> | |||
<li>tinywebserver.js serves the files that are in the directory when the server is executed</li> | |||
<li>You can put content onto the web server by putting files in its directory</li> | |||
<li>Any page that you add to the web server in this way is a static resource</li> | |||
<li>If you download a web page and try to add it to your server, you should expect it to break</li> | |||
<li>This can happen when downloading modern web pages because the web browser cannot get the entire web page (it gets the html document and any other files that are associated with it but may miss some resources)</li> | |||
<li>You can go through the lines of code that break by seeing what requests have been successful and what requests have received 404 errors</li> | |||
<li>tinywebserver.js depends on the Node modules path, http, and fs | |||
<ul> | |||
<li>path is used to parse request and resource paths | |||
<li>fs is for basic file system operations</li> | |||
<li>http gives you the barest bones of a web</li> | |||
</ul> | |||
</li> | |||
</ul> | |||
<h4><code>var server = http.createServer(request_handler)</code></h4> | |||
<ul> | |||
<li>Creates a web server</li> | |||
<li>Given a function called request_handler</li> | |||
<li>request_handler handles every incoming HTTP request</li> | |||
<li><code>server.listen()</code> tells the server to begin listening for incoming requests for a the supplied port and host</li> | |||
<li><code>server.listen()</code> is also given a callback function which prints the host and port that the server is listening at</li> | |||
</ul> | |||
<h4><code>var request_handler = function(request, response)</code></h4> | |||
<ul> | |||
<li>This function is used to deal with all incoming requests</li> | |||
<li>It takes a request and a response object as parameters</li> | |||
<li>We can find out what the request and response objects are through the debugger</li> | |||
<li>We can access specific fields of the request object</li> | |||
<li>If you want to find out what a specific line does; type it in when you run node in the terminal</li> | |||
<li>requestpath is the incoming url concatenated with the docroot after it has been cleaned up</li> | |||
<li>We can see that there are two more functions defined within this function</li> | |||
<li>These additional functions are callback functions which are supplied to other function calls</li> | |||
<li><code>fs.exists()</code> is a method which checks if the file specified in the first parameter exists and then calls the function in its second parameter</li> | |||
<li>This second parameter is a callback function with a parameter file_exists</li> | |||
<li>A callback function is used because we don’t how long it will take for <code>fs.exists()</code> to run and we don't want to wait for it so we let it work asynchronously and then call the callback function when it is done</li> | |||
<li>If the requested path is found and it is a directory then <code>return_index()</code> is called</li> | |||
<li>If the requested path is found and it is a file then <code>serve_file()</code> is called</li> | |||
</ul> | |||
<h4><code>var return_index = function(request, response, requestpath)</code></h4> | |||
<ul> | |||
<li><code>return_index()</code> sends a response with an index page</li> | |||
<li>If neccessary, it adds a ‘/’ to the requestpath and then adds the <code>options.index</code> value, which in this case is index.html</li> | |||
<li>Using fs, it checks to see if the requestpath exists</li> | |||
<li>Once again, a callback function is seen in the call to <code>fs.exists()</code></li> | |||
<li>Inside the callback function, if the file was found to exist, then <code>serve_file()</code> is called</li> | |||
<li>If the file does not exist then <code>respond()</code> is called to provide a response with an error</li> | |||
</ul> | |||
<h4><code>var serve_file = function(request, response, requestpath)</code></h4> | |||
<ul> | |||
<li>serve_file() sends a response with the specified file</li> | |||
<li>It reads the file using <code>fs.readFile</code> and then serves the response inside the callback function which is called after reading the file</li> | |||
<li>If there is an error, it sends a response with a 500 error</li> | |||
<li>If there is no error, it send a response with the contents of the file and a 200 HTTP code in the header</li> | |||
<li>In either case, the response is sent using the <code>respond()</code> function</li> | |||
</ul> | |||
<h4><code>var respond = function(request, response, status, content, content_type)</code></h4> | |||
<ul> | |||
<li>This function is responsible for sending a response to the client</li> | |||
<li>The response may simply be a page with an error in the response headers or it may be an actual file depending on what happened in the functions leading up to the call to <code>respond()</code></li> | |||
<li><code>response.writeHead()</code> writes headers in the response including one which indicates the type of content that is being served</li> | |||
<li><code>response.write()</code> writes content to the body of the response</li> | |||
<li><code>response.end()</code> sends the response to the client</li> | |||
</ul> | |||
<h4>Flow of control</h4> | |||
<ul> | |||
<li>All functions are defined in sequential order</li> | |||
<li>The web server is created</li> | |||
<li>The web server begins listening for incoming requests and the callback function of the <code>listen()</code> function runs</li> | |||
<li>When the server receives a request, <code>request_handler()</code> runs</li> | |||
<li>Subsequent function calls occur with each incoming request based on the nature of the request</li> | |||
</ul> | |||
<h4>Callbacks / asynchronous function calls</h4> | |||
<ul> | |||
<li>Asynchronous means non-blocking</li> | |||
<li>We use an asynchronous function call because we don’t want to block and wait a long time for something to finish while other work can still be done</li> | |||
<li>The functions defined in <code>request_handler()</code> are callback functions used for <code>fs.exists()</code> and <code>fs.stat()</code> since file system operations are slow</li> | |||
<li>To better understand the flow of the code try to rewrite it synchronously (without the use of callback functions)</li> | |||
</ul> |
Latest revision as of 08:09, 4 February 2015
Resources
Video
The video from the lecture given on January 26, 2015 is available now as an MP4.
Notes
Tutorial 3 / Assignment 3
- Assignment 3 is about tinywebserver.js
- 170 lines of code (40 lines are comments)
- tinywebserver.js is a static web server in Node
- The understanding of this code leads to a better understanding of:
- JavaScript
- Callbacks
- What is a web server?
- The tutorial consists of exercises involving changing things around in the code
- The assignment consists of more in-depth questions such as:
- Breaking lines of code and see what happens
- Explaining what various functions do
- Download some web sites and try to serve them from tinywebserver.js
Tinywebserver
- Running tinywebserver.js for the first time and immediately trying to access a web page yields a 404 error because there is no index.html file in the directory of tinywebserver.js
- tinywebserver.js serves the files that are in the directory when the server is executed
- You can put content onto the web server by putting files in its directory
- Any page that you add to the web server in this way is a static resource
- If you download a web page and try to add it to your server, you should expect it to break
- This can happen when downloading modern web pages because the web browser cannot get the entire web page (it gets the html document and any other files that are associated with it but may miss some resources)
- You can go through the lines of code that break by seeing what requests have been successful and what requests have received 404 errors
- tinywebserver.js depends on the Node modules path, http, and fs
- path is used to parse request and resource paths
- fs is for basic file system operations
- http gives you the barest bones of a web
var server = http.createServer(request_handler)
- Creates a web server
- Given a function called request_handler
- request_handler handles every incoming HTTP request
server.listen()
tells the server to begin listening for incoming requests for a the supplied port and hostserver.listen()
is also given a callback function which prints the host and port that the server is listening at
var request_handler = function(request, response)
- This function is used to deal with all incoming requests
- It takes a request and a response object as parameters
- We can find out what the request and response objects are through the debugger
- We can access specific fields of the request object
- If you want to find out what a specific line does; type it in when you run node in the terminal
- requestpath is the incoming url concatenated with the docroot after it has been cleaned up
- We can see that there are two more functions defined within this function
- These additional functions are callback functions which are supplied to other function calls
fs.exists()
is a method which checks if the file specified in the first parameter exists and then calls the function in its second parameter- This second parameter is a callback function with a parameter file_exists
- A callback function is used because we don’t how long it will take for
fs.exists()
to run and we don't want to wait for it so we let it work asynchronously and then call the callback function when it is done - If the requested path is found and it is a directory then
return_index()
is called - If the requested path is found and it is a file then
serve_file()
is called
var return_index = function(request, response, requestpath)
return_index()
sends a response with an index page- If neccessary, it adds a ‘/’ to the requestpath and then adds the
options.index
value, which in this case is index.html - Using fs, it checks to see if the requestpath exists
- Once again, a callback function is seen in the call to
fs.exists()
- Inside the callback function, if the file was found to exist, then
serve_file()
is called - If the file does not exist then
respond()
is called to provide a response with an error
var serve_file = function(request, response, requestpath)
- serve_file() sends a response with the specified file
- It reads the file using
fs.readFile
and then serves the response inside the callback function which is called after reading the file - If there is an error, it sends a response with a 500 error
- If there is no error, it send a response with the contents of the file and a 200 HTTP code in the header
- In either case, the response is sent using the
respond()
function
var respond = function(request, response, status, content, content_type)
- This function is responsible for sending a response to the client
- The response may simply be a page with an error in the response headers or it may be an actual file depending on what happened in the functions leading up to the call to
respond()
response.writeHead()
writes headers in the response including one which indicates the type of content that is being servedresponse.write()
writes content to the body of the responseresponse.end()
sends the response to the client
Flow of control
- All functions are defined in sequential order
- The web server is created
- The web server begins listening for incoming requests and the callback function of the
listen()
function runs - When the server receives a request,
request_handler()
runs - Subsequent function calls occur with each incoming request based on the nature of the request
Callbacks / asynchronous function calls
- Asynchronous means non-blocking
- We use an asynchronous function call because we don’t want to block and wait a long time for something to finish while other work can still be done
- The functions defined in
request_handler()
are callback functions used forfs.exists()
andfs.stat()
since file system operations are slow - To better understand the flow of the code try to rewrite it synchronously (without the use of callback functions)