WebFund 2015W Midterm Review
Video
The video for the lecture given on Feb. 9, 2015 is now available.
Topics for the Midterm
- JavaScript
- Node
- sessions
- notes-demo
- Assignments
- Tutorials
Notes
Assignment 3 review
The point of assignment 3 was to help in understanding how some of the parts of the server are really working. It was also intended to improve your ability to read JavaScript code.
The MIME types are used to indicate what type of data is being sent in a response. MIME stands for Multipurpose Internet Mail Extensions. By using the developer tools in the web browser to look at a response, we can see: headers-‐> content type-‐ > text/html; charset = UTF‐8. This tells the browser what kind of data has been sent to it. If the content type is not supplied, the browser will assume a default type which can lead to problems.
The docroot attribute is used to indicate to the server what location to use as the root when checking for resources.Removing this attribute causes options.docroot to be undefined in the call to path.join()
. When it tries to concatenate undefined with request.url this will cause an error.
The return value of a call to an asynchronous function such as fs.readFile()
does not reflect anything about the work that is delegated to the callback function. Although the function will immediately provide a return value, the callback function has likely not yet been called. Due to this, when serve_file()
returns, nothing has been done yet except for making a call to fs.readFile()
. The respond()
function is not called until the callback function is called.
Everything in the respond()
function is written synchronously. The head of the response is set up and then if there is content, it is written in the response. Then the response is sent to the client. There are no callback functions involved in this.
A way to conceptualize these callback functions is to think of it as delegating work to someone else. I want you to print 100 copy of a paper. You take a piece of paper and go drop it off at a copy center. You get a call from copy center once copying is done and you then have the 100 copies. The copy center only calls you when they are done, they don’t deal with me. Every time you create a function you are creating a little entity to handle the work. It’s not a thread, its just another execution context. Its not a preemptive multitasking its a cooperative multitasking. Get idea of callbacks (Its important).
Node applications are single threaded. Synchronous patterns blocks the server and it won’t respond to any incoming requests. That’s why we should do things asynchronously.
More details about assignment solutions can be found on the assignment 3 page.
Assignment 4 review
This notes-demo code will be on the midterm. You will have the code provided and questions will be about it.
app.js and bin/www are generated as part of Express framework. app.js has the code from the framework that you likely want change to configure things like what to use for page rendering and body parsing etc. The www script is mostly code that you will not touch. Why are they separated into two different files? bin/www and app.js were one file in previous Express versions. Problem was that app.js was longer.
error.jade is a template for error pages.
package.json is not used in the normal operation of web server. It is used to determine what to download to the node_modules directory before actually running the server. In this course the prof gives these modules to us. Normally when an app is downloaded, you don’t get the modules so they must be downloaded through NPM. Every node module has its own package.json that has its dependencies listed. There are hundreds of these modules recursively downloaded. They are individually tiny in size but constitute the majority of the size in the apps we have seen so far. There may also be different versions of the same module found in different directories in the node_modules folder. If both Express and Jade depended on a particular module but different versions of it, they will each have their own copy with the correct version. Even when different modules require the same version of another module, they will still each have their own copy.
To display an error on the page when the user tries to edit a note which does not exist, add an error attribute to the object being passed to notes.jade and give it the error value pulled from the querystring. Then add an if statement in notes.jade to display the error if there is one. This can be modeled from the same thing that is done for the user not logged in error on the index page.
To change the font properties of the h1 tags, add an appropriate tag into the CSS file.
More details about assignment solutions can be found on the assignment 4 page.
“I believe in recycling, when it comes to tests” ‐ Anil Somayaji
Other Node and JavaScript items
We'll later cover the topic of call conventions from JavaScript: The Good Parts.
In the index.js file of the notes-demo, router is set in, var router = express.Router();
This line indicates that Router()
is a method of express and it will return something. Presumably it’s an object.
Why did we have to call a function?
That function is a constructor, we can tell because it is capitalized (by convention). There is no new before the call. This is because “new” does not need to be present in JavaScript.
When var routes = require('./routes/index');
is called in app.js, it is expected that the module.exports object in index.js is given a value to be used. In this case it is set to the router object.