<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=LeeCroft</id>
	<title>Soma-notes - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=LeeCroft"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php/Special:Contributions/LeeCroft"/>
	<updated>2026-05-12T21:32:26Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_24&amp;diff=20940</id>
		<title>WebFund 2016W Lecture 24</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_24&amp;diff=20940"/>
		<updated>2016-04-09T03:34:26Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on April 7, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec24-07Apr2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In Class===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 24&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Modern web pages are a mess because they draw content&lt;br /&gt;
(and code) from many sources&lt;br /&gt;
* Including that content is easy, you just make a few&lt;br /&gt;
  changes, typically saying to load some JavaScript&lt;br /&gt;
&lt;br /&gt;
Is there any barrier between that external code&lt;br /&gt;
and your client-side code?&lt;br /&gt;
* not really&lt;br /&gt;
* there are technologies for separating content, and&lt;br /&gt;
* JavaScript provides some separation (e.g. lexical scoping)&lt;br /&gt;
* But everyone shares the DOM&lt;br /&gt;
&lt;br /&gt;
So if you control any of the content in a page,&lt;br /&gt;
you can potentially change any of it&lt;br /&gt;
* Imagine censoring comments&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If you want others to incorporate your stuff into their&lt;br /&gt;
pages, what do you offer?&lt;br /&gt;
&lt;br /&gt;
Maybe offer some sort of API...but how?&lt;br /&gt;
&lt;br /&gt;
Maybe just give a link to my animated GIFs?&lt;br /&gt;
 - but a link to which one?&lt;br /&gt;
 - random every time?&lt;br /&gt;
&lt;br /&gt;
Why not just give them JSON + JavaScript?&lt;br /&gt;
* Just give JavaScript, it will load the JSON on its own&lt;br /&gt;
&lt;br /&gt;
So when anyone talks about &amp;quot;Web Services&amp;quot;&lt;br /&gt;
* Make an API out of HTTP requests (GET, POST, ...)&lt;br /&gt;
&lt;br /&gt;
Might be accessed by&lt;br /&gt;
* a web page from the developer&lt;br /&gt;
* 3rd party web page&lt;br /&gt;
* mobile apps&lt;br /&gt;
* any other program connected to the Internet&lt;br /&gt;
&lt;br /&gt;
It doesn&#039;t matter to the server.&lt;br /&gt;
THIS is the key benefit of a &amp;quot;RESTful&amp;quot; API&lt;br /&gt;
&lt;br /&gt;
Used to be 2 ways to do a &amp;quot;Web Service&amp;quot;:&lt;br /&gt;
* REST (e.g., use regular HTTP GET and POST)&lt;br /&gt;
* SOAP&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
(at least) Two schools of thought for code talking to&lt;br /&gt;
code:&lt;br /&gt;
* make everything a function call&lt;br /&gt;
* do something weird for network communication&lt;br /&gt;
&lt;br /&gt;
SOAP is just a way to do function calls over HTTP&lt;br /&gt;
&lt;br /&gt;
How do you do a function call over HTTP?&lt;br /&gt;
 - prepare arguments&lt;br /&gt;
 - send arguments&lt;br /&gt;
 - invoke remote function&lt;br /&gt;
 - retrieve result&lt;br /&gt;
 - store result&lt;br /&gt;
&lt;br /&gt;
What about&lt;br /&gt;
* types?&lt;br /&gt;
  - have to communicate that&lt;br /&gt;
* but think how complicated data structures can get&lt;br /&gt;
* Solution: XML Schemas for data&lt;br /&gt;
&lt;br /&gt;
SOAP is an example of RPC: remote procedure calls&lt;br /&gt;
but over HTTP&lt;br /&gt;
&lt;br /&gt;
RPC turns local code into network code, but&lt;br /&gt;
local code isn&#039;t ready to face the network&lt;br /&gt;
 * security&lt;br /&gt;
 * efficiency&lt;br /&gt;
&lt;br /&gt;
So why SOAP?&lt;br /&gt;
 * the old ways of doing RPC were blocked by firewalls&lt;br /&gt;
&lt;br /&gt;
There&#039;s a &amp;quot;winpopup&amp;quot; service in Windows&lt;br /&gt;
 * pops up a window&lt;br /&gt;
 * as requested by a remote host&lt;br /&gt;
&lt;br /&gt;
REST came along when &amp;quot;web natives&amp;quot; (programmers)&lt;br /&gt;
realized they didn&#039;t need all that infrastructure&lt;br /&gt;
&lt;br /&gt;
Easy to see new things as &amp;quot;mostly old things&amp;quot;&lt;br /&gt;
But really you need to see it as its own thing&lt;br /&gt;
to really master it&lt;br /&gt;
&lt;br /&gt;
What is the web?&lt;br /&gt;
&lt;br /&gt;
* Publishing&lt;br /&gt;
* e-commerce (mail order catalogs)&lt;br /&gt;
* Information access (fine-grained)&lt;br /&gt;
* Multimedia&lt;br /&gt;
  - realplayer =&amp;gt; youtube&lt;br /&gt;
  - CDs and videos&lt;br /&gt;
* VoIP (telephony), telegrams&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* Social media&lt;br /&gt;
  - gossip&lt;br /&gt;
&lt;br /&gt;
* &amp;quot;Uberization&amp;quot;&lt;br /&gt;
  - use a mobile app to connect people for business&lt;br /&gt;
 &lt;br /&gt;
Some of the progress in web technologies today is to&lt;br /&gt;
make &amp;quot;platforms&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Platforms are really technology looking for a purpose&lt;br /&gt;
&lt;br /&gt;
Weird thing about the web is it is a &amp;quot;platform&amp;quot; that&lt;br /&gt;
nobody controls&lt;br /&gt;
 * Good: allows innovation&lt;br /&gt;
 * Bad: evolutionary cruft&lt;br /&gt;
&lt;br /&gt;
Programmers HATE evolutionary cruft&lt;br /&gt;
 - it doesn&#039;t make sense!&lt;br /&gt;
 - its a mess!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Many, many investors don&#039;t like the web as a platform&lt;br /&gt;
 - they want to control the platform&lt;br /&gt;
&lt;br /&gt;
iOS as a platform&lt;br /&gt;
 * apple gets 30%&lt;br /&gt;
&lt;br /&gt;
Google takes a cut from the Play store as well&lt;br /&gt;
&lt;br /&gt;
Platforms are a play at a monopoly in a space&lt;br /&gt;
&lt;br /&gt;
Two directions&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Logistics====&lt;br /&gt;
*Final exam on the 12th, 2 hours long, with similar format to the midterm&lt;br /&gt;
*Solution for Assignment #6 will be posted on Sunday, but can be submitted until the end of Saturday&lt;br /&gt;
*A study session will be on the coming Monday on 11th at [http://carleton.ca/ims/rooms/steacie-building-103/ Steacie Building 103]&lt;br /&gt;
**No set agenda&lt;br /&gt;
**Focus on exam-storage&lt;br /&gt;
**Attend and study beforehand, come with questions&lt;br /&gt;
**If you just sit and listen, better to just study by yourself for 2 hours&lt;br /&gt;
**If good questions are asked, will stick around around 10-12&lt;br /&gt;
&lt;br /&gt;
====End of the Line====&lt;br /&gt;
*What did I leave out, what&#039;s up ahead?&lt;br /&gt;
*Tuesday we talked about scalability, but today we&#039;ll talk about what you&#039;re likely to encounter now and what to expect in the near future.&lt;br /&gt;
*Let&#039;s pick a modern [http://www.arstechnica.com/ website], and see how it compares to websites we&#039;ve built.&lt;br /&gt;
**There&#039;s a reader comment section. What&#039;s happening?&lt;br /&gt;
***The comments are loaded in the background, so the server doesn&#039;t load them all at the start.&lt;br /&gt;
***Let&#039;s look at the page source - it seems to have comments in it - no it&#039;s a list of stories, but I didn&#039;t expect it to be in the DOM.&lt;br /&gt;
***Do we load pages all at once or in pieces? There are a lot of pieces and parts on the page, some images, JavaScript, GIFs, JSON.&lt;br /&gt;
**A lot of the pieces being requested have nothing to do with the main site. Will your webpage ever look like this? Does ArsTechnica even create and publish all this content?&lt;br /&gt;
***Where does this complexity come from? There&#039;s icons, banner ads, sponsored stories that are clickbait to gather click impressions.&lt;br /&gt;
***Almost no work to include these - only a few JavaScript scripts to include and let them run to grab someone else&#039;s content.&lt;br /&gt;
***e.g. code for share buttons comes from Twitter and other social media sites.&lt;br /&gt;
***You can combine content from other people &amp;amp; create a big mess, because it&#039;s easy.&lt;br /&gt;
***The embedded code does whatever it wants, and it&#039;s inherently not separate from your own content as they share the DOM.&lt;br /&gt;
***Compromised code originating from ad servers can do almost anything. Let&#039;s say people are revealing Google&#039;s secrets. Instead of blocking connections, just have ad code check the DOM for &amp;quot;google&amp;quot; or &amp;quot;secrets&amp;quot; in the main content and erase them or block them off with ads&lt;br /&gt;
**The Web is not intended to be this mishmash of content&lt;br /&gt;
&lt;br /&gt;
====Functionality====&lt;br /&gt;
*What if you want your stuff incorporated into other people&#039;s pages - how do they make use of your functionality?&lt;br /&gt;
**APIs are interfaces to code that we haven&#039;t covered in this class, but we&#039;re on the web and not running on a machine directly&lt;br /&gt;
*You could provide just images and give them links, but if something dynamic is needed with parameters and controls like a twitter feed, then it needs to be data-driven&lt;br /&gt;
**Need JSON+JS to be loaded on the browser to do background GET/POSTs, and we&#039;ve already seen this&lt;br /&gt;
*Web services are a buzzword for API over HTTP requests: GET/POST depending on what&#039;s important, loaded by browsers or other clients subject to interpretation at the end of the wire.&lt;br /&gt;
**Facebook has a connect API for apps to authenticate people on phones and when people log in to Facebook on Desktop browsers. How do they look different? They&#039;re the same API because it&#039;s a login specific to Facebook users. Wrapping/delivering code with HTTP means it can apply to everyone on the web through the remote service&#039;s API&lt;br /&gt;
*REST is Representational State Transfer, a successor to Simple Object Access Protocol (SOAP). They both are a way to access functionality over HTTP.&lt;br /&gt;
*Older inter-process communications are through function calls on specific network protocols, but that&#039;s an abomination because arguments need to be determined, formatted, sent, and retrieved. There&#039;s packaging and unpackaging involved, and data structures to parse. &lt;br /&gt;
**SOAP&#039;s solution was to use XML as a successor to SGML, a language for describing data rather than documents (which is what HTML does). It&#039;s a complicated way to send a dictionary + grammar book at once.&lt;br /&gt;
***Remote procedure calls on the network has been around a long time, but it&#039;s a dumb idea because of lack of trust in unknown remote code. Security + Efficiency a problem on the web. &lt;br /&gt;
***RPC bypasses firewalls that blocked other code like the Windows popup messaging functionality that was designed for local networks with considerations to convenience and not security.&lt;br /&gt;
***Programs trying remote access, made use of HTTP to make things simple. We know REST has stopped trying to describe the whole universe inside schemas and just use GET/POSTs. We&#039;ve already built websites under the same networked architecture.&lt;br /&gt;
&lt;br /&gt;
*Humans try relate new concepts with the familiar, describing the first cars as horseless carriages.&lt;br /&gt;
**The internet is due for change and we don&#039;t know who&#039;s going to start it, what direction it&#039;s going, and what form it&#039;s going to take on in the future.&lt;br /&gt;
***The functionality of displaying documents on the web supplants published books, e-commerce to replace mail-order catalogues, multimedia to consolidate cassettes and cds, and telephony to communicate better than telegrams.&lt;br /&gt;
***However, social media has no pre-internet equivalent to imitate except gossiping. It is a new format that evolved from webpages enabling comments that are similar to academic annotations.&lt;br /&gt;
***What&#039;s next? New usage of the web include &#039;&#039;Uberization&#039;&#039; to bring people closer to businesses.&lt;br /&gt;
***Uber and Facebook have a purpose. Is the &amp;quot;Internet of Things&amp;quot; useful for smart web-capable fridges and other appliances? It&#039;s more of a buzzword that nobody has made intensive use of, unlike how driverless cars can be conceptualized and implemented with a purpose.&lt;br /&gt;
**Efforts are made to create &amp;quot;platforms&amp;quot;, things to be built on for other services&lt;br /&gt;
***What really is a platform? Technology looking for a purpose and/or users&lt;br /&gt;
***The web is becoming the universal platform that nobody can control. &lt;br /&gt;
**The free nature of the web enables innovation and evolution past its designed standards, however it is rife with legacy technology. Sometimes they&#039;re senseless, vestigial cruft that&#039;s no longer being maintained but still used out of convenience. They can be ignored without maintenance or scrapped and started over, but it&#039;s dangerous for programmers to assume they know how other people will use the code out of hubris.&lt;br /&gt;
&lt;br /&gt;
====Future Platforms====&lt;br /&gt;
*People that are investors in companies dislike the web, since they have no control over it.&lt;br /&gt;
*iOS has a 30% of everything on iTunes, and so does Google and Microsoft over their respective app stores.&lt;br /&gt;
**Microsoft is transitioning from a OS-oriented company to a service-oriented company (Business + Azure cloud services)&lt;br /&gt;
*The web is an uncontrolled environment among other platforms have been acquired with monopolies.&lt;br /&gt;
**It will be acquired and turned stale into a legacy platform or supersede all others to become &#039;&#039;the&#039;&#039; platform.&lt;br /&gt;
**Developers can and should choose their own technology, to become involved in choosing what direction they want the web to go.&lt;br /&gt;
**Why become involved in the development of code? Open-source developments have increased in prominence, with Linux based OS taking on greater marketshare over Windows. It has co-evolved with the web through many programmers to become a better foundation for building technologies.&lt;br /&gt;
**We don&#039;t know what&#039;s coming next (augmented reality), but choosing the right platforms are the only way to develop infrastructure towards the next generation. The web is going to stick around, so good luck studying.&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_23&amp;diff=20933</id>
		<title>WebFund 2016W Lecture 23</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_23&amp;diff=20933"/>
		<updated>2016-04-07T05:03:26Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on April 5, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec23-05Apr2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In class===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 23&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Scalability&lt;br /&gt;
&lt;br /&gt;
* You replicate your web application, should be&lt;br /&gt;
  &amp;quot;embarassingly parallel&amp;quot; (no direct interaction)&lt;br /&gt;
&lt;br /&gt;
* Communication between servers happens through the&lt;br /&gt;
  backend database&lt;br /&gt;
&lt;br /&gt;
Why not have the web servers talk directly to each other?&lt;br /&gt;
 - you then have to figure out how to do&lt;br /&gt;
   synchronization/concurrency right&lt;br /&gt;
 - that&#039;s what databases are for!&lt;br /&gt;
&lt;br /&gt;
So how in the world do you scale up databases?&lt;br /&gt;
&lt;br /&gt;
First answer: use a minimal solution&lt;br /&gt;
 - only get the functionality that you want&lt;br /&gt;
&lt;br /&gt;
First rule of scalability&lt;br /&gt;
 - you can&#039;t do everything at scale&lt;br /&gt;
&lt;br /&gt;
So, you have to choose what you will do&lt;br /&gt;
&lt;br /&gt;
Why are sacrifices necessary?&lt;br /&gt;
&lt;br /&gt;
latency versus bandwidth&lt;br /&gt;
&lt;br /&gt;
bandwith: bits transferred per second on average&lt;br /&gt;
latency: time to get first bit of response after request&lt;br /&gt;
&lt;br /&gt;
Consider a large truck full of hard disks driving&lt;br /&gt;
across Canada.&lt;br /&gt;
  - very, very high bandwidth&lt;br /&gt;
  - very, very high latency as well!&lt;br /&gt;
    (2 weeks to get first bit of response)&lt;br /&gt;
&lt;br /&gt;
Ideally, you want high bandwidth and low latency&lt;br /&gt;
 - bandwidth you get through parallelism&lt;br /&gt;
 - latency has to be engineered&lt;br /&gt;
&lt;br /&gt;
A &amp;quot;supercomputer&amp;quot; is one with low-latency memory access,&lt;br /&gt;
for LOTS of memory&lt;br /&gt;
  - so it has to have fast interconnects&lt;br /&gt;
  - thus, accesses to different nodes aren&#039;t much&lt;br /&gt;
    slower than local accesses&lt;br /&gt;
&lt;br /&gt;
Challeng for large web apps is having the database&lt;br /&gt;
answer queries with low latency&lt;br /&gt;
&lt;br /&gt;
But some amount of latency is inevitable&lt;br /&gt;
 - speed of light is finite&lt;br /&gt;
&lt;br /&gt;
So if you want fast access to your webserver worldwide&lt;br /&gt;
 - you need to replicate across the globe&lt;br /&gt;
 - be close to your clients&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
NoSQL databases became popular because of latency&lt;br /&gt;
concerns&lt;br /&gt;
 - you needed to be as fast as possible,&lt;br /&gt;
 - so strip it to the bone&lt;br /&gt;
&lt;br /&gt;
Use an in-memory key-value store if it is sufficient&lt;br /&gt;
  - lowest latency&lt;br /&gt;
  - least functionality&lt;br /&gt;
&lt;br /&gt;
If you have to, use an SQL database&lt;br /&gt;
  - highest latency&lt;br /&gt;
  - most functionality&lt;br /&gt;
&lt;br /&gt;
Or use something in between (MongoDB)&lt;br /&gt;
&lt;br /&gt;
Once you choose the type of database, you OPTIMIZE&lt;br /&gt;
 - minimize I/O and computation required per access&lt;br /&gt;
   (read or write)&lt;br /&gt;
 - example: query optimization&lt;br /&gt;
 - how you form the query&lt;br /&gt;
   - how database is organized&lt;br /&gt;
&lt;br /&gt;
Count the number of web pages that have the word&lt;br /&gt;
 &amp;quot;amazing&amp;quot; in them&lt;br /&gt;
&lt;br /&gt;
How?&lt;br /&gt;
 - first, need a database with a copy of the web pages&lt;br /&gt;
 - then, you could do linear search through all&lt;br /&gt;
   of the web pages...&lt;br /&gt;
&lt;br /&gt;
I ask this because a web search is a massive challenge&lt;br /&gt;
in query optimization&lt;br /&gt;
&lt;br /&gt;
 - need to limit scope as early as possible in query&lt;br /&gt;
 - organize data so queries are quick to be answered&lt;br /&gt;
    - precompute as much as possible&lt;br /&gt;
&lt;br /&gt;
The best you can do is table lookup. So have the right&lt;br /&gt;
tables ready!&lt;br /&gt;
&lt;br /&gt;
Key tool is making an INDEX&lt;br /&gt;
 - table of search term and pointers to data&lt;br /&gt;
&lt;br /&gt;
E.g., you have a table of customers sorted by ID&lt;br /&gt;
 - have an index of names, so a table of names versus&lt;br /&gt;
   IDs&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Counting Log Entries====&lt;br /&gt;
&lt;br /&gt;
*Exercise related to Assignment 6: The following is to help you in getting started on adding in single-page functionality for the web app.&lt;br /&gt;
**How do we get the data related to the number of log files and the number of entries from the database? Ask the database how many log files are there?&lt;br /&gt;
**What do we want to do? Count the number of entries. Use the count method.&lt;br /&gt;
***&amp;lt;code&amp;gt;db.logs.count()&amp;lt;/code&amp;gt; counts records&lt;br /&gt;
**How many distinct values are there?&lt;br /&gt;
***&amp;lt;code&amp;gt;db.logs.distinct(“file”)&amp;lt;/code&amp;gt; counts how many files we have&lt;br /&gt;
**The placeholder information is currently filled in on the server&lt;br /&gt;
***How? First go to views. The Jade file already has &amp;lt;code&amp;gt;numfiles&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;numentries&amp;lt;/code&amp;gt; variables which are passed in to the template through the call to &amp;lt;code&amp;gt;render()&amp;lt;/code&amp;gt; in the routes. How do we update them?&lt;br /&gt;
**What problems do we run in to when using the &amp;lt;code&amp;gt;count()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;distinct()&amp;lt;/code&amp;gt; methods?&lt;br /&gt;
***Reloading the page is one problem&lt;br /&gt;
***The real problem is with the callbacks&lt;br /&gt;
****In order to do this, we need to do 2 separate database operations to get the values. We can try to combine them, however, this entails nesting one call to the database within the other callback, and therefore is messy.&lt;br /&gt;
****Instead, we can make them each as a separate route. Set up the following in index.js:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
router.get(‘/count’, function(req,res){&lt;br /&gt;
   function reportCount(err,count){&lt;br /&gt;
      if (err) {&lt;br /&gt;
         res.send(-1);&lt;br /&gt;
      }&lt;br /&gt;
      else {&lt;br /&gt;
         res.send(count);&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   logsCollection.count({}, reportCount);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*A problem with this code is that the count is interpreted as a response code&lt;br /&gt;
*We can change the code to fix this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
router.get(‘/count’, function(req,res){&lt;br /&gt;
   function reportCount(err,count){&lt;br /&gt;
      if err {&lt;br /&gt;
         res.sendStatus(500);}&lt;br /&gt;
      } else {&lt;br /&gt;
         res.send({count: count});&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   logsCollection.count({}, reportCount);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*And the route for file count:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
router.get(‘/storeFiles’, function(req,res) {&lt;br /&gt;
   function reportStoredFiles(err,files){&lt;br /&gt;
      if err {&lt;br /&gt;
         res.sendStatus(500);&lt;br /&gt;
      } else{&lt;br /&gt;
         res.send(files); //to get number of files res.send({count: files.length});&lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   logsCollection.distinct(“file”, reportStoredFiles);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*This code allows us to get these values to the client&lt;br /&gt;
*Currently, we do not have any client-side code to do anything with the values&lt;br /&gt;
*We want to somehow update the page to reflect the correct values&lt;br /&gt;
*We can do this on the server-side before sending the page to the client but this can be a pain so let’s change this so that we do an AJAX request from the browser and update the DOM&lt;br /&gt;
*We will need a client-side script to do this&lt;br /&gt;
*We can reference exam-storage to see how to link our scripts in the Jade templates&lt;br /&gt;
**In layout.jade, we want to link the jquery script since we will be using it&lt;br /&gt;
**From account.jade, we can see where our main client-side script was linked. We will need to do something like this for index.jade&lt;br /&gt;
**Make sure that the scripts are stored somewhere in the public directory and that the paths in the links are correct&lt;br /&gt;
*We can add some default text into index.jade to display if we do not have the actual file and log entry counts&lt;br /&gt;
*Then we need to write our query.js script&lt;br /&gt;
**The functions in this script are used to update the number of logs and number of entries shown in the browser. So, when you reload the page after uploading a file, the number of logs and entries also get updated accordingly.&lt;br /&gt;
***&amp;lt;code&amp;gt;updateStats()&amp;lt;/code&amp;gt; is used to make the AJAX requests to the server to get the updated values&lt;br /&gt;
***&amp;lt;code&amp;gt;updatesStatsText()&amp;lt;/code&amp;gt; is used to update the DOM with the new values&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$(function() {&lt;br /&gt;
   var numEntries = 0;&lt;br /&gt;
   var numFiles = 0;&lt;br /&gt;
   var stats = $(“#stats”);&lt;br /&gt;
&lt;br /&gt;
   function updatesStatsText() {&lt;br /&gt;
      stats.html(“Currently we have “ + numEntries + “ log entries in “ + numFiles + “log files.”);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   function updateStats() {&lt;br /&gt;
      var numUpdated = 0;&lt;br /&gt;
      $.getJSON(“/ count”,function(v){&lt;br /&gt;
         numEntries = v.count;&lt;br /&gt;
         numUpdated++;&lt;br /&gt;
&lt;br /&gt;
         if(numUpdated &amp;gt;= 2){&lt;br /&gt;
            updateStatsText();&lt;br /&gt;
         }&lt;br /&gt;
      });&lt;br /&gt;
&lt;br /&gt;
      $.getJSON(“/storedFileCount”, function(v){&lt;br /&gt;
         numFiles = v.count;&lt;br /&gt;
         numUpdated++;&lt;br /&gt;
&lt;br /&gt;
         if(numUpdated &amp;gt;= 2){&lt;br /&gt;
            updateStatsText(); });&lt;br /&gt;
         }&lt;br /&gt;
      });&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   updateStats();&lt;br /&gt;
}      &lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*With the assignment, everything should show up at the bottom of the page.&lt;br /&gt;
*This means upating the DOM in a similar fashion to what we have done here&lt;br /&gt;
&lt;br /&gt;
====Scalability====&lt;br /&gt;
&lt;br /&gt;
*When replicating your web application, your code should be “embarrassingly parallel” (no direct interaction)&lt;br /&gt;
*Communication between servers happens through the backend database&lt;br /&gt;
*Why not have the web servers talk directly to each other?&lt;br /&gt;
**You have to figure out how to do synchronization/concurrency right&lt;br /&gt;
**That&#039;s what databases are for! Let it deal with the problem&lt;br /&gt;
*So, how in the world do you scale up databases?&lt;br /&gt;
*First answer: use a minimal solution&lt;br /&gt;
**Only get the functionality that you want&lt;br /&gt;
*Basic/first rule of scalability&lt;br /&gt;
**You cant do everything at scale&lt;br /&gt;
*So you have to choose what you will do. Have to make some sacrifices&lt;br /&gt;
*Why are sacrifices necessary?&lt;br /&gt;
*Latency vs Bandwidth&lt;br /&gt;
**Bandwidth: bits transferred per second on average&lt;br /&gt;
**Latency: time to get first bit of response after request&lt;br /&gt;
*Consider a large truck full of hard disks driving across Canada. What’s the bandwidth of this truck? Low bandwidth or high bandwidth?&lt;br /&gt;
**It’s very very high bandwidth takes two weeks to travel across the country, but when it arrives, a lot of data has been transferred&lt;br /&gt;
**Very very high latency as well! (2 weeks to get first bit of response)&lt;br /&gt;
*Ideally you want HIGH BANDWIDTH and LOW LATENCY&lt;br /&gt;
**Bandwidth you get through parallelism&lt;br /&gt;
**Latency has to be engineered.&lt;br /&gt;
*Latency is hard so,&lt;br /&gt;
**A “supercoumputer” is one with low latency memory access, for lots of memory.&lt;br /&gt;
***So it has to have fast interconnects between the nodes. Fast meaning low latency, not high bandwidth.&lt;br /&gt;
***Thus, accesses to different nodes aren&#039;t much slower than local accesses&lt;br /&gt;
*Challenge for large web apps is having the database answer queries with low latency&lt;br /&gt;
*If a user goes to your website the page should be loaded fast. Even if it&#039;s a bit slow, they will be unhappy. So, you need the low latency.&lt;br /&gt;
*But some amount of latency is inevitable (laws of physics, Einstein)&lt;br /&gt;
**The speed of light is finite. A nanosecond in wire is a short piece of wire. A microsecond a pretty good size spool. Milliseconds is thousand times larger.&lt;br /&gt;
**How far do signals go?&lt;br /&gt;
***Microprocessors operate on the nanoseconds&lt;br /&gt;
***Networks on the milliseconds&lt;br /&gt;
***Ping time to get to the other side of the globe, 100s of millisecond&lt;br /&gt;
****If it&#039;s more than 50 milliseconds or half a second, people will notice&lt;br /&gt;
*So if you want fast access to your web server worldwide&lt;br /&gt;
**You need to replicate across the globe&lt;br /&gt;
**Be close to your clients&lt;br /&gt;
*In general, the more the functionality a database will have, the longer it will take&lt;br /&gt;
*NoSQL databases became popular because of latency concerns&lt;br /&gt;
**You needed to be as fast as possible&lt;br /&gt;
**So strip it to the bone&lt;br /&gt;
*Use an in-memory key value store if it is sufficient&lt;br /&gt;
**Lowest latency&lt;br /&gt;
**Least functionality&lt;br /&gt;
*If you have to, use an SQL database&lt;br /&gt;
**Higher latency&lt;br /&gt;
**Most functionality&lt;br /&gt;
*Or use something in between (MongoDB)&lt;br /&gt;
*Once you choose the type of database, you optimize&lt;br /&gt;
**Minimize I/O and computation require per access (read or write)&lt;br /&gt;
**Example: query optimization&lt;br /&gt;
**How you form the query&lt;br /&gt;
**How the database is organized&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Why Google is getting big bucks? How do you query the entire Web?&lt;br /&gt;
**Google is good at query optimization&lt;br /&gt;
*Google has a copy of everything on the Web. Their Web crawlers do this for them&lt;br /&gt;
*How do you make a query of the entire Web?&lt;br /&gt;
**Use a very big database!&lt;br /&gt;
**The interface to query through Google is just a regular website, it&#039;s just working with very large data sets!&lt;br /&gt;
*How can we count the number of web pages that have the word that have the word “amazing” in them?&lt;br /&gt;
**First you need a database with a copy of all the web pages&lt;br /&gt;
**Then you could do linear search through all the of the pages of the web (this is not an efficient solution)&lt;br /&gt;
*I ask this because a web search is a massive challenge in query optimization&lt;br /&gt;
**We need to limit scope as early as possible in query&lt;br /&gt;
**Organize data so queries can be answered quickly&lt;br /&gt;
**Precompute as much as possible&lt;br /&gt;
*The best you can do is table lookup. So have the right tables ready!&lt;br /&gt;
*When you do a Google search, it is picking out answer from answers it already figured out before&lt;br /&gt;
*A key tool is making an INDEX&lt;br /&gt;
**Table of search terms and pointers to data&lt;br /&gt;
**e.g. If you have a table of customers sorted by ID&lt;br /&gt;
***Have an index of names, so a table of names vs IDs&lt;br /&gt;
**An index is a table that is very short, one column for the indexed attribute and the second for the ID&lt;br /&gt;
**Is this what Google is doing? No, they are doing much fancier things but don’t tell anyone because of layers and layers of proprietary stuff&lt;br /&gt;
*Microsoft has about 100 millions lines of code, Linux kernel is 20 millions lines of code, Google is estimated at about 2 billion lines of code. This is not surprising, there is a lot of functionality&lt;br /&gt;
*Look up MapReduce. You can use it to find all web pages with “amazing”. All you do is you have a cluster of machines with data across them and you tell each machine to count the number of times amazing appears in the web pages you have on that machine and then accumulate altogether to be a single count. It can also handle failures during a query. Data is being stored in special replicated way, so that if there is one particular node that is failing, others can take over the job. It is functional programming at scale.&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
* [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/analyzeLogs-filecount.zip analyzeLogs-filecount]&lt;br /&gt;
&lt;br /&gt;
Note this version has node_modules removed; copy this directory from analyzeLogs-sol or run &amp;quot;npm install&amp;quot;.&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_22&amp;diff=20907</id>
		<title>WebFund 2016W Lecture 22</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_22&amp;diff=20907"/>
		<updated>2016-04-04T03:06:12Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: /* And now the web */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 31, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec22-31Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In Class===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 22&lt;br /&gt;
------------&lt;br /&gt;
Web Security&lt;br /&gt;
&lt;br /&gt;
Security in isolated systems&lt;br /&gt;
 - no networking&lt;br /&gt;
&lt;br /&gt;
What is the threat model?&lt;br /&gt;
&lt;br /&gt;
Threat model&lt;br /&gt;
 - what attacks are you trying to defend against&lt;br /&gt;
 - what attacks are you NOT trying to defend against&lt;br /&gt;
&lt;br /&gt;
Computer in an isolated room&lt;br /&gt;
 - ignore physical attacks, e.g. sledgehammers&lt;br /&gt;
&lt;br /&gt;
How to compromise this computer?&lt;br /&gt;
 - someone bad starts typing&lt;br /&gt;
 - someone inserts malicious media/device&lt;br /&gt;
   - parking lot flashdrive attack&lt;br /&gt;
     (solution: epoxy)&lt;br /&gt;
 - electromagnetic emissions&lt;br /&gt;
     (live in a faraday cage)&lt;br /&gt;
&lt;br /&gt;
Computer on a network&lt;br /&gt;
 - all local attacks&lt;br /&gt;
 - attacker can send arbitrary network data to system&lt;br /&gt;
 - attacker can listen in on network traffic&lt;br /&gt;
 - attacker can change outgoing traffic&lt;br /&gt;
&lt;br /&gt;
Basic defense: network crypto&lt;br /&gt;
 - encrypt to hide, sign data to protect integrity,&lt;br /&gt;
   verify authenticity&lt;br /&gt;
&lt;br /&gt;
Who will access?&lt;br /&gt;
 - authenticated, authorized users&lt;br /&gt;
 - unauthorized individuals/systems&lt;br /&gt;
 - attackers (can be either of the above)&lt;br /&gt;
&lt;br /&gt;
How can an attacker be &amp;quot;authorized&amp;quot;?&lt;br /&gt;
 - social engineering&lt;br /&gt;
   (can you please reset my password?)&lt;br /&gt;
 - brute force (try all the passwords)&lt;br /&gt;
 - technical compromise of credentials (keylogger)&lt;br /&gt;
 - authorized user turns/is malicious&lt;br /&gt;
   (insider attack)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And now the web&lt;br /&gt;
&lt;br /&gt;
Who can access your application?&lt;br /&gt;
 - ANYBODY&lt;br /&gt;
 - for public web apps, ANYBODY is an authorized user&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So just from this we can see that web security is going&lt;br /&gt;
to be hard.&lt;br /&gt;
&lt;br /&gt;
The other half of the nightmare: the browser&lt;br /&gt;
&lt;br /&gt;
Ideally, to secure systems you isolate components&lt;br /&gt;
  - build big walls&lt;br /&gt;
&lt;br /&gt;
Your web browser has walls between pages...but there are&lt;br /&gt;
BIG HOLES (on purpose)&lt;br /&gt;
&lt;br /&gt;
 - cookies, in particular, are shared&lt;br /&gt;
 - this is bad because the scope of cookies is&lt;br /&gt;
   determined by DNS (domain name system)&lt;br /&gt;
&lt;br /&gt;
DNS has almost no security&lt;br /&gt;
 - there is DNSSEC but nobody uses it, yet at least&lt;br /&gt;
&lt;br /&gt;
You can better protect cookies by using HSTS&lt;br /&gt;
 - force HTTPS everywhere on a page/domain&lt;br /&gt;
 - protects against mixes of http/https content and&lt;br /&gt;
   downgrade attacks&lt;br /&gt;
&lt;br /&gt;
Why do we have cookies in the first place?&lt;br /&gt;
 - because HTTP was supposed to be stateless&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Also...&lt;br /&gt;
&lt;br /&gt;
Cross-site Scripting (XSS) attacks&lt;br /&gt;
 - not necessarily cross-site, not necesarily scripting&lt;br /&gt;
 - &amp;quot;web injection attacks&amp;quot;&lt;br /&gt;
 - see anywhere attacker can inject content into a page&lt;br /&gt;
   - (ads)&lt;br /&gt;
   - comments&lt;br /&gt;
   - social media&lt;br /&gt;
   - &amp;quot;user-generated content&amp;quot;&lt;br /&gt;
 - failure to sanitize untrusted input that is inserted&lt;br /&gt;
   into a web page&lt;br /&gt;
   (adding a comment to a page)&lt;br /&gt;
 - otherwise, you can insert arbitrary HTML, CSS,&lt;br /&gt;
   and JavaScript into the page&lt;br /&gt;
&lt;br /&gt;
Cross-site request forgery&lt;br /&gt;
 - makes use of cookies automatically being sent&lt;br /&gt;
   to domain, no matter where the link came from&lt;br /&gt;
 - can stop using tokens in URLs, or checking&lt;br /&gt;
   Referer/Origin headers (and a few other ways)&lt;br /&gt;
&lt;br /&gt;
Same-origin policy&lt;br /&gt;
 - data should only be loaded from the originating&lt;br /&gt;
   server&lt;br /&gt;
 - but, you can load almost anything else from&lt;br /&gt;
   anywhere&lt;br /&gt;
    - images&lt;br /&gt;
    - sound&lt;br /&gt;
    - javascript&lt;br /&gt;
&lt;br /&gt;
So what about JSON?&lt;br /&gt;
 - when you load it with AJAX, you have same origin&lt;br /&gt;
   policy&lt;br /&gt;
 - but if you just treated it as a script...&lt;br /&gt;
 - and what if you were logged in&lt;br /&gt;
   - bank sending data as JSON&lt;br /&gt;
&lt;br /&gt;
JSON files are valid JavaScript but they don&#039;t specify&lt;br /&gt;
the name to give to the data structure&lt;br /&gt;
 - so you can&#039;t access it, unless&lt;br /&gt;
&lt;br /&gt;
To build an object, you have to call the object&#039;s&lt;br /&gt;
constructor&lt;br /&gt;
 - why not replace the constructor with our own method?&lt;br /&gt;
   e.g., override Array&lt;br /&gt;
 - this is solved in regular browsers for JSON by&lt;br /&gt;
   only using a clean JavaScript environment for&lt;br /&gt;
   JSON.parse()&lt;br /&gt;
&lt;br /&gt;
This doesn&#039;t help you for code, but who cares about&lt;br /&gt;
code, right?&lt;br /&gt;
 - many websites dynamically generate JavaScript and put&lt;br /&gt;
   personal information into it&lt;br /&gt;
&lt;br /&gt;
Solution?  Don&#039;t mix code and data!&lt;br /&gt;
&lt;br /&gt;
WebAssembly&lt;br /&gt;
-----------&lt;br /&gt;
&lt;br /&gt;
* Many, many people hate JavaScript&lt;br /&gt;
* Lots of code that isn&#039;t JavaScript&lt;br /&gt;
* But people want everything to run on the web&lt;br /&gt;
  in a browser&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Past efforts at having alternative languages all&lt;br /&gt;
had a basic problem&lt;br /&gt;
 - they couldn&#039;t see the DOM&lt;br /&gt;
 - e.g., Java applets, Flash, Silverlight&lt;br /&gt;
&lt;br /&gt;
And, what about all those other languages?&lt;br /&gt;
 - C, C++??!&lt;br /&gt;
 - how about games?&lt;br /&gt;
&lt;br /&gt;
Unreal runs in a web page&lt;br /&gt;
 - there&#039;s WebGL which is pretty fast&lt;br /&gt;
&lt;br /&gt;
So, why can&#039;t we compile code and have it run in the&lt;br /&gt;
browser?&lt;br /&gt;
&lt;br /&gt;
Two old solutions:&lt;br /&gt;
 - NaCl (Google)&lt;br /&gt;
 - asm.js (Mozilla)&lt;br /&gt;
 &lt;br /&gt;
They joined up and are now making WebAssembly&lt;br /&gt;
&lt;br /&gt;
This will be good for functionality and bad for&lt;br /&gt;
security.&lt;br /&gt;
 - security won&#039;t go well because code isn&#039;t&lt;br /&gt;
   designed for the web&lt;br /&gt;
 - but it will be cool to play games in a browser at&lt;br /&gt;
   full speed :-)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Administrative====&lt;br /&gt;
&lt;br /&gt;
*There is a study session on April 11 morning.&lt;br /&gt;
*Students can come to tutorial next week and consider it as extended office hours&lt;br /&gt;
&lt;br /&gt;
====Web Security====&lt;br /&gt;
&lt;br /&gt;
*There are certain fundamental things of the web that make it very hard to secure&lt;br /&gt;
*When considering security, we want to consider a threat model&lt;br /&gt;
**What attacks are you trying to defend against?&lt;br /&gt;
**What attacks are you NOT trying to defend against?&lt;br /&gt;
&lt;br /&gt;
=====Computer in an isolated room=====&lt;br /&gt;
&lt;br /&gt;
*Ignore physical attacks, e.g. sledgehammers&lt;br /&gt;
*If we talking about software level, how can the computer be compromised?&lt;br /&gt;
**Someone bad starts typing&lt;br /&gt;
**Someone inserts malicious media/device&lt;br /&gt;
**Parking lot flashdrive attack (a flashdrive is left in a parking lot for someone to pick up and plug in)&lt;br /&gt;
**(solution: epoxy, no one can pug anything in)&lt;br /&gt;
**Electromagnetic emissions&lt;br /&gt;
***(solution: live in a Faraday cage, almost completely isolated from any EM transmission)&lt;br /&gt;
&lt;br /&gt;
=====Computer on a network=====&lt;br /&gt;
&lt;br /&gt;
*Threats:&lt;br /&gt;
**All local attacks (all the physical attack will be there too)&lt;br /&gt;
**An attacker can send arbitrary network data to system&lt;br /&gt;
**An attacker can listen in on network traffic&lt;br /&gt;
**An attacker can change outgoing traffic&lt;br /&gt;
**(they can listen anything on the computer, can listen anything going out of the computer, can send arbitrary network data to the computer, can change data and leaving the computer going elsewhere)&lt;br /&gt;
*How to protect against that?&lt;br /&gt;
*Basic defense: network crypto&lt;br /&gt;
**Encrypt to hide, sign data to protect integrity, verify authenticity&lt;br /&gt;
*We need to distinguish between who is trying to access the computer&lt;br /&gt;
*Who will access?&lt;br /&gt;
**Authenticated/authorized users&lt;br /&gt;
**Unauthorized individuals/systems&lt;br /&gt;
**Attackers (can be either of the above)&lt;br /&gt;
*How can an attacker be “authorized” ?&lt;br /&gt;
**Social engineering (can you please reset my password?)&lt;br /&gt;
**Brute force (try all the passwords)&lt;br /&gt;
**Technical compromise of credentials (keylogger)&lt;br /&gt;
**Authorized user turns/is malicious (insider attack)(most dangerous, because they are not only authorized, they also know the system)&lt;br /&gt;
&lt;br /&gt;
=====And now the web=====&lt;br /&gt;
&lt;br /&gt;
*Why is the web so nasty?&lt;br /&gt;
*Who can access your application?&lt;br /&gt;
**ANYBODY&lt;br /&gt;
**For most public web apps, ANYBODY is an authorized user&lt;br /&gt;
**There are different levels of insider attacks. Just because of you have account on gmail doesn’t mean you have access to the inside system. You don’t have full access, but you have some access, that is potentially dangerous.&lt;br /&gt;
*So from this we can see that web security is going to be hard&lt;br /&gt;
*The other half of the nightmare is the browser&lt;br /&gt;
*Ideally, to secure systems you isolate components&lt;br /&gt;
**Build big walls&lt;br /&gt;
*Your web browser has walls between pages... but there are BIG HOLES (on purpose)&lt;br /&gt;
*The tabs in the browser are not so isolated (e.g Log in to an account on one tab then refresh another tab on the same page... it will be logged in as well). They are connected.&lt;br /&gt;
*Cookies are being shared between the pages, it is a shared data source&lt;br /&gt;
*This is bad because the scope of cookies is determined by DNS (Domain Name System)&lt;br /&gt;
*DNS has almost no security&lt;br /&gt;
*There is DNSSEC but nobody uses it, yet at least&lt;br /&gt;
**If you want to know more about this, you can find the article on [usenix.org usenix.org]&lt;br /&gt;
*You can better protect cookies by using HSTS&lt;br /&gt;
**This forces HTTPS everywhere on a page/domain&lt;br /&gt;
**Protects against mixes of HTTP/HTTPS communications and downgrade attacks&lt;br /&gt;
*Why we do we have cookies in the first place?&lt;br /&gt;
**Because HTTP was supposed to be stateless&lt;br /&gt;
**The cookies are used instead to create a state between the client and server&lt;br /&gt;
*If we could get past these problems, would we have proper security?&lt;br /&gt;
**Definitely not&lt;br /&gt;
*There are other issues...&lt;br /&gt;
&lt;br /&gt;
======Cross-Site Scripting (XSS) Attacks======&lt;br /&gt;
&lt;br /&gt;
*Not necessarily cross-site, and not necessarily scripting&lt;br /&gt;
*More like “web injection attacks”&lt;br /&gt;
*This can happen anywhere an attacker can inject content into a page&lt;br /&gt;
**Ads&lt;br /&gt;
**Comments&lt;br /&gt;
**Social media&lt;br /&gt;
**User-generated content&lt;br /&gt;
*Problems can occur when there is a failure to sanitize untrusted input that is inserted into a web page&lt;br /&gt;
**This allows users to insert arbitrary HTML, CSS, and JavaScript into the page&lt;br /&gt;
&lt;br /&gt;
======Cross-Site Request Forgery======&lt;br /&gt;
&lt;br /&gt;
*Cross-site scripting is more straight-forward, cross-site request forgery is a little bit weird&lt;br /&gt;
*This involves having a link from a page sent by one server cause a request to be sent off to another server&lt;br /&gt;
*If you are logged in at the other server, this can cause things to happen which you didn&#039;t expect or want to happen&lt;br /&gt;
**For example, while logged in to the course wiki, a link from a page on a different website may cause a page to be deleted from the wiki by sending an appropriate request&lt;br /&gt;
*This makes use of cookies automatically being sent to a server, no matter where the link came from&lt;br /&gt;
*How can we stop this?&lt;br /&gt;
**One way is to try to verify where the link came from&lt;br /&gt;
**In the browser tools, go to the network tab and check the referer header&lt;br /&gt;
***This header indicates where the request came from&lt;br /&gt;
**So in principle, to stop a cross-site request forgery, we can check this header in the requests received on the server&lt;br /&gt;
***If it came from one of our pages, then we can trust the request&lt;br /&gt;
***Otherwise we don’t&lt;br /&gt;
**That does not actually work. It kind of works, but not always&lt;br /&gt;
**Why? Because the Referer header can often be scrapped by a proxy&lt;br /&gt;
**An alternative solution is to randomize your links&lt;br /&gt;
**Instead of using a predictable url, stick a random string in the middle of it&lt;br /&gt;
***The string is connected to the client&#039;s cookie so that the attacker can’t guess it&lt;br /&gt;
&lt;br /&gt;
======Same-Origin Policy======&lt;br /&gt;
&lt;br /&gt;
*Data should only loaded from the originating server but, you can load almost anything else from anywhere&lt;br /&gt;
**Images&lt;br /&gt;
**Sound&lt;br /&gt;
**JavaScript&lt;br /&gt;
*So what about JSON?&lt;br /&gt;
**when you load it with AJAX, you have the same-origin policy&lt;br /&gt;
**But what if you just treated it as a script? (why not just load it in the page?)&lt;br /&gt;
**And what if you were logged in?&lt;br /&gt;
***Consider a bank sending data as JSON (Could I from another tab just grab your bank account data as a JSON data in the background and see what exactly what you have? It doesn’t work.)&lt;br /&gt;
*JSON files are valid JavaScript structure (JSON is a subset of JavaScript)&lt;br /&gt;
*JSON files do not specify a name to give to the data structure so you can’t access it by name&lt;br /&gt;
*However, to build the object in the first place, its constructor must be called&lt;br /&gt;
**Why not replace the constructor with our own method?&lt;br /&gt;
***e.g., override Array&lt;br /&gt;
**This is solved in regular browsers for JSON by only using a clean JavaScript environment for JSON.parse()&lt;br /&gt;
**But what about code?&lt;br /&gt;
***This doesn’t help you for code, but who cares about code, right?&lt;br /&gt;
**Turns out many websites dynamically generate JavaScript and put personal information into it (different people have different scripts)&lt;br /&gt;
***Solution? Don’t mix code and data! (treat the JSON separately, let the code just be loaded on the system, don’t mix it with anything else)&lt;br /&gt;
&lt;br /&gt;
====Web Assembly====&lt;br /&gt;
&lt;br /&gt;
*Many, many people hate JavaScript&lt;br /&gt;
**There is a lot of code that isn’t JavaScript but people want everything to run on the web in a browser (JavaScript is the language of the web)&lt;br /&gt;
*Past efforts at having alternative languages all had a basic problem&lt;br /&gt;
**They couldn’t see the DOM&lt;br /&gt;
***e.g., Java applets, Flash, Silverlight&lt;br /&gt;
*And, what about all those other languages?&lt;br /&gt;
**C,C++?&lt;br /&gt;
**How about games?&lt;br /&gt;
***Unreal runs in a web page because there’s WebGL which is pretty fast (WebGL is a subset of openGL)&lt;br /&gt;
*So why can’t we compile code and have it run in the browser?&lt;br /&gt;
*Two old solutions:&lt;br /&gt;
**NaCl (Google)&lt;br /&gt;
***Native Client is a sandbox for running compiled C and C++ code in the browser efficiently and securely, independent of the user’s operating system&lt;br /&gt;
**asm.js (Mozilla)&lt;br /&gt;
***This uses a compiler that takes arbitrary programs and compiles them to JavaScript&lt;br /&gt;
***asm.js is perfectly legal JavaScript, it is tightly restricted JavaScript&lt;br /&gt;
***You cannot read asm.js code, it looks like binary&lt;br /&gt;
*Google and Mozilla joined up and are now making WebAssembly&lt;br /&gt;
**You can compile arbitrary code to run inside of the browser with WebAssembly&lt;br /&gt;
**This will be good for functionality and bad for security&lt;br /&gt;
***Security won’t go well because the code isn’t designed for the web&lt;br /&gt;
***But it will be cool to play games in a browser at full speed&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_22&amp;diff=20906</id>
		<title>WebFund 2016W Lecture 22</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_22&amp;diff=20906"/>
		<updated>2016-04-04T03:05:52Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: /* And now the web */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 31, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec22-31Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In Class===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 22&lt;br /&gt;
------------&lt;br /&gt;
Web Security&lt;br /&gt;
&lt;br /&gt;
Security in isolated systems&lt;br /&gt;
 - no networking&lt;br /&gt;
&lt;br /&gt;
What is the threat model?&lt;br /&gt;
&lt;br /&gt;
Threat model&lt;br /&gt;
 - what attacks are you trying to defend against&lt;br /&gt;
 - what attacks are you NOT trying to defend against&lt;br /&gt;
&lt;br /&gt;
Computer in an isolated room&lt;br /&gt;
 - ignore physical attacks, e.g. sledgehammers&lt;br /&gt;
&lt;br /&gt;
How to compromise this computer?&lt;br /&gt;
 - someone bad starts typing&lt;br /&gt;
 - someone inserts malicious media/device&lt;br /&gt;
   - parking lot flashdrive attack&lt;br /&gt;
     (solution: epoxy)&lt;br /&gt;
 - electromagnetic emissions&lt;br /&gt;
     (live in a faraday cage)&lt;br /&gt;
&lt;br /&gt;
Computer on a network&lt;br /&gt;
 - all local attacks&lt;br /&gt;
 - attacker can send arbitrary network data to system&lt;br /&gt;
 - attacker can listen in on network traffic&lt;br /&gt;
 - attacker can change outgoing traffic&lt;br /&gt;
&lt;br /&gt;
Basic defense: network crypto&lt;br /&gt;
 - encrypt to hide, sign data to protect integrity,&lt;br /&gt;
   verify authenticity&lt;br /&gt;
&lt;br /&gt;
Who will access?&lt;br /&gt;
 - authenticated, authorized users&lt;br /&gt;
 - unauthorized individuals/systems&lt;br /&gt;
 - attackers (can be either of the above)&lt;br /&gt;
&lt;br /&gt;
How can an attacker be &amp;quot;authorized&amp;quot;?&lt;br /&gt;
 - social engineering&lt;br /&gt;
   (can you please reset my password?)&lt;br /&gt;
 - brute force (try all the passwords)&lt;br /&gt;
 - technical compromise of credentials (keylogger)&lt;br /&gt;
 - authorized user turns/is malicious&lt;br /&gt;
   (insider attack)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And now the web&lt;br /&gt;
&lt;br /&gt;
Who can access your application?&lt;br /&gt;
 - ANYBODY&lt;br /&gt;
 - for public web apps, ANYBODY is an authorized user&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So just from this we can see that web security is going&lt;br /&gt;
to be hard.&lt;br /&gt;
&lt;br /&gt;
The other half of the nightmare: the browser&lt;br /&gt;
&lt;br /&gt;
Ideally, to secure systems you isolate components&lt;br /&gt;
  - build big walls&lt;br /&gt;
&lt;br /&gt;
Your web browser has walls between pages...but there are&lt;br /&gt;
BIG HOLES (on purpose)&lt;br /&gt;
&lt;br /&gt;
 - cookies, in particular, are shared&lt;br /&gt;
 - this is bad because the scope of cookies is&lt;br /&gt;
   determined by DNS (domain name system)&lt;br /&gt;
&lt;br /&gt;
DNS has almost no security&lt;br /&gt;
 - there is DNSSEC but nobody uses it, yet at least&lt;br /&gt;
&lt;br /&gt;
You can better protect cookies by using HSTS&lt;br /&gt;
 - force HTTPS everywhere on a page/domain&lt;br /&gt;
 - protects against mixes of http/https content and&lt;br /&gt;
   downgrade attacks&lt;br /&gt;
&lt;br /&gt;
Why do we have cookies in the first place?&lt;br /&gt;
 - because HTTP was supposed to be stateless&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Also...&lt;br /&gt;
&lt;br /&gt;
Cross-site Scripting (XSS) attacks&lt;br /&gt;
 - not necessarily cross-site, not necesarily scripting&lt;br /&gt;
 - &amp;quot;web injection attacks&amp;quot;&lt;br /&gt;
 - see anywhere attacker can inject content into a page&lt;br /&gt;
   - (ads)&lt;br /&gt;
   - comments&lt;br /&gt;
   - social media&lt;br /&gt;
   - &amp;quot;user-generated content&amp;quot;&lt;br /&gt;
 - failure to sanitize untrusted input that is inserted&lt;br /&gt;
   into a web page&lt;br /&gt;
   (adding a comment to a page)&lt;br /&gt;
 - otherwise, you can insert arbitrary HTML, CSS,&lt;br /&gt;
   and JavaScript into the page&lt;br /&gt;
&lt;br /&gt;
Cross-site request forgery&lt;br /&gt;
 - makes use of cookies automatically being sent&lt;br /&gt;
   to domain, no matter where the link came from&lt;br /&gt;
 - can stop using tokens in URLs, or checking&lt;br /&gt;
   Referer/Origin headers (and a few other ways)&lt;br /&gt;
&lt;br /&gt;
Same-origin policy&lt;br /&gt;
 - data should only be loaded from the originating&lt;br /&gt;
   server&lt;br /&gt;
 - but, you can load almost anything else from&lt;br /&gt;
   anywhere&lt;br /&gt;
    - images&lt;br /&gt;
    - sound&lt;br /&gt;
    - javascript&lt;br /&gt;
&lt;br /&gt;
So what about JSON?&lt;br /&gt;
 - when you load it with AJAX, you have same origin&lt;br /&gt;
   policy&lt;br /&gt;
 - but if you just treated it as a script...&lt;br /&gt;
 - and what if you were logged in&lt;br /&gt;
   - bank sending data as JSON&lt;br /&gt;
&lt;br /&gt;
JSON files are valid JavaScript but they don&#039;t specify&lt;br /&gt;
the name to give to the data structure&lt;br /&gt;
 - so you can&#039;t access it, unless&lt;br /&gt;
&lt;br /&gt;
To build an object, you have to call the object&#039;s&lt;br /&gt;
constructor&lt;br /&gt;
 - why not replace the constructor with our own method?&lt;br /&gt;
   e.g., override Array&lt;br /&gt;
 - this is solved in regular browsers for JSON by&lt;br /&gt;
   only using a clean JavaScript environment for&lt;br /&gt;
   JSON.parse()&lt;br /&gt;
&lt;br /&gt;
This doesn&#039;t help you for code, but who cares about&lt;br /&gt;
code, right?&lt;br /&gt;
 - many websites dynamically generate JavaScript and put&lt;br /&gt;
   personal information into it&lt;br /&gt;
&lt;br /&gt;
Solution?  Don&#039;t mix code and data!&lt;br /&gt;
&lt;br /&gt;
WebAssembly&lt;br /&gt;
-----------&lt;br /&gt;
&lt;br /&gt;
* Many, many people hate JavaScript&lt;br /&gt;
* Lots of code that isn&#039;t JavaScript&lt;br /&gt;
* But people want everything to run on the web&lt;br /&gt;
  in a browser&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Past efforts at having alternative languages all&lt;br /&gt;
had a basic problem&lt;br /&gt;
 - they couldn&#039;t see the DOM&lt;br /&gt;
 - e.g., Java applets, Flash, Silverlight&lt;br /&gt;
&lt;br /&gt;
And, what about all those other languages?&lt;br /&gt;
 - C, C++??!&lt;br /&gt;
 - how about games?&lt;br /&gt;
&lt;br /&gt;
Unreal runs in a web page&lt;br /&gt;
 - there&#039;s WebGL which is pretty fast&lt;br /&gt;
&lt;br /&gt;
So, why can&#039;t we compile code and have it run in the&lt;br /&gt;
browser?&lt;br /&gt;
&lt;br /&gt;
Two old solutions:&lt;br /&gt;
 - NaCl (Google)&lt;br /&gt;
 - asm.js (Mozilla)&lt;br /&gt;
 &lt;br /&gt;
They joined up and are now making WebAssembly&lt;br /&gt;
&lt;br /&gt;
This will be good for functionality and bad for&lt;br /&gt;
security.&lt;br /&gt;
 - security won&#039;t go well because code isn&#039;t&lt;br /&gt;
   designed for the web&lt;br /&gt;
 - but it will be cool to play games in a browser at&lt;br /&gt;
   full speed :-)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Administrative====&lt;br /&gt;
&lt;br /&gt;
*There is a study session on April 11 morning.&lt;br /&gt;
*Students can come to tutorial next week and consider it as extended office hours&lt;br /&gt;
&lt;br /&gt;
====Web Security====&lt;br /&gt;
&lt;br /&gt;
*There are certain fundamental things of the web that make it very hard to secure&lt;br /&gt;
*When considering security, we want to consider a threat model&lt;br /&gt;
**What attacks are you trying to defend against?&lt;br /&gt;
**What attacks are you NOT trying to defend against?&lt;br /&gt;
&lt;br /&gt;
=====Computer in an isolated room=====&lt;br /&gt;
&lt;br /&gt;
*Ignore physical attacks, e.g. sledgehammers&lt;br /&gt;
*If we talking about software level, how can the computer be compromised?&lt;br /&gt;
**Someone bad starts typing&lt;br /&gt;
**Someone inserts malicious media/device&lt;br /&gt;
**Parking lot flashdrive attack (a flashdrive is left in a parking lot for someone to pick up and plug in)&lt;br /&gt;
**(solution: epoxy, no one can pug anything in)&lt;br /&gt;
**Electromagnetic emissions&lt;br /&gt;
***(solution: live in a Faraday cage, almost completely isolated from any EM transmission)&lt;br /&gt;
&lt;br /&gt;
=====Computer on a network=====&lt;br /&gt;
&lt;br /&gt;
*Threats:&lt;br /&gt;
**All local attacks (all the physical attack will be there too)&lt;br /&gt;
**An attacker can send arbitrary network data to system&lt;br /&gt;
**An attacker can listen in on network traffic&lt;br /&gt;
**An attacker can change outgoing traffic&lt;br /&gt;
**(they can listen anything on the computer, can listen anything going out of the computer, can send arbitrary network data to the computer, can change data and leaving the computer going elsewhere)&lt;br /&gt;
*How to protect against that?&lt;br /&gt;
*Basic defense: network crypto&lt;br /&gt;
**Encrypt to hide, sign data to protect integrity, verify authenticity&lt;br /&gt;
*We need to distinguish between who is trying to access the computer&lt;br /&gt;
*Who will access?&lt;br /&gt;
**Authenticated/authorized users&lt;br /&gt;
**Unauthorized individuals/systems&lt;br /&gt;
**Attackers (can be either of the above)&lt;br /&gt;
*How can an attacker be “authorized” ?&lt;br /&gt;
**Social engineering (can you please reset my password?)&lt;br /&gt;
**Brute force (try all the passwords)&lt;br /&gt;
**Technical compromise of credentials (keylogger)&lt;br /&gt;
**Authorized user turns/is malicious (insider attack)(most dangerous, because they are not only authorized, they also know the system)&lt;br /&gt;
&lt;br /&gt;
=====And now the web=====&lt;br /&gt;
&lt;br /&gt;
*Why is the web so nasty?&lt;br /&gt;
*Who can access your application?&lt;br /&gt;
**ANYBODY&lt;br /&gt;
**For most public web apps, ANYBODY is an authorized user&lt;br /&gt;
**There are different levels of insider attacks. Just because of you have account on gmail doesn’t mean you have access to the inside system. You don’t have full access, but you have some access, that is potentially dangerous.&lt;br /&gt;
*So from this we can see that web security is going to be hard&lt;br /&gt;
*The other half of the nightmare is the browser&lt;br /&gt;
*Ideally, to secure systems you isolate components&lt;br /&gt;
**Build big walls&lt;br /&gt;
*Your web browser has walls between pages... but there are BIG HOLES (on purpose)&lt;br /&gt;
*The tabs in the browser are not so isolated (e.g Log in to an account on one tab then refresh another tab on the same page... it will be logged in as well). They are connected.&lt;br /&gt;
*Cookies are being shared between the pages, it is a shared data source&lt;br /&gt;
*This is bad because the scope of cookies is determined by DNS (Domain Name System)&lt;br /&gt;
*DNS has almost no security&lt;br /&gt;
*There is DNSSEC but nobody uses it, yet at least&lt;br /&gt;
**If you want to know more about this, you can find the article on [usenix.org usenix.org]&lt;br /&gt;
*You can better protect cookies by using HSTS&lt;br /&gt;
**This forces HTTPS everywhere on a page/domain&lt;br /&gt;
**Protects against mixes of HTTP/HTTPS communications and downgrade attacks&lt;br /&gt;
*Why we do we have cookies in the first place?&lt;br /&gt;
**Because HTTP was supposed to be stateless&lt;br /&gt;
**The cookies are used instead to create a state between the client and server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If we could get past these problems, would we have proper security?&lt;br /&gt;
**Definitely not&lt;br /&gt;
*There are other issues...&lt;br /&gt;
&lt;br /&gt;
======Cross-Site Scripting (XSS) Attacks======&lt;br /&gt;
&lt;br /&gt;
*Not necessarily cross-site, and not necessarily scripting&lt;br /&gt;
*More like “web injection attacks”&lt;br /&gt;
*This can happen anywhere an attacker can inject content into a page&lt;br /&gt;
**Ads&lt;br /&gt;
**Comments&lt;br /&gt;
**Social media&lt;br /&gt;
**User-generated content&lt;br /&gt;
*Problems can occur when there is a failure to sanitize untrusted input that is inserted into a web page&lt;br /&gt;
**This allows users to insert arbitrary HTML, CSS, and JavaScript into the page&lt;br /&gt;
&lt;br /&gt;
======Cross-Site Request Forgery======&lt;br /&gt;
&lt;br /&gt;
*Cross-site scripting is more straight-forward, cross-site request forgery is a little bit weird&lt;br /&gt;
*This involves having a link from a page sent by one server cause a request to be sent off to another server&lt;br /&gt;
*If you are logged in at the other server, this can cause things to happen which you didn&#039;t expect or want to happen&lt;br /&gt;
**For example, while logged in to the course wiki, a link from a page on a different website may cause a page to be deleted from the wiki by sending an appropriate request&lt;br /&gt;
*This makes use of cookies automatically being sent to a server, no matter where the link came from&lt;br /&gt;
*How can we stop this?&lt;br /&gt;
**One way is to try to verify where the link came from&lt;br /&gt;
**In the browser tools, go to the network tab and check the referer header&lt;br /&gt;
***This header indicates where the request came from&lt;br /&gt;
**So in principle, to stop a cross-site request forgery, we can check this header in the requests received on the server&lt;br /&gt;
***If it came from one of our pages, then we can trust the request&lt;br /&gt;
***Otherwise we don’t&lt;br /&gt;
**That does not actually work. It kind of works, but not always&lt;br /&gt;
**Why? Because the Referer header can often be scrapped by a proxy&lt;br /&gt;
**An alternative solution is to randomize your links&lt;br /&gt;
**Instead of using a predictable url, stick a random string in the middle of it&lt;br /&gt;
***The string is connected to the client&#039;s cookie so that the attacker can’t guess it&lt;br /&gt;
&lt;br /&gt;
======Same-Origin Policy======&lt;br /&gt;
&lt;br /&gt;
*Data should only loaded from the originating server but, you can load almost anything else from anywhere&lt;br /&gt;
**Images&lt;br /&gt;
**Sound&lt;br /&gt;
**JavaScript&lt;br /&gt;
*So what about JSON?&lt;br /&gt;
**when you load it with AJAX, you have the same-origin policy&lt;br /&gt;
**But what if you just treated it as a script? (why not just load it in the page?)&lt;br /&gt;
**And what if you were logged in?&lt;br /&gt;
***Consider a bank sending data as JSON (Could I from another tab just grab your bank account data as a JSON data in the background and see what exactly what you have? It doesn’t work.)&lt;br /&gt;
*JSON files are valid JavaScript structure (JSON is a subset of JavaScript)&lt;br /&gt;
*JSON files do not specify a name to give to the data structure so you can’t access it by name&lt;br /&gt;
*However, to build the object in the first place, its constructor must be called&lt;br /&gt;
**Why not replace the constructor with our own method?&lt;br /&gt;
***e.g., override Array&lt;br /&gt;
**This is solved in regular browsers for JSON by only using a clean JavaScript environment for JSON.parse()&lt;br /&gt;
**But what about code?&lt;br /&gt;
***This doesn’t help you for code, but who cares about code, right?&lt;br /&gt;
**Turns out many websites dynamically generate JavaScript and put personal information into it (different people have different scripts)&lt;br /&gt;
***Solution? Don’t mix code and data! (treat the JSON separately, let the code just be loaded on the system, don’t mix it with anything else)&lt;br /&gt;
&lt;br /&gt;
====Web Assembly====&lt;br /&gt;
&lt;br /&gt;
*Many, many people hate JavaScript&lt;br /&gt;
**There is a lot of code that isn’t JavaScript but people want everything to run on the web in a browser (JavaScript is the language of the web)&lt;br /&gt;
*Past efforts at having alternative languages all had a basic problem&lt;br /&gt;
**They couldn’t see the DOM&lt;br /&gt;
***e.g., Java applets, Flash, Silverlight&lt;br /&gt;
*And, what about all those other languages?&lt;br /&gt;
**C,C++?&lt;br /&gt;
**How about games?&lt;br /&gt;
***Unreal runs in a web page because there’s WebGL which is pretty fast (WebGL is a subset of openGL)&lt;br /&gt;
*So why can’t we compile code and have it run in the browser?&lt;br /&gt;
*Two old solutions:&lt;br /&gt;
**NaCl (Google)&lt;br /&gt;
***Native Client is a sandbox for running compiled C and C++ code in the browser efficiently and securely, independent of the user’s operating system&lt;br /&gt;
**asm.js (Mozilla)&lt;br /&gt;
***This uses a compiler that takes arbitrary programs and compiles them to JavaScript&lt;br /&gt;
***asm.js is perfectly legal JavaScript, it is tightly restricted JavaScript&lt;br /&gt;
***You cannot read asm.js code, it looks like binary&lt;br /&gt;
*Google and Mozilla joined up and are now making WebAssembly&lt;br /&gt;
**You can compile arbitrary code to run inside of the browser with WebAssembly&lt;br /&gt;
**This will be good for functionality and bad for security&lt;br /&gt;
***Security won’t go well because the code isn’t designed for the web&lt;br /&gt;
***But it will be cool to play games in a browser at full speed&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_22&amp;diff=20905</id>
		<title>WebFund 2016W Lecture 22</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_22&amp;diff=20905"/>
		<updated>2016-04-04T03:05:26Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: /* Computer in an isolated room */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 31, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec22-31Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In Class===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 22&lt;br /&gt;
------------&lt;br /&gt;
Web Security&lt;br /&gt;
&lt;br /&gt;
Security in isolated systems&lt;br /&gt;
 - no networking&lt;br /&gt;
&lt;br /&gt;
What is the threat model?&lt;br /&gt;
&lt;br /&gt;
Threat model&lt;br /&gt;
 - what attacks are you trying to defend against&lt;br /&gt;
 - what attacks are you NOT trying to defend against&lt;br /&gt;
&lt;br /&gt;
Computer in an isolated room&lt;br /&gt;
 - ignore physical attacks, e.g. sledgehammers&lt;br /&gt;
&lt;br /&gt;
How to compromise this computer?&lt;br /&gt;
 - someone bad starts typing&lt;br /&gt;
 - someone inserts malicious media/device&lt;br /&gt;
   - parking lot flashdrive attack&lt;br /&gt;
     (solution: epoxy)&lt;br /&gt;
 - electromagnetic emissions&lt;br /&gt;
     (live in a faraday cage)&lt;br /&gt;
&lt;br /&gt;
Computer on a network&lt;br /&gt;
 - all local attacks&lt;br /&gt;
 - attacker can send arbitrary network data to system&lt;br /&gt;
 - attacker can listen in on network traffic&lt;br /&gt;
 - attacker can change outgoing traffic&lt;br /&gt;
&lt;br /&gt;
Basic defense: network crypto&lt;br /&gt;
 - encrypt to hide, sign data to protect integrity,&lt;br /&gt;
   verify authenticity&lt;br /&gt;
&lt;br /&gt;
Who will access?&lt;br /&gt;
 - authenticated, authorized users&lt;br /&gt;
 - unauthorized individuals/systems&lt;br /&gt;
 - attackers (can be either of the above)&lt;br /&gt;
&lt;br /&gt;
How can an attacker be &amp;quot;authorized&amp;quot;?&lt;br /&gt;
 - social engineering&lt;br /&gt;
   (can you please reset my password?)&lt;br /&gt;
 - brute force (try all the passwords)&lt;br /&gt;
 - technical compromise of credentials (keylogger)&lt;br /&gt;
 - authorized user turns/is malicious&lt;br /&gt;
   (insider attack)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And now the web&lt;br /&gt;
&lt;br /&gt;
Who can access your application?&lt;br /&gt;
 - ANYBODY&lt;br /&gt;
 - for public web apps, ANYBODY is an authorized user&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So just from this we can see that web security is going&lt;br /&gt;
to be hard.&lt;br /&gt;
&lt;br /&gt;
The other half of the nightmare: the browser&lt;br /&gt;
&lt;br /&gt;
Ideally, to secure systems you isolate components&lt;br /&gt;
  - build big walls&lt;br /&gt;
&lt;br /&gt;
Your web browser has walls between pages...but there are&lt;br /&gt;
BIG HOLES (on purpose)&lt;br /&gt;
&lt;br /&gt;
 - cookies, in particular, are shared&lt;br /&gt;
 - this is bad because the scope of cookies is&lt;br /&gt;
   determined by DNS (domain name system)&lt;br /&gt;
&lt;br /&gt;
DNS has almost no security&lt;br /&gt;
 - there is DNSSEC but nobody uses it, yet at least&lt;br /&gt;
&lt;br /&gt;
You can better protect cookies by using HSTS&lt;br /&gt;
 - force HTTPS everywhere on a page/domain&lt;br /&gt;
 - protects against mixes of http/https content and&lt;br /&gt;
   downgrade attacks&lt;br /&gt;
&lt;br /&gt;
Why do we have cookies in the first place?&lt;br /&gt;
 - because HTTP was supposed to be stateless&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Also...&lt;br /&gt;
&lt;br /&gt;
Cross-site Scripting (XSS) attacks&lt;br /&gt;
 - not necessarily cross-site, not necesarily scripting&lt;br /&gt;
 - &amp;quot;web injection attacks&amp;quot;&lt;br /&gt;
 - see anywhere attacker can inject content into a page&lt;br /&gt;
   - (ads)&lt;br /&gt;
   - comments&lt;br /&gt;
   - social media&lt;br /&gt;
   - &amp;quot;user-generated content&amp;quot;&lt;br /&gt;
 - failure to sanitize untrusted input that is inserted&lt;br /&gt;
   into a web page&lt;br /&gt;
   (adding a comment to a page)&lt;br /&gt;
 - otherwise, you can insert arbitrary HTML, CSS,&lt;br /&gt;
   and JavaScript into the page&lt;br /&gt;
&lt;br /&gt;
Cross-site request forgery&lt;br /&gt;
 - makes use of cookies automatically being sent&lt;br /&gt;
   to domain, no matter where the link came from&lt;br /&gt;
 - can stop using tokens in URLs, or checking&lt;br /&gt;
   Referer/Origin headers (and a few other ways)&lt;br /&gt;
&lt;br /&gt;
Same-origin policy&lt;br /&gt;
 - data should only be loaded from the originating&lt;br /&gt;
   server&lt;br /&gt;
 - but, you can load almost anything else from&lt;br /&gt;
   anywhere&lt;br /&gt;
    - images&lt;br /&gt;
    - sound&lt;br /&gt;
    - javascript&lt;br /&gt;
&lt;br /&gt;
So what about JSON?&lt;br /&gt;
 - when you load it with AJAX, you have same origin&lt;br /&gt;
   policy&lt;br /&gt;
 - but if you just treated it as a script...&lt;br /&gt;
 - and what if you were logged in&lt;br /&gt;
   - bank sending data as JSON&lt;br /&gt;
&lt;br /&gt;
JSON files are valid JavaScript but they don&#039;t specify&lt;br /&gt;
the name to give to the data structure&lt;br /&gt;
 - so you can&#039;t access it, unless&lt;br /&gt;
&lt;br /&gt;
To build an object, you have to call the object&#039;s&lt;br /&gt;
constructor&lt;br /&gt;
 - why not replace the constructor with our own method?&lt;br /&gt;
   e.g., override Array&lt;br /&gt;
 - this is solved in regular browsers for JSON by&lt;br /&gt;
   only using a clean JavaScript environment for&lt;br /&gt;
   JSON.parse()&lt;br /&gt;
&lt;br /&gt;
This doesn&#039;t help you for code, but who cares about&lt;br /&gt;
code, right?&lt;br /&gt;
 - many websites dynamically generate JavaScript and put&lt;br /&gt;
   personal information into it&lt;br /&gt;
&lt;br /&gt;
Solution?  Don&#039;t mix code and data!&lt;br /&gt;
&lt;br /&gt;
WebAssembly&lt;br /&gt;
-----------&lt;br /&gt;
&lt;br /&gt;
* Many, many people hate JavaScript&lt;br /&gt;
* Lots of code that isn&#039;t JavaScript&lt;br /&gt;
* But people want everything to run on the web&lt;br /&gt;
  in a browser&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Past efforts at having alternative languages all&lt;br /&gt;
had a basic problem&lt;br /&gt;
 - they couldn&#039;t see the DOM&lt;br /&gt;
 - e.g., Java applets, Flash, Silverlight&lt;br /&gt;
&lt;br /&gt;
And, what about all those other languages?&lt;br /&gt;
 - C, C++??!&lt;br /&gt;
 - how about games?&lt;br /&gt;
&lt;br /&gt;
Unreal runs in a web page&lt;br /&gt;
 - there&#039;s WebGL which is pretty fast&lt;br /&gt;
&lt;br /&gt;
So, why can&#039;t we compile code and have it run in the&lt;br /&gt;
browser?&lt;br /&gt;
&lt;br /&gt;
Two old solutions:&lt;br /&gt;
 - NaCl (Google)&lt;br /&gt;
 - asm.js (Mozilla)&lt;br /&gt;
 &lt;br /&gt;
They joined up and are now making WebAssembly&lt;br /&gt;
&lt;br /&gt;
This will be good for functionality and bad for&lt;br /&gt;
security.&lt;br /&gt;
 - security won&#039;t go well because code isn&#039;t&lt;br /&gt;
   designed for the web&lt;br /&gt;
 - but it will be cool to play games in a browser at&lt;br /&gt;
   full speed :-)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Administrative====&lt;br /&gt;
&lt;br /&gt;
*There is a study session on April 11 morning.&lt;br /&gt;
*Students can come to tutorial next week and consider it as extended office hours&lt;br /&gt;
&lt;br /&gt;
====Web Security====&lt;br /&gt;
&lt;br /&gt;
*There are certain fundamental things of the web that make it very hard to secure&lt;br /&gt;
*When considering security, we want to consider a threat model&lt;br /&gt;
**What attacks are you trying to defend against?&lt;br /&gt;
**What attacks are you NOT trying to defend against?&lt;br /&gt;
&lt;br /&gt;
=====Computer in an isolated room=====&lt;br /&gt;
&lt;br /&gt;
*Ignore physical attacks, e.g. sledgehammers&lt;br /&gt;
*If we talking about software level, how can the computer be compromised?&lt;br /&gt;
**Someone bad starts typing&lt;br /&gt;
**Someone inserts malicious media/device&lt;br /&gt;
**Parking lot flashdrive attack (a flashdrive is left in a parking lot for someone to pick up and plug in)&lt;br /&gt;
**(solution: epoxy, no one can pug anything in)&lt;br /&gt;
**Electromagnetic emissions&lt;br /&gt;
***(solution: live in a Faraday cage, almost completely isolated from any EM transmission)&lt;br /&gt;
&lt;br /&gt;
=====Computer on a network=====&lt;br /&gt;
&lt;br /&gt;
*Threats:&lt;br /&gt;
**All local attacks (all the physical attack will be there too)&lt;br /&gt;
**An attacker can send arbitrary network data to system&lt;br /&gt;
**An attacker can listen in on network traffic&lt;br /&gt;
**An attacker can change outgoing traffic&lt;br /&gt;
**(they can listen anything on the computer, can listen anything going out of the computer, can send arbitrary network data to the computer, can change data and leaving the computer going elsewhere)&lt;br /&gt;
*How to protect against that?&lt;br /&gt;
*Basic defense: network crypto&lt;br /&gt;
**Encrypt to hide, sign data to protect integrity, verify authenticity&lt;br /&gt;
*We need to distinguish between who is trying to access the computer&lt;br /&gt;
*Who will access?&lt;br /&gt;
**Authenticated/authorized users&lt;br /&gt;
**Unauthorized individuals/systems&lt;br /&gt;
**Attackers (can be either of the above)&lt;br /&gt;
*How can an attacker be “authorized” ?&lt;br /&gt;
**Social engineering (can you please reset my password?)&lt;br /&gt;
**Brute force (try all the passwords)&lt;br /&gt;
**Technical compromise of credentials (keylogger)&lt;br /&gt;
**Authorized user turns/is malicious (insider attack)(most dangerous, because they are not only authorized, they also know the system)&lt;br /&gt;
&lt;br /&gt;
=====And now the web=====&lt;br /&gt;
&lt;br /&gt;
*Why is the web so nasty?&lt;br /&gt;
*Who can access your application?&lt;br /&gt;
**ANYBODY&lt;br /&gt;
**For most public web apps, ANYBODY is an authorized user&lt;br /&gt;
**There are different levels of insider attacks. Just because of you have account on gmail doesn’t mean you have access to the inside system. You don’t have full access, but you have some access, that is potentially dangerous.&lt;br /&gt;
*So from this we can see that web security is going to be hard&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*The other half of the nightmare: the browser&lt;br /&gt;
*Ideally, to secure systems you isolate components&lt;br /&gt;
**Build big walls&lt;br /&gt;
*Your web browser has walls between pages... but there are BIG HOLES (on purpose)&lt;br /&gt;
*The tabs in the browser are not so isolated (e.g Log in to an account on one tab then refresh another tab on the same page... it will be logged in as well). They are connected.&lt;br /&gt;
*Cookies are being shared between the pages, it is a shared data source&lt;br /&gt;
*This is bad because the scope of cookies is determined by DNS (Domain Name System)&lt;br /&gt;
*DNS has almost no security&lt;br /&gt;
*There is DNSSEC but nobody uses it, yet at least&lt;br /&gt;
**If you want to know more about this, you can find the article on [usenix.org usenix.org]&lt;br /&gt;
*You can better protect cookies by using HSTS&lt;br /&gt;
**This forces HTTPS everywhere on a page/domain&lt;br /&gt;
**Protects against mixes of HTTP/HTTPS communications and downgrade attacks&lt;br /&gt;
*Why we do we have cookies in the first place?&lt;br /&gt;
**Because HTTP was supposed to be stateless&lt;br /&gt;
**The cookies are used instead to create a state between the client and server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If we could get past these problems, would we have proper security?&lt;br /&gt;
**Definitely not&lt;br /&gt;
*There are other issues...&lt;br /&gt;
&lt;br /&gt;
======Cross-Site Scripting (XSS) Attacks======&lt;br /&gt;
&lt;br /&gt;
*Not necessarily cross-site, and not necessarily scripting&lt;br /&gt;
*More like “web injection attacks”&lt;br /&gt;
*This can happen anywhere an attacker can inject content into a page&lt;br /&gt;
**Ads&lt;br /&gt;
**Comments&lt;br /&gt;
**Social media&lt;br /&gt;
**User-generated content&lt;br /&gt;
*Problems can occur when there is a failure to sanitize untrusted input that is inserted into a web page&lt;br /&gt;
**This allows users to insert arbitrary HTML, CSS, and JavaScript into the page&lt;br /&gt;
&lt;br /&gt;
======Cross-Site Request Forgery======&lt;br /&gt;
&lt;br /&gt;
*Cross-site scripting is more straight-forward, cross-site request forgery is a little bit weird&lt;br /&gt;
*This involves having a link from a page sent by one server cause a request to be sent off to another server&lt;br /&gt;
*If you are logged in at the other server, this can cause things to happen which you didn&#039;t expect or want to happen&lt;br /&gt;
**For example, while logged in to the course wiki, a link from a page on a different website may cause a page to be deleted from the wiki by sending an appropriate request&lt;br /&gt;
*This makes use of cookies automatically being sent to a server, no matter where the link came from&lt;br /&gt;
*How can we stop this?&lt;br /&gt;
**One way is to try to verify where the link came from&lt;br /&gt;
**In the browser tools, go to the network tab and check the referer header&lt;br /&gt;
***This header indicates where the request came from&lt;br /&gt;
**So in principle, to stop a cross-site request forgery, we can check this header in the requests received on the server&lt;br /&gt;
***If it came from one of our pages, then we can trust the request&lt;br /&gt;
***Otherwise we don’t&lt;br /&gt;
**That does not actually work. It kind of works, but not always&lt;br /&gt;
**Why? Because the Referer header can often be scrapped by a proxy&lt;br /&gt;
**An alternative solution is to randomize your links&lt;br /&gt;
**Instead of using a predictable url, stick a random string in the middle of it&lt;br /&gt;
***The string is connected to the client&#039;s cookie so that the attacker can’t guess it&lt;br /&gt;
&lt;br /&gt;
======Same-Origin Policy======&lt;br /&gt;
&lt;br /&gt;
*Data should only loaded from the originating server but, you can load almost anything else from anywhere&lt;br /&gt;
**Images&lt;br /&gt;
**Sound&lt;br /&gt;
**JavaScript&lt;br /&gt;
*So what about JSON?&lt;br /&gt;
**when you load it with AJAX, you have the same-origin policy&lt;br /&gt;
**But what if you just treated it as a script? (why not just load it in the page?)&lt;br /&gt;
**And what if you were logged in?&lt;br /&gt;
***Consider a bank sending data as JSON (Could I from another tab just grab your bank account data as a JSON data in the background and see what exactly what you have? It doesn’t work.)&lt;br /&gt;
*JSON files are valid JavaScript structure (JSON is a subset of JavaScript)&lt;br /&gt;
*JSON files do not specify a name to give to the data structure so you can’t access it by name&lt;br /&gt;
*However, to build the object in the first place, its constructor must be called&lt;br /&gt;
**Why not replace the constructor with our own method?&lt;br /&gt;
***e.g., override Array&lt;br /&gt;
**This is solved in regular browsers for JSON by only using a clean JavaScript environment for JSON.parse()&lt;br /&gt;
**But what about code?&lt;br /&gt;
***This doesn’t help you for code, but who cares about code, right?&lt;br /&gt;
**Turns out many websites dynamically generate JavaScript and put personal information into it (different people have different scripts)&lt;br /&gt;
***Solution? Don’t mix code and data! (treat the JSON separately, let the code just be loaded on the system, don’t mix it with anything else)&lt;br /&gt;
&lt;br /&gt;
====Web Assembly====&lt;br /&gt;
&lt;br /&gt;
*Many, many people hate JavaScript&lt;br /&gt;
**There is a lot of code that isn’t JavaScript but people want everything to run on the web in a browser (JavaScript is the language of the web)&lt;br /&gt;
*Past efforts at having alternative languages all had a basic problem&lt;br /&gt;
**They couldn’t see the DOM&lt;br /&gt;
***e.g., Java applets, Flash, Silverlight&lt;br /&gt;
*And, what about all those other languages?&lt;br /&gt;
**C,C++?&lt;br /&gt;
**How about games?&lt;br /&gt;
***Unreal runs in a web page because there’s WebGL which is pretty fast (WebGL is a subset of openGL)&lt;br /&gt;
*So why can’t we compile code and have it run in the browser?&lt;br /&gt;
*Two old solutions:&lt;br /&gt;
**NaCl (Google)&lt;br /&gt;
***Native Client is a sandbox for running compiled C and C++ code in the browser efficiently and securely, independent of the user’s operating system&lt;br /&gt;
**asm.js (Mozilla)&lt;br /&gt;
***This uses a compiler that takes arbitrary programs and compiles them to JavaScript&lt;br /&gt;
***asm.js is perfectly legal JavaScript, it is tightly restricted JavaScript&lt;br /&gt;
***You cannot read asm.js code, it looks like binary&lt;br /&gt;
*Google and Mozilla joined up and are now making WebAssembly&lt;br /&gt;
**You can compile arbitrary code to run inside of the browser with WebAssembly&lt;br /&gt;
**This will be good for functionality and bad for security&lt;br /&gt;
***Security won’t go well because the code isn’t designed for the web&lt;br /&gt;
***But it will be cool to play games in a browser at full speed&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_22&amp;diff=20904</id>
		<title>WebFund 2016W Lecture 22</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_22&amp;diff=20904"/>
		<updated>2016-04-04T03:05:01Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 31, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec22-31Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In Class===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 22&lt;br /&gt;
------------&lt;br /&gt;
Web Security&lt;br /&gt;
&lt;br /&gt;
Security in isolated systems&lt;br /&gt;
 - no networking&lt;br /&gt;
&lt;br /&gt;
What is the threat model?&lt;br /&gt;
&lt;br /&gt;
Threat model&lt;br /&gt;
 - what attacks are you trying to defend against&lt;br /&gt;
 - what attacks are you NOT trying to defend against&lt;br /&gt;
&lt;br /&gt;
Computer in an isolated room&lt;br /&gt;
 - ignore physical attacks, e.g. sledgehammers&lt;br /&gt;
&lt;br /&gt;
How to compromise this computer?&lt;br /&gt;
 - someone bad starts typing&lt;br /&gt;
 - someone inserts malicious media/device&lt;br /&gt;
   - parking lot flashdrive attack&lt;br /&gt;
     (solution: epoxy)&lt;br /&gt;
 - electromagnetic emissions&lt;br /&gt;
     (live in a faraday cage)&lt;br /&gt;
&lt;br /&gt;
Computer on a network&lt;br /&gt;
 - all local attacks&lt;br /&gt;
 - attacker can send arbitrary network data to system&lt;br /&gt;
 - attacker can listen in on network traffic&lt;br /&gt;
 - attacker can change outgoing traffic&lt;br /&gt;
&lt;br /&gt;
Basic defense: network crypto&lt;br /&gt;
 - encrypt to hide, sign data to protect integrity,&lt;br /&gt;
   verify authenticity&lt;br /&gt;
&lt;br /&gt;
Who will access?&lt;br /&gt;
 - authenticated, authorized users&lt;br /&gt;
 - unauthorized individuals/systems&lt;br /&gt;
 - attackers (can be either of the above)&lt;br /&gt;
&lt;br /&gt;
How can an attacker be &amp;quot;authorized&amp;quot;?&lt;br /&gt;
 - social engineering&lt;br /&gt;
   (can you please reset my password?)&lt;br /&gt;
 - brute force (try all the passwords)&lt;br /&gt;
 - technical compromise of credentials (keylogger)&lt;br /&gt;
 - authorized user turns/is malicious&lt;br /&gt;
   (insider attack)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And now the web&lt;br /&gt;
&lt;br /&gt;
Who can access your application?&lt;br /&gt;
 - ANYBODY&lt;br /&gt;
 - for public web apps, ANYBODY is an authorized user&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So just from this we can see that web security is going&lt;br /&gt;
to be hard.&lt;br /&gt;
&lt;br /&gt;
The other half of the nightmare: the browser&lt;br /&gt;
&lt;br /&gt;
Ideally, to secure systems you isolate components&lt;br /&gt;
  - build big walls&lt;br /&gt;
&lt;br /&gt;
Your web browser has walls between pages...but there are&lt;br /&gt;
BIG HOLES (on purpose)&lt;br /&gt;
&lt;br /&gt;
 - cookies, in particular, are shared&lt;br /&gt;
 - this is bad because the scope of cookies is&lt;br /&gt;
   determined by DNS (domain name system)&lt;br /&gt;
&lt;br /&gt;
DNS has almost no security&lt;br /&gt;
 - there is DNSSEC but nobody uses it, yet at least&lt;br /&gt;
&lt;br /&gt;
You can better protect cookies by using HSTS&lt;br /&gt;
 - force HTTPS everywhere on a page/domain&lt;br /&gt;
 - protects against mixes of http/https content and&lt;br /&gt;
   downgrade attacks&lt;br /&gt;
&lt;br /&gt;
Why do we have cookies in the first place?&lt;br /&gt;
 - because HTTP was supposed to be stateless&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Also...&lt;br /&gt;
&lt;br /&gt;
Cross-site Scripting (XSS) attacks&lt;br /&gt;
 - not necessarily cross-site, not necesarily scripting&lt;br /&gt;
 - &amp;quot;web injection attacks&amp;quot;&lt;br /&gt;
 - see anywhere attacker can inject content into a page&lt;br /&gt;
   - (ads)&lt;br /&gt;
   - comments&lt;br /&gt;
   - social media&lt;br /&gt;
   - &amp;quot;user-generated content&amp;quot;&lt;br /&gt;
 - failure to sanitize untrusted input that is inserted&lt;br /&gt;
   into a web page&lt;br /&gt;
   (adding a comment to a page)&lt;br /&gt;
 - otherwise, you can insert arbitrary HTML, CSS,&lt;br /&gt;
   and JavaScript into the page&lt;br /&gt;
&lt;br /&gt;
Cross-site request forgery&lt;br /&gt;
 - makes use of cookies automatically being sent&lt;br /&gt;
   to domain, no matter where the link came from&lt;br /&gt;
 - can stop using tokens in URLs, or checking&lt;br /&gt;
   Referer/Origin headers (and a few other ways)&lt;br /&gt;
&lt;br /&gt;
Same-origin policy&lt;br /&gt;
 - data should only be loaded from the originating&lt;br /&gt;
   server&lt;br /&gt;
 - but, you can load almost anything else from&lt;br /&gt;
   anywhere&lt;br /&gt;
    - images&lt;br /&gt;
    - sound&lt;br /&gt;
    - javascript&lt;br /&gt;
&lt;br /&gt;
So what about JSON?&lt;br /&gt;
 - when you load it with AJAX, you have same origin&lt;br /&gt;
   policy&lt;br /&gt;
 - but if you just treated it as a script...&lt;br /&gt;
 - and what if you were logged in&lt;br /&gt;
   - bank sending data as JSON&lt;br /&gt;
&lt;br /&gt;
JSON files are valid JavaScript but they don&#039;t specify&lt;br /&gt;
the name to give to the data structure&lt;br /&gt;
 - so you can&#039;t access it, unless&lt;br /&gt;
&lt;br /&gt;
To build an object, you have to call the object&#039;s&lt;br /&gt;
constructor&lt;br /&gt;
 - why not replace the constructor with our own method?&lt;br /&gt;
   e.g., override Array&lt;br /&gt;
 - this is solved in regular browsers for JSON by&lt;br /&gt;
   only using a clean JavaScript environment for&lt;br /&gt;
   JSON.parse()&lt;br /&gt;
&lt;br /&gt;
This doesn&#039;t help you for code, but who cares about&lt;br /&gt;
code, right?&lt;br /&gt;
 - many websites dynamically generate JavaScript and put&lt;br /&gt;
   personal information into it&lt;br /&gt;
&lt;br /&gt;
Solution?  Don&#039;t mix code and data!&lt;br /&gt;
&lt;br /&gt;
WebAssembly&lt;br /&gt;
-----------&lt;br /&gt;
&lt;br /&gt;
* Many, many people hate JavaScript&lt;br /&gt;
* Lots of code that isn&#039;t JavaScript&lt;br /&gt;
* But people want everything to run on the web&lt;br /&gt;
  in a browser&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Past efforts at having alternative languages all&lt;br /&gt;
had a basic problem&lt;br /&gt;
 - they couldn&#039;t see the DOM&lt;br /&gt;
 - e.g., Java applets, Flash, Silverlight&lt;br /&gt;
&lt;br /&gt;
And, what about all those other languages?&lt;br /&gt;
 - C, C++??!&lt;br /&gt;
 - how about games?&lt;br /&gt;
&lt;br /&gt;
Unreal runs in a web page&lt;br /&gt;
 - there&#039;s WebGL which is pretty fast&lt;br /&gt;
&lt;br /&gt;
So, why can&#039;t we compile code and have it run in the&lt;br /&gt;
browser?&lt;br /&gt;
&lt;br /&gt;
Two old solutions:&lt;br /&gt;
 - NaCl (Google)&lt;br /&gt;
 - asm.js (Mozilla)&lt;br /&gt;
 &lt;br /&gt;
They joined up and are now making WebAssembly&lt;br /&gt;
&lt;br /&gt;
This will be good for functionality and bad for&lt;br /&gt;
security.&lt;br /&gt;
 - security won&#039;t go well because code isn&#039;t&lt;br /&gt;
   designed for the web&lt;br /&gt;
 - but it will be cool to play games in a browser at&lt;br /&gt;
   full speed :-)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Administrative====&lt;br /&gt;
&lt;br /&gt;
*There is a study session on April 11 morning.&lt;br /&gt;
*Students can come to tutorial next week and consider it as extended office hours&lt;br /&gt;
&lt;br /&gt;
====Web Security====&lt;br /&gt;
&lt;br /&gt;
*There are certain fundamental things of the web that make it very hard to secure&lt;br /&gt;
*When considering security, we want to consider a threat model&lt;br /&gt;
**What attacks are you trying to defend against?&lt;br /&gt;
**What attacks are you NOT trying to defend against?&lt;br /&gt;
&lt;br /&gt;
=====Computer in an isolated room=====&lt;br /&gt;
&lt;br /&gt;
*Ignore physical attacks, e.g. sledgehammers&lt;br /&gt;
*If we talking about software level, how can the computer be compromised?&lt;br /&gt;
**Someone bad starts typing&lt;br /&gt;
**Someone inserts malicious media/device&lt;br /&gt;
**Parking lot flashdrive attack (a flashdrive is left in a parking lot for someone to pick up and plug in)&lt;br /&gt;
**(solution: epoxy, no one can pug anything in)&lt;br /&gt;
**electromagnetic emissions&lt;br /&gt;
***(solution: live in a Faraday cage, almost completely isolated from any EM transmission)&lt;br /&gt;
&lt;br /&gt;
=====Computer on a network=====&lt;br /&gt;
&lt;br /&gt;
*Threats:&lt;br /&gt;
**All local attacks (all the physical attack will be there too)&lt;br /&gt;
**An attacker can send arbitrary network data to system&lt;br /&gt;
**An attacker can listen in on network traffic&lt;br /&gt;
**An attacker can change outgoing traffic&lt;br /&gt;
**(they can listen anything on the computer, can listen anything going out of the computer, can send arbitrary network data to the computer, can change data and leaving the computer going elsewhere)&lt;br /&gt;
*How to protect against that?&lt;br /&gt;
*Basic defense: network crypto&lt;br /&gt;
**Encrypt to hide, sign data to protect integrity, verify authenticity&lt;br /&gt;
*We need to distinguish between who is trying to access the computer&lt;br /&gt;
*Who will access?&lt;br /&gt;
**Authenticated/authorized users&lt;br /&gt;
**Unauthorized individuals/systems&lt;br /&gt;
**Attackers (can be either of the above)&lt;br /&gt;
*How can an attacker be “authorized” ?&lt;br /&gt;
**Social engineering (can you please reset my password?)&lt;br /&gt;
**Brute force (try all the passwords)&lt;br /&gt;
**Technical compromise of credentials (keylogger)&lt;br /&gt;
**Authorized user turns/is malicious (insider attack)(most dangerous, because they are not only authorized, they also know the system)&lt;br /&gt;
&lt;br /&gt;
=====And now the web=====&lt;br /&gt;
&lt;br /&gt;
*Why is the web so nasty?&lt;br /&gt;
*Who can access your application?&lt;br /&gt;
**ANYBODY&lt;br /&gt;
**For most public web apps, ANYBODY is an authorized user&lt;br /&gt;
**There are different levels of insider attacks. Just because of you have account on gmail doesn’t mean you have access to the inside system. You don’t have full access, but you have some access, that is potentially dangerous.&lt;br /&gt;
*So from this we can see that web security is going to be hard&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*The other half of the nightmare: the browser&lt;br /&gt;
*Ideally, to secure systems you isolate components&lt;br /&gt;
**Build big walls&lt;br /&gt;
*Your web browser has walls between pages... but there are BIG HOLES (on purpose)&lt;br /&gt;
*The tabs in the browser are not so isolated (e.g Log in to an account on one tab then refresh another tab on the same page... it will be logged in as well). They are connected.&lt;br /&gt;
*Cookies are being shared between the pages, it is a shared data source&lt;br /&gt;
*This is bad because the scope of cookies is determined by DNS (Domain Name System)&lt;br /&gt;
*DNS has almost no security&lt;br /&gt;
*There is DNSSEC but nobody uses it, yet at least&lt;br /&gt;
**If you want to know more about this, you can find the article on [usenix.org usenix.org]&lt;br /&gt;
*You can better protect cookies by using HSTS&lt;br /&gt;
**This forces HTTPS everywhere on a page/domain&lt;br /&gt;
**Protects against mixes of HTTP/HTTPS communications and downgrade attacks&lt;br /&gt;
*Why we do we have cookies in the first place?&lt;br /&gt;
**Because HTTP was supposed to be stateless&lt;br /&gt;
**The cookies are used instead to create a state between the client and server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*If we could get past these problems, would we have proper security?&lt;br /&gt;
**Definitely not&lt;br /&gt;
*There are other issues...&lt;br /&gt;
&lt;br /&gt;
======Cross-Site Scripting (XSS) Attacks======&lt;br /&gt;
&lt;br /&gt;
*Not necessarily cross-site, and not necessarily scripting&lt;br /&gt;
*More like “web injection attacks”&lt;br /&gt;
*This can happen anywhere an attacker can inject content into a page&lt;br /&gt;
**Ads&lt;br /&gt;
**Comments&lt;br /&gt;
**Social media&lt;br /&gt;
**User-generated content&lt;br /&gt;
*Problems can occur when there is a failure to sanitize untrusted input that is inserted into a web page&lt;br /&gt;
**This allows users to insert arbitrary HTML, CSS, and JavaScript into the page&lt;br /&gt;
&lt;br /&gt;
======Cross-Site Request Forgery======&lt;br /&gt;
&lt;br /&gt;
*Cross-site scripting is more straight-forward, cross-site request forgery is a little bit weird&lt;br /&gt;
*This involves having a link from a page sent by one server cause a request to be sent off to another server&lt;br /&gt;
*If you are logged in at the other server, this can cause things to happen which you didn&#039;t expect or want to happen&lt;br /&gt;
**For example, while logged in to the course wiki, a link from a page on a different website may cause a page to be deleted from the wiki by sending an appropriate request&lt;br /&gt;
*This makes use of cookies automatically being sent to a server, no matter where the link came from&lt;br /&gt;
*How can we stop this?&lt;br /&gt;
**One way is to try to verify where the link came from&lt;br /&gt;
**In the browser tools, go to the network tab and check the referer header&lt;br /&gt;
***This header indicates where the request came from&lt;br /&gt;
**So in principle, to stop a cross-site request forgery, we can check this header in the requests received on the server&lt;br /&gt;
***If it came from one of our pages, then we can trust the request&lt;br /&gt;
***Otherwise we don’t&lt;br /&gt;
**That does not actually work. It kind of works, but not always&lt;br /&gt;
**Why? Because the Referer header can often be scrapped by a proxy&lt;br /&gt;
**An alternative solution is to randomize your links&lt;br /&gt;
**Instead of using a predictable url, stick a random string in the middle of it&lt;br /&gt;
***The string is connected to the client&#039;s cookie so that the attacker can’t guess it&lt;br /&gt;
&lt;br /&gt;
======Same-Origin Policy======&lt;br /&gt;
&lt;br /&gt;
*Data should only loaded from the originating server but, you can load almost anything else from anywhere&lt;br /&gt;
**Images&lt;br /&gt;
**Sound&lt;br /&gt;
**JavaScript&lt;br /&gt;
*So what about JSON?&lt;br /&gt;
**when you load it with AJAX, you have the same-origin policy&lt;br /&gt;
**But what if you just treated it as a script? (why not just load it in the page?)&lt;br /&gt;
**And what if you were logged in?&lt;br /&gt;
***Consider a bank sending data as JSON (Could I from another tab just grab your bank account data as a JSON data in the background and see what exactly what you have? It doesn’t work.)&lt;br /&gt;
*JSON files are valid JavaScript structure (JSON is a subset of JavaScript)&lt;br /&gt;
*JSON files do not specify a name to give to the data structure so you can’t access it by name&lt;br /&gt;
*However, to build the object in the first place, its constructor must be called&lt;br /&gt;
**Why not replace the constructor with our own method?&lt;br /&gt;
***e.g., override Array&lt;br /&gt;
**This is solved in regular browsers for JSON by only using a clean JavaScript environment for JSON.parse()&lt;br /&gt;
**But what about code?&lt;br /&gt;
***This doesn’t help you for code, but who cares about code, right?&lt;br /&gt;
**Turns out many websites dynamically generate JavaScript and put personal information into it (different people have different scripts)&lt;br /&gt;
***Solution? Don’t mix code and data! (treat the JSON separately, let the code just be loaded on the system, don’t mix it with anything else)&lt;br /&gt;
&lt;br /&gt;
====Web Assembly====&lt;br /&gt;
&lt;br /&gt;
*Many, many people hate JavaScript&lt;br /&gt;
**There is a lot of code that isn’t JavaScript but people want everything to run on the web in a browser (JavaScript is the language of the web)&lt;br /&gt;
*Past efforts at having alternative languages all had a basic problem&lt;br /&gt;
**They couldn’t see the DOM&lt;br /&gt;
***e.g., Java applets, Flash, Silverlight&lt;br /&gt;
*And, what about all those other languages?&lt;br /&gt;
**C,C++?&lt;br /&gt;
**How about games?&lt;br /&gt;
***Unreal runs in a web page because there’s WebGL which is pretty fast (WebGL is a subset of openGL)&lt;br /&gt;
*So why can’t we compile code and have it run in the browser?&lt;br /&gt;
*Two old solutions:&lt;br /&gt;
**NaCl (Google)&lt;br /&gt;
***Native Client is a sandbox for running compiled C and C++ code in the browser efficiently and securely, independent of the user’s operating system&lt;br /&gt;
**asm.js (Mozilla)&lt;br /&gt;
***This uses a compiler that takes arbitrary programs and compiles them to JavaScript&lt;br /&gt;
***asm.js is perfectly legal JavaScript, it is tightly restricted JavaScript&lt;br /&gt;
***You cannot read asm.js code, it looks like binary&lt;br /&gt;
*Google and Mozilla joined up and are now making WebAssembly&lt;br /&gt;
**You can compile arbitrary code to run inside of the browser with WebAssembly&lt;br /&gt;
**This will be good for functionality and bad for security&lt;br /&gt;
***Security won’t go well because the code isn’t designed for the web&lt;br /&gt;
***But it will be cool to play games in a browser at full speed&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_20&amp;diff=20894</id>
		<title>WebFund 2016W Lecture 20</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_20&amp;diff=20894"/>
		<updated>2016-03-31T14:04:47Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: /* Exam Storage */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 24, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec20-24Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===Assignment 5 – Last Minute Question===&lt;br /&gt;
&lt;br /&gt;
*How should the download functionality display the query result? &lt;br /&gt;
**Display as a page as a text file. Do a &amp;lt;code&amp;gt;res.send()&amp;lt;/code&amp;gt; of a big string with each log entry terminated by a new line. You should be sending the file as plain text. &lt;br /&gt;
&lt;br /&gt;
===Assignment 6 Hints===&lt;br /&gt;
&lt;br /&gt;
*This will involve making an application that works in a similar fashion to exam-storage (a single-page app)&lt;br /&gt;
*You will be doing the same things that are already implemented in assignment 5 but you need to change it to work from a single page&lt;br /&gt;
&lt;br /&gt;
===Exam Storage===&lt;br /&gt;
&lt;br /&gt;
*A little bit more complicated than interactive-demo. &lt;br /&gt;
*The file structure of the application is modified to clarify the division between what runs on the client and what runs on the server&lt;br /&gt;
**public: contains everything that goes to the client directly (CSS and JavaScript files)&lt;br /&gt;
**server: contains all the JavaScript files that will run on the server&lt;br /&gt;
**start.js is a little JavaScript file that you will use to start the server (this is replacing bin/www)&lt;br /&gt;
**views contains all the templates for generating html&lt;br /&gt;
**keys: cryptographic keys&lt;br /&gt;
*Note that you can use &amp;lt;code&amp;gt;npm rebuild&amp;lt;/code&amp;gt; to recompile any binary dependencies in the application modules (in case something is not working on your machine)&lt;br /&gt;
&lt;br /&gt;
====start.js====&lt;br /&gt;
&lt;br /&gt;
*The app.js file is now located in the server directory&lt;br /&gt;
**The call to &amp;lt;code&amp;gt;require()&amp;lt;/code&amp;gt; in start.js is therefore changed to: &amp;lt;code&amp;gt;var app = require(‘./server/app’);&amp;lt;/code&amp;gt;&lt;br /&gt;
**We then set the port, create the server and it start listening on port 3000.&lt;br /&gt;
&lt;br /&gt;
====app.js====&lt;br /&gt;
&lt;br /&gt;
*Things are fairly similar to our previous applications&lt;br /&gt;
**We are specifying the ssl keys and setting up the same modules we&#039;ve previously used including one for sessions&lt;br /&gt;
**&amp;lt;code&amp;gt;app.use(express.static(‘public’));&amp;lt;/code&amp;gt; is serving everything from public directory&lt;br /&gt;
&lt;br /&gt;
====routes.js ====&lt;br /&gt;
&lt;br /&gt;
*Starts with setting up a connection to the database.&lt;br /&gt;
**The users collection contains documents which have a username and a hashed password&lt;br /&gt;
*Many routes defined in here are similar to what we have done before&lt;br /&gt;
*The &amp;lt;code&amp;gt;storeSaltedpassword()&amp;lt;/code&amp;gt; function in the /register is something new&lt;br /&gt;
**This stores the username and hashed and salted password in the users collection&lt;br /&gt;
**The hashing and salting is done using bcrypt&lt;br /&gt;
*In the /login route, we are again using bcrypt to compare a password that a user has entered with a hashed version stored in the database&lt;br /&gt;
**Why do we have the same error message for both incorrect password and invalid username?&lt;br /&gt;
***If someone is trying to break into your system, you don’t want them to know that they have come across the valid account and they are trying to figure out the password. You just want them to know that the username and the password is not the right one. Therefore, we use the same error string for both username and password. &lt;br /&gt;
&lt;br /&gt;
====Password Hashing====&lt;br /&gt;
&lt;br /&gt;
*When you log in to the virtual machine and type in the password, it is stored in the virtual machine&lt;br /&gt;
**Go to cd /etc there is a file called passwd which gives you a file that contains all of the accounts on the system&lt;br /&gt;
**In old school UNIX installations, the hash of the password is shown instead of &#039;x&#039; in the file, but not here if you check the stored passwords stored in virtual machine&lt;br /&gt;
**To show the hash of the password in the file, you can type in the command &amp;lt;code&amp;gt;sudo shadowconfig off&amp;lt;/code&amp;gt;, and then you should be able to see the file with the hash of your password instead of the &#039;x&#039; &lt;br /&gt;
*For any file you create, you can compute a sha1sum (hash) of that file which is a big alphanumeric string&lt;br /&gt;
**If you change one bit in the file (input), it will make a huge change in the output&lt;br /&gt;
**Sha1sum should no longer be used since it is no longer considered secure but you can use sha256 instead &lt;br /&gt;
*Bcrypt is what we are using to hash passwords before storing them in the database&lt;br /&gt;
**Bcrypt is a hash function but it is a bit weird. It is slow by design and is memory intensive.&lt;br /&gt;
**Storing your passwords using bcrypt is really good today but in general, it is a good idea to use some kind of framework of something like Passport.&lt;br /&gt;
***These types of frameworks take care of making decisions that you might not know how to make and they help avoid using outdated and insecure technology&lt;br /&gt;
***Passport is a middleware authentication module for Node.js&lt;br /&gt;
*Why do we want to use hashing for passwords in the first place? Why do we not want to store them in the plain text?&lt;br /&gt;
**We want to avoid a breach in security caused by an attacker obtaining the stored passwords&lt;br /&gt;
**The hashing approach should have an irreversibility property that is essential for password storage, has adequate pre-computation defenses, and performance defenses needed to prevent dictionary attacks&lt;br /&gt;
**Just by looking at a hashed string, no one should be able to guess the original value and get your password&lt;br /&gt;
***They will actually have to play around with the hashing function or will have to plug in random values and try every single possibility which should hopefully take them a very long time&lt;br /&gt;
*In our web application, it is important to do the hashing on the server as opposed to on the client&#039;s machine even though doing this on the client would reduce the computational burden for the server&lt;br /&gt;
**This is because we cannot trust anything that is sent to us from the client&lt;br /&gt;
**Rather than computing the hash function and then sending us the resultant hash, the client could iterate over hash values and send them to us&lt;br /&gt;
***This would allow the client to avoid the need to spend time actually running the hash function&lt;br /&gt;
&lt;br /&gt;
====Salting====&lt;br /&gt;
&lt;br /&gt;
*Let’s say user 1 and user 2 have the same password, they will also have the same resultant hash&lt;br /&gt;
**We don’t want that as this makes things easier for an attacker&lt;br /&gt;
**What we will do is that we use the same password for both but add a string called salt1 and salt2 for users 1 and 2 respectively&lt;br /&gt;
**Now they have completely different hashes but the password is still the same&lt;br /&gt;
**Every user should have a different hash&lt;br /&gt;
*Salting is also the defeat to precomputation&lt;br /&gt;
**An attacker could use a precomputed dictionary of hash values allowing them to quickly look up the input that was used to create the hash&lt;br /&gt;
**By hashing a salted password, we are able to render the lookup tables useless&lt;br /&gt;
&lt;br /&gt;
====account.js====&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;code&amp;gt;updateFileList()&amp;lt;/code&amp;gt; is used to update the files displayed on the page&lt;br /&gt;
**It first sends an ajax GET request for /getFileStats to the server to get the updated file list&lt;br /&gt;
***When the server receives this request, it first checks to see if the user is logged in&lt;br /&gt;
***If the user is logged in, it queries the database to get their uploaded files&lt;br /&gt;
****When doing a query, we can optionally specify a projection object&lt;br /&gt;
****The projection is used to specify which information (document attributes) you want to retrieve in the query&lt;br /&gt;
****We specify the query object as &amp;lt;code&amp;gt;{content:0}&amp;lt;/code&amp;gt; meaning that we do not want to include the content attribute in the documents that we retrieve&lt;br /&gt;
**In the &amp;lt;code&amp;gt;doUpdateFileList()&amp;lt;/code&amp;gt; callback function, we update the DOM with the data received in the response&lt;br /&gt;
*&amp;lt;code&amp;gt;updateFileList()&amp;lt;/code&amp;gt; is called when the page is initially loaded and every time a file is uploaded&lt;br /&gt;
**This means that the content on the page will always be up to date&lt;br /&gt;
*When a user requests a file download, how do we know which file to give them?&lt;br /&gt;
**We use the &amp;lt;code&amp;gt;downloadFile()&amp;lt;/code&amp;gt; function to create and return a function which will be used as a callback function for the click event of the link to download a specific file&lt;br /&gt;
**By passing the ID of the file into &amp;lt;code&amp;gt;downloadFile()&amp;lt;/code&amp;gt; we are able to create a callback function specific to each file so that we know which one to download when it gets called for the click event&lt;br /&gt;
*Notice that the href attribute for the download links is set to #&lt;br /&gt;
**This causes the link to take the user to the top of the page&lt;br /&gt;
**This is useful when we want a link that will not cause the user to go to a different page&lt;br /&gt;
*&amp;lt;code&amp;gt;saveDownloadedFile()&amp;lt;/code&amp;gt; is used as a callback function for the POST request that is sent to the server when a file is requested for download from within a download click event callback function&lt;br /&gt;
**This function handles saving the file that gets sent to the client from the server&lt;br /&gt;
**It relies on the &amp;lt;code&amp;gt;saveAs()&amp;lt;/code&amp;gt; function which lets us download file in the folder/location of our choice without changing the page.&lt;br /&gt;
*The reason why you want to manipulate things on the browser instead of on the server and the differences involved in making this change are important parts of exam-storage that you should understand&lt;br /&gt;
&lt;br /&gt;
===Types of Questions for the Exam===&lt;br /&gt;
&lt;br /&gt;
*Why are we using https?&lt;br /&gt;
**To protect data going between the browser and the server, just to make sure everything is encrypted there&lt;br /&gt;
*Why are we using bcrypt?&lt;br /&gt;
**To make sure that the password is not stored in plain text on the server&lt;br /&gt;
*What if in routes.js I just compare the password and the user.password instead of doing bcrypt? &lt;br /&gt;
&lt;br /&gt;
===Next Week===&lt;br /&gt;
&lt;br /&gt;
*High level topics which aren’t included in the final&lt;br /&gt;
*How do you make web applications pretty without being a web designer? &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
In this lecture we discussed [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/exam-storage.zip exam-storage], the code for [[WebFund 2016W: Tutorial 10|Tutorial 10]]&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_21&amp;diff=20893</id>
		<title>WebFund 2016W Lecture 21</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_21&amp;diff=20893"/>
		<updated>2016-03-30T19:20:08Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 29, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec21-29Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===In Class===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 21&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
* interactive-graphs-sol will be posted after lecture&lt;br /&gt;
* study session on April 11th, time TBD (final is on April 12th) but will be in the morning.&lt;br /&gt;
  (Earliest is 10 AM).&lt;br /&gt;
&lt;br /&gt;
What did I leave out?&lt;br /&gt;
&lt;br /&gt;
First, what did we cover?&lt;br /&gt;
 - JavaScript&lt;br /&gt;
 - Some HTML, CSS&lt;br /&gt;
 - clients versus servers&lt;br /&gt;
 - HTTP&lt;br /&gt;
 - synchronous versus async code&lt;br /&gt;
   - callbacks&lt;br /&gt;
 - Node, Jade, Express&lt;br /&gt;
 - browser developer tools&lt;br /&gt;
 - closures, lexical scoping&lt;br /&gt;
 - a little networking&lt;br /&gt;
 - databases&lt;br /&gt;
   - noSQL, MongoDB&lt;br /&gt;
   - queries&lt;br /&gt;
 - a little crypto (SSL, certificates, password hashing)&lt;br /&gt;
 - a little security&lt;br /&gt;
   (cross-site scripting, input validation&lt;br /&gt;
 - client side versus server side JavaScript&lt;br /&gt;
   - jQuery&lt;br /&gt;
 - DOM basics&lt;br /&gt;
 - basic AJAX&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Left out...&lt;br /&gt;
 - much more to HTML, CSS&lt;br /&gt;
 - much more to the DOM&lt;br /&gt;
 - more to AJAX, client-server communication&lt;br /&gt;
   (e.g., WebSockets)&lt;br /&gt;
 - SQL&lt;br /&gt;
   - transactions&lt;br /&gt;
   - indexing *&lt;br /&gt;
 - PHP&lt;br /&gt;
   - language + HTML templates&lt;br /&gt;
   - instead we learned Node + Jade&lt;br /&gt;
 - Meteor, AngularJS, ...&lt;br /&gt;
   client-side JS libraries and frameworks&lt;br /&gt;
 - other server-side languages &amp;amp; frameworks&lt;br /&gt;
   - ASP.NET, .NET, Java-based frameworks,&lt;br /&gt;
     Ruby on Rails&lt;br /&gt;
   - every programming language has multiple&lt;br /&gt;
     web frameworks&lt;br /&gt;
 &lt;br /&gt;
So, is this how the &amp;quot;big players&amp;quot; do the web?&lt;br /&gt;
 - facebook, google, etc.&lt;br /&gt;
&lt;br /&gt;
They have&lt;br /&gt;
 - more servers&lt;br /&gt;
 - challenge is how to *use* them&lt;br /&gt;
&lt;br /&gt;
HTTP is inherently stateless.  How do you scale stateless?&lt;br /&gt;
 - replicate, load balance&lt;br /&gt;
 - to store state, make state store bigger&lt;br /&gt;
   - need bigger database!&lt;br /&gt;
&lt;br /&gt;
Big websites have some mix of:&lt;br /&gt;
 - scalable databases&lt;br /&gt;
 - replicate application servers&lt;br /&gt;
 - caching for static content&lt;br /&gt;
&lt;br /&gt;
E.g., ecommerce&lt;br /&gt;
 - while shopping, users get static content&lt;br /&gt;
    - easy to scale&lt;br /&gt;
 - but purchases have to hit the database&lt;br /&gt;
   - and have to generate writes&lt;br /&gt;
&lt;br /&gt;
To scale you have to engineer the system&lt;br /&gt;
 - but you don&#039;t need to change your basic application&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Security?&lt;br /&gt;
 - input validation/sanitization&lt;br /&gt;
   - don&#039;t let the user talk to the browser, server,&lt;br /&gt;
     or database directly - no code injection&lt;br /&gt;
 - use reasonable crypto&lt;br /&gt;
   - communications security (SSL/TLS)&lt;br /&gt;
   - authentication (passwords, etc)&lt;br /&gt;
 - security policy&lt;br /&gt;
   - what is allowed, what is not&lt;br /&gt;
   - how to enforce&lt;br /&gt;
&lt;br /&gt;
Networking?&lt;br /&gt;
 - latency versus bandwidth*&lt;br /&gt;
 - IPv6&lt;br /&gt;
 - firewalls&lt;br /&gt;
 - NAT&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Cloud computing&lt;br /&gt;
 - remote servers where there are LOTS of remote servers&lt;br /&gt;
 - challenge is in coordinating applications across&lt;br /&gt;
   all of those servers&lt;br /&gt;
 - basic tools is virtualization&lt;br /&gt;
    - easier to manage a virtual server than a real one&lt;br /&gt;
    - Give me 10,000 servers for 2 hours&lt;br /&gt;
 - cloud servers run web apps, almost entirely&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Amazon is the real &amp;quot;parent&amp;quot; of cloud computing&lt;br /&gt;
 - they got into the cloud because of Christmas&lt;br /&gt;
 - they had servers that were doing nothing 11 months&lt;br /&gt;
   out of the year&lt;br /&gt;
   - so why not rent them out&lt;br /&gt;
 - they provide more and more infrastructure for&lt;br /&gt;
   building large web applications, based on their&lt;br /&gt;
   internal tech&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Networking&lt;br /&gt;
&lt;br /&gt;
The Internet is a network of networks&lt;br /&gt;
  SCS network is connected to the Carleton backbone&lt;br /&gt;
  which is connected to the Unicentre&#039;s network.  The&lt;br /&gt;
  backbone also connects to Carleton&#039;s ISPs (ISPs)&lt;br /&gt;
&lt;br /&gt;
Firewalls on the Internet block all kinds of network traffic.&lt;br /&gt;
Mainly for &#039;security reasons&#039;&lt;br /&gt;
&lt;br /&gt;
Now, arbitrary network traffic gets routed through HTTP.&lt;br /&gt;
Because it can reach all of the Internet&lt;br /&gt;
 - other protocols get squeezed into HTTP&lt;br /&gt;
&lt;br /&gt;
WebRTC&lt;br /&gt;
&lt;br /&gt;
Learn networking but realize modern networks just ship HTTP&lt;br /&gt;
&lt;br /&gt;
* Databases, indexes&lt;br /&gt;
* Security&lt;br /&gt;
  - web security issues&lt;br /&gt;
* The future&lt;br /&gt;
&lt;br /&gt;
Posting Assignment 5 solutions now&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Announcements====&lt;br /&gt;
&lt;br /&gt;
*There might be an exam review on April 7th. There will also be a review session on April 11th (time TBD, probably AM). Final is on April 12th (9:00 AM).&lt;br /&gt;
*Solutions for Assignment 5 (Interactive Graphs) have been posted on the course website.&lt;br /&gt;
&lt;br /&gt;
====What We Covered in the Course====&lt;br /&gt;
&lt;br /&gt;
*First, what did we cover in the course so far?&lt;br /&gt;
**JavaScript&lt;br /&gt;
**Some HTML, CSS&lt;br /&gt;
**Clients versus servers&lt;br /&gt;
**HTTP&lt;br /&gt;
**Synchronous versus asynchronous code (callbacks)&lt;br /&gt;
**Node, Jade, Express&lt;br /&gt;
**Browser developer tools&lt;br /&gt;
**Closures, lexical scoping&lt;br /&gt;
**A little bit of networking&lt;br /&gt;
**Databases (noSQL, MongoDB)&lt;br /&gt;
***Queries&lt;br /&gt;
**A little bit of cryptography (SSL, certificates, password hashing)&lt;br /&gt;
**A little security (cross-site scripting, input validation)&lt;br /&gt;
**Client-side versus server-side JavaScript (jQuery)&lt;br /&gt;
**DOM basics&lt;br /&gt;
**Basic AJAX&lt;br /&gt;
*This is a good amount of material, especially given that COMP 2406 is an introductory class. However, it is only a small portion of the technologies upon which the modern web is based.&lt;br /&gt;
&lt;br /&gt;
====What did we leave out?====&lt;br /&gt;
&lt;br /&gt;
*There&#039;s much more to HTML and CSS&lt;br /&gt;
**The stuff we left out wasn&#039;t essential to an introductory class like 2406, and can be learned easily through online resources like MDN or Code Academy. You might have to learn them on your own if you intend to work on an actual web development project. Thanks to technologies like Express, however, you don&#039;t really have to know a lot of HTML and CSS to get a basic website up and running.&lt;br /&gt;
*SQL&lt;br /&gt;
**The way databases are accessed&lt;br /&gt;
**Transactions&lt;br /&gt;
**Indexing (how to make database access faster)&lt;br /&gt;
*PHP: language + HTML templates&lt;br /&gt;
**There are a lot of serious web applications, both proprietary and open source, that use PHP. If you understand Java and JavaScript well, PHP isn&#039;t very hard to learn. It&#039;s basically a language combined with HTML templates.&lt;br /&gt;
**Instead, we learned Node + Jade to generate HTML&lt;br /&gt;
*Meteor, AngularJS, and other client-side JS libraries and frameworks&lt;br /&gt;
**There are a lot of JS libraries: low-level as well as high-level&lt;br /&gt;
**There are JS libraries to do almost everything under the sun&lt;br /&gt;
*Other server-side languages and frameworks&lt;br /&gt;
**E.g. ASP.NET, .NET, Java-based frameworks, Ruby on Rails&lt;br /&gt;
**There&#039;s even a COBOL-based server-side language&lt;br /&gt;
**Learning each of these frameworks takes time and energy, but it might be worthwhile if you end up working with them.&lt;br /&gt;
**Many fundamental concepts will be the same across the different languages&lt;br /&gt;
*There&#039;s much more to the DOM&lt;br /&gt;
**The DOM is a very complicated data structure; there&#039;s much more to learn than what we covered in class.&lt;br /&gt;
*There&#039;s more to AJAX&lt;br /&gt;
**AJAX provides a way for the client and server to talk in the background  &lt;br /&gt;
  &lt;br /&gt;
====Scaling====  &lt;br /&gt;
  &lt;br /&gt;
*So, is this how the &amp;quot;big players&amp;quot; do web development?&lt;br /&gt;
**They have more servers&lt;br /&gt;
**The challenge is how to use the powerful servers&lt;br /&gt;
**Scalability becomes a key issue&lt;br /&gt;
*The big players have to be able to serve a massive number of people at the same time. Not only do they have to have more servers, they also need to utilize the additional hardware efficiently. The tough part is figuring out how to make use of the extra computational resources.&lt;br /&gt;
*HTTP is inherently stateless. How do you scale stateless machines?&lt;br /&gt;
**Replicate, load-balance&lt;br /&gt;
***These things can be done automatically for you if you use the cloud. For example: AWS.&lt;br /&gt;
*To store state, you&#039;d need a bigger database&lt;br /&gt;
**The database has to be able to scale to whatever load you put on it&lt;br /&gt;
**Companies like Oracle sell big, scalable databases&lt;br /&gt;
*Big websites have some mix of:&lt;br /&gt;
**Scalable databases&lt;br /&gt;
***Databases must be designed for concurrency&lt;br /&gt;
**Replication of application on servers (many instances running)&lt;br /&gt;
***Allows you to serve large number of clients concurrently&lt;br /&gt;
**Caching for static content&lt;br /&gt;
***Preemptive loading of some content to reduce dynamic database access&lt;br /&gt;
**The state of your app is not stored on server, it is stored in the database&lt;br /&gt;
**Good databases are designed for concurrency&lt;br /&gt;
**Good databases are designed to optimize write performance and store data reliably.&lt;br /&gt;
**However, as a web designer, your job is not to reinvent the wheel. Pick the proper DB option for your particular app.&lt;br /&gt;
**Your app should try to minimize DB accesses in order to increase performance&lt;br /&gt;
*E.g.: e-commerce&lt;br /&gt;
**While shopping, you&#039;re getting static content. That&#039;s because the static parts are easy to scale.&lt;br /&gt;
**However, purchases have to hit the database and have to invoke writes. Writes are much more expensive than reads.&lt;br /&gt;
*To scale up, you don&#039;t need to change your basic application. But you&#039;ll have to engineer the system so that it can handle a large amount of simultaneous database queries and writes.&lt;br /&gt;
  &lt;br /&gt;
====Security====&lt;br /&gt;
&lt;br /&gt;
*Input validation/sanitization&lt;br /&gt;
**Don&#039;t let users talk to the browser, server, or database directly (no code injection)&lt;br /&gt;
*Use good cryptography&lt;br /&gt;
**Communications security (SSL/TLS)&lt;br /&gt;
**Authentication (passwords, etc.)&lt;br /&gt;
*Security policy (what is allowed, what isn&#039;t allowed)&lt;br /&gt;
*Ways of enforcing the security policy&lt;br /&gt;
**Policy specification and enforcement are both difficult&lt;br /&gt;
&lt;br /&gt;
====Cloud Computing====&lt;br /&gt;
&lt;br /&gt;
*Remote servers that handle a lot of data and processing for clients&lt;br /&gt;
*The challenge is to coordinate applications across all the servers&lt;br /&gt;
*Basic tool is virtualization&lt;br /&gt;
**Easier to manage a virtual server than a real server&lt;br /&gt;
*It&#039;s easy to forget that cloud servers run big web apps, almost entirely&lt;br /&gt;
&lt;br /&gt;
====Amazon====&lt;br /&gt;
&lt;br /&gt;
*Amazon is the real &amp;quot;parent&amp;quot; of cloud computing&lt;br /&gt;
*They got into the cloud because of Christmas&lt;br /&gt;
**They had servers that were doing nothing 11 months out of the year&lt;br /&gt;
**Why not rent them out?&lt;br /&gt;
**That&#039;s how Amazon got into the cloud computing business!&lt;br /&gt;
**They provide more and more infrastructure for building large web apps, based on their internal technologies.&lt;br /&gt;
**They sell a scalable cloud computing service called AWS Elastic Beanstalk&lt;br /&gt;
***&amp;quot;elastic&amp;quot; refers to the scalability aspect of the service&lt;br /&gt;
***Handles load-balancing and instance replication automatically&lt;br /&gt;
&lt;br /&gt;
====Networking====&lt;br /&gt;
&lt;br /&gt;
*Latency versus badwidth&lt;br /&gt;
**Latency: how many hops to destination?&lt;br /&gt;
**Bandwidth: max total download/upload speed&lt;br /&gt;
*IPv6&lt;br /&gt;
*Firewalls&lt;br /&gt;
*NAT&lt;br /&gt;
*The internet is a &amp;quot;network of networks&amp;quot;. For instance, the SCS network is connected to the Carleton backbone, which is connected to the Unicentre&#039;s network. The backbone is also connected to Carleton&#039;s ISPs.&lt;br /&gt;
*Firewalls on the internet block all kinds of network traffic, mainly due to &amp;quot;security concerns&amp;quot;. However, firewalls always let web traffic through.&lt;br /&gt;
*Now, arbitrary network traffic gets routed through HTTP. This is mainly done for security. This means that other protocols can be squeezed into HTTP. Learn networking, but realize that modern networks just ship all kind of data within HTTP.&lt;br /&gt;
&lt;br /&gt;
====Topics for Next Lecture====&lt;br /&gt;
&lt;br /&gt;
*Databases, indexes&lt;br /&gt;
*Security (web security issues)&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_20&amp;diff=20888</id>
		<title>WebFund 2016W Lecture 20</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_20&amp;diff=20888"/>
		<updated>2016-03-28T18:41:20Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 24, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec20-24Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===Assignment 5 – Last Minute Question===&lt;br /&gt;
&lt;br /&gt;
*How should the download functionality display the query result? &lt;br /&gt;
**Display as a page as a text file. Do a &amp;lt;code&amp;gt;res.send()&amp;lt;/code&amp;gt; of a big string with each log entry terminated by a new line. You should be sending the file as plain text. &lt;br /&gt;
&lt;br /&gt;
===Assignment 6 Hints===&lt;br /&gt;
&lt;br /&gt;
*This will involve making an application that works in a similar fashion to exam-storage (a single-page app)&lt;br /&gt;
*You will be doing the same things that are already implemented in assignment 5 but you need to change it to work from a single page&lt;br /&gt;
&lt;br /&gt;
===Exam Storage===&lt;br /&gt;
&lt;br /&gt;
*A little bit more complicated than interactive-demo. &lt;br /&gt;
*The file structure of the application is modified to clarify the division between what runs on the client and what runs on the server&lt;br /&gt;
**public: contains everything that goes to the client directly (CSS and JavaScript files)&lt;br /&gt;
**server: contains all the JavaScript files that will run on the server&lt;br /&gt;
**start.js is a little JavaScript file that you will use to start the server (this is replacing bin/www)&lt;br /&gt;
**views contains all the templates for generating html&lt;br /&gt;
**keys: cryptographic keys&lt;br /&gt;
*Note that you can use &amp;lt;code&amp;gt;npm rebuild&amp;lt;code&amp;gt; to recompile any binary dependencies in the application modules (in case something is not working on your machine)&lt;br /&gt;
&lt;br /&gt;
====start.js====&lt;br /&gt;
&lt;br /&gt;
*The app.js file is now located in the server directory&lt;br /&gt;
**The call to &amp;lt;code&amp;gt;require()&amp;lt;/code&amp;gt; in start.js is therefore changed to: &amp;lt;code&amp;gt;var app = require(‘./server/app’);&amp;lt;/code&amp;gt;&lt;br /&gt;
**We then set the port, create the server and it start listening on port 3000.&lt;br /&gt;
&lt;br /&gt;
====app.js====&lt;br /&gt;
&lt;br /&gt;
*Things are fairly similar to our previous applications&lt;br /&gt;
**We are specifying the ssl keys and setting up the same modules we&#039;ve previously used including one for sessions&lt;br /&gt;
**&amp;lt;code&amp;gt;app.use(express.static(‘public’));&amp;lt;/code&amp;gt; is serving everything from public directory&lt;br /&gt;
&lt;br /&gt;
====routes.js ====&lt;br /&gt;
&lt;br /&gt;
*Starts with setting up a connection to the database.&lt;br /&gt;
**The users collection contains documents which have a username and a hashed password&lt;br /&gt;
*Many routes defined in here are similar to what we have done before&lt;br /&gt;
*The &amp;lt;code&amp;gt;storeSaltedpassword()&amp;lt;/code&amp;gt; function in the /register is something new&lt;br /&gt;
**This stores the username and hashed and salted password in the users collection&lt;br /&gt;
**The hashing and salting is done using bcrypt&lt;br /&gt;
*In the /login route, we are again using bcrypt to compare a password that a user has entered with a hashed version stored in the database&lt;br /&gt;
**Why do we have the same error message for both incorrect password and invalid username?&lt;br /&gt;
***If someone is trying to break into your system, you don’t want them to know that they have come across the valid account and they are trying to figure out the password. You just want them to know that the username and the password is not the right one. Therefore, we use the same error string for both username and password. &lt;br /&gt;
&lt;br /&gt;
====Password Hashing====&lt;br /&gt;
&lt;br /&gt;
*When you log in to the virtual machine and type in the password, it is stored in the virtual machine&lt;br /&gt;
**Go to cd /etc there is a file called passwd which gives you a file that contains all of the accounts on the system&lt;br /&gt;
**In old school UNIX installations, the hash of the password is shown instead of &#039;x&#039; in the file, but not here if you check the stored passwords stored in virtual machine&lt;br /&gt;
**To show the hash of the password in the file, you can type in the command &amp;lt;code&amp;gt;sudo shadowconfig off&amp;lt;/code&amp;gt;, and then you should be able to see the file with the hash of your password instead of the &#039;x&#039; &lt;br /&gt;
*For any file you create, you can compute a sha1sum (hash) of that file which is a big alphanumeric string&lt;br /&gt;
**If you change one bit in the file (input), it will make a huge change in the output&lt;br /&gt;
**Sha1sum should no longer be used since it is no longer considered secure but you can use sha256 instead &lt;br /&gt;
*Bcrypt is what we are using to hash passwords before storing them in the database&lt;br /&gt;
**Bcrypt is a hash function but it is a bit weird. It is slow by design and is memory intensive.&lt;br /&gt;
**Storing your passwords using bcrypt is really good today but in general, it is a good idea to use some kind of framework of something like Passport.&lt;br /&gt;
***These types of frameworks take care of making decisions that you might not know how to make and they help avoid using outdated and insecure technology&lt;br /&gt;
***Passport is a middleware authentication module for Node.js&lt;br /&gt;
*Why do we want to use hashing for passwords in the first place? Why do we not want to store them in the plain text?&lt;br /&gt;
**We want to avoid a breach in security caused by an attacker obtaining the stored passwords&lt;br /&gt;
**The hashing approach should have an irreversibility property that is essential for password storage, has adequate pre-computation defenses, and performance defenses needed to prevent dictionary attacks&lt;br /&gt;
**Just by looking at a hashed string, no one should be able to guess the original value and get your password&lt;br /&gt;
***They will actually have to play around with the hashing function or will have to plug in random values and try every single possibility which should hopefully take them a very long time&lt;br /&gt;
*In our web application, it is important to do the hashing on the server as opposed to on the client&#039;s machine even though doing this on the client would reduce the computational burden for the server&lt;br /&gt;
**This is because we cannot trust anything that is sent to us from the client&lt;br /&gt;
**Rather than computing the hash function and then sending us the resultant hash, the client could iterate over hash values and send them to us&lt;br /&gt;
***This would allow the client to avoid the need to spend time actually running the hash function&lt;br /&gt;
&lt;br /&gt;
====Salting====&lt;br /&gt;
&lt;br /&gt;
*Let’s say user 1 and user 2 have the same password, they will also have the same resultant hash&lt;br /&gt;
**We don’t want that as this makes things easier for an attacker&lt;br /&gt;
**What we will do is that we use the same password for both but add a string called salt1 and salt2 for users 1 and 2 respectively&lt;br /&gt;
**Now they have completely different hashes but the password is still the same&lt;br /&gt;
**Every user should have a different hash&lt;br /&gt;
*Salting is also the defeat to precomputation&lt;br /&gt;
**An attacker could use a precomputed dictionary of hash values allowing them to quickly look up the input that was used to create the hash&lt;br /&gt;
**By hashing a salted password, we are able to render the lookup tables useless&lt;br /&gt;
&lt;br /&gt;
====account.js====&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;code&amp;gt;updateFileList()&amp;lt;/code&amp;gt; is used to update the files displayed on the page&lt;br /&gt;
**It first sends an ajax GET request for /getFileStats to the server to get the updated file list&lt;br /&gt;
***When the server receives this request, it first checks to see if the user is logged in&lt;br /&gt;
***If the user is logged in, it queries the database to get their uploaded files&lt;br /&gt;
****When doing a query, we can optionally specify a projection object&lt;br /&gt;
****The projection is used to specify which information (document attributes) you want to retrieve in the query&lt;br /&gt;
****We specify the query object as &amp;lt;code&amp;gt;{content:0}&amp;lt;/code&amp;gt; meaning that we do not want to include the content attribute in the documents that we retrieve&lt;br /&gt;
**In the &amp;lt;code&amp;gt;doUpdateFileList()&amp;lt;/code&amp;gt; callback function, we update the DOM with the data received in the response&lt;br /&gt;
*&amp;lt;code&amp;gt;updateFileList()&amp;lt;/code&amp;gt; is called when the page is initially loaded and every time a file is uploaded&lt;br /&gt;
**This means that the content on the page will always be up to date&lt;br /&gt;
*When a user requests a file download, how do we know which file to give them?&lt;br /&gt;
**We use the &amp;lt;code&amp;gt;downloadFile()&amp;lt;/code&amp;gt; function to create and return a function which will be used as a callback function for the click event of the link to download a specific file&lt;br /&gt;
**By passing the ID of the file into &amp;lt;code&amp;gt;downloadFile()&amp;lt;/code&amp;gt; we are able to create a callback function specific to each file so that we know which one to download when it gets called for the click event&lt;br /&gt;
*Notice that the href attribute for the download links is set to #&lt;br /&gt;
**This causes the link to take the user to the top of the page&lt;br /&gt;
**This is useful when we want a link that will not cause the user to go to a different page&lt;br /&gt;
*&amp;lt;code&amp;gt;saveDownloadedFile()&amp;lt;/code&amp;gt; is used as a callback function for the POST request that is sent to the server when a file is requested for download from within a download click event callback function&lt;br /&gt;
**This function handles saving the file that gets sent to the client from the server&lt;br /&gt;
**It relies on the &amp;lt;code&amp;gt;saveAs()&amp;lt;/code&amp;gt; function which lets us download file in the folder/location of our choice without changing the page.&lt;br /&gt;
*The reason why you want to manipulate things on the browser instead of on the server and the differences involved in making this change are important parts of exam-storage that you should understand &lt;br /&gt;
&lt;br /&gt;
===Types of Questions for the Exam===&lt;br /&gt;
&lt;br /&gt;
*Why are we using https?&lt;br /&gt;
**To protect data going between the browser and the server, just to make sure everything is encrypted there&lt;br /&gt;
*Why are we using bcrypt?&lt;br /&gt;
**To make sure that the password is not stored in plain text on the server&lt;br /&gt;
*What if in routes.js I just compare the password and the user.password instead of doing bcrypt? &lt;br /&gt;
&lt;br /&gt;
===Next Week===&lt;br /&gt;
&lt;br /&gt;
*High level topics which aren’t included in the final&lt;br /&gt;
*How do you make web applications pretty without being a web designer? &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
In this lecture we discussed [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/exam-storage.zip exam-storage], the code for [[WebFund 2016W: Tutorial 10|Tutorial 10]]&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_19&amp;diff=20887</id>
		<title>WebFund 2016W Lecture 19</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_19&amp;diff=20887"/>
		<updated>2016-03-28T17:16:17Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on March 22, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec19-22Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===OpenStack===&lt;br /&gt;
&lt;br /&gt;
*There is no need to use the course VM to set up of connect to an instance&lt;br /&gt;
**This is intended as a substitute for the VM&lt;br /&gt;
*There are various options to get your web application on the instance:&lt;br /&gt;
**WINSCP&lt;br /&gt;
**wget [URL]&lt;br /&gt;
**Others...&lt;br /&gt;
*You can create multiple sessions to the same instance with SSH&lt;br /&gt;
&lt;br /&gt;
===Assignment 5===&lt;br /&gt;
&lt;br /&gt;
*Extended until Sunday (March 27) at 11:55 pm&lt;br /&gt;
*Extra functionality (2% bonus) will be at the TA’s discretion&lt;br /&gt;
*If things are running slowly for you, try testing with files no larger than 1000 entries&lt;br /&gt;
*If database has too many entries, drop them&lt;br /&gt;
**To drop: &amp;lt;code&amp;gt;db.logs.drop()&amp;lt;/code&amp;gt;&lt;br /&gt;
***This is done from the Mongo terminal client&lt;br /&gt;
*“Show” shows the entries in a scrollable textbox&lt;br /&gt;
&lt;br /&gt;
===MongoDB Storage===&lt;br /&gt;
&lt;br /&gt;
*By default, documents in MongoDB can be up to 16 megabytes&lt;br /&gt;
*For what we are doing, nothing should ever exceed this size since we are breaking files up into multiple documents&lt;br /&gt;
*GridFS can be used to store larger documents&lt;br /&gt;
**It will split these larger documents into chunks sized 255 kB each&lt;br /&gt;
&lt;br /&gt;
===Assignment 6===&lt;br /&gt;
&lt;br /&gt;
*This will involve making assignment 5 into a single-page web appliction&lt;br /&gt;
*Look at exam-storage to see an example of how to make a single-page application using client-side code&lt;br /&gt;
&lt;br /&gt;
===Tutorial 9===&lt;br /&gt;
&lt;br /&gt;
*The base application analyzes text entered by the user and counts word frequencies to display in a graph&lt;br /&gt;
*Division between client and server:&lt;br /&gt;
**Server: dumb web server (one route to render index)&lt;br /&gt;
**Client: everything (all the functionality, ie. interact.js)&lt;br /&gt;
*In index.jade, we can see that some client-side scripts are linked and many of the HTML elements have IDs&lt;br /&gt;
**These IDs are used in the client-side script to easily access the elements using jQuery&lt;br /&gt;
*It is important to understand the control flow when clicking on the update button&lt;br /&gt;
**The click event for the button causes a function to be called&lt;br /&gt;
**In this function, the user-entered data is taken from the page using jQuery&lt;br /&gt;
**Based on the settings specified by this data, words frequencies are counted and the graph is created&lt;br /&gt;
*What does the map method do?&lt;br /&gt;
**It processes each element of an array, passing that element in to a function&lt;br /&gt;
**The return value of the function is put into a new array&lt;br /&gt;
&lt;br /&gt;
===Other Notes===&lt;br /&gt;
&lt;br /&gt;
*If VM runs out of space, things may break&lt;br /&gt;
**Be careful, clean up space&lt;br /&gt;
**See the forums for a way to minimize the space MongoDB takes up&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
In this lecture we discussed [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/interactive-graphs.zip interactive-graphs], the code for [[WebFund 2016W: Tutorial 9|Tutorial 9]].&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_20&amp;diff=20883</id>
		<title>WebFund 2016W Lecture 20</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_20&amp;diff=20883"/>
		<updated>2016-03-26T02:33:42Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 24, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec20-24Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===OpenStack===&lt;br /&gt;
&lt;br /&gt;
*There is no need to use the course VM to set up of connect to an instance&lt;br /&gt;
**This is intended as a substitute for the VM&lt;br /&gt;
*There are various options to get your web application on the instance:&lt;br /&gt;
**WINSCP&lt;br /&gt;
**wget [URL]&lt;br /&gt;
**Others...&lt;br /&gt;
*You can create multiple sessions to the same instance with SSH&lt;br /&gt;
&lt;br /&gt;
===Assignment 5===&lt;br /&gt;
&lt;br /&gt;
*Extended until Sunday (March 27) at 11:55 pm&lt;br /&gt;
*Extra functionality (2% bonus) will be at the TA’s discretion&lt;br /&gt;
*If things are running slowly for you, try testing with files no larger than 1000 entries&lt;br /&gt;
*If database has too many entries, drop them&lt;br /&gt;
**To drop: &amp;lt;code&amp;gt;db.logs.drop()&amp;lt;/code&amp;gt;&lt;br /&gt;
***This is done from the Mongo terminal client&lt;br /&gt;
*“Show” shows the entries in a scrollable textbox&lt;br /&gt;
&lt;br /&gt;
===MongoDB Storage===&lt;br /&gt;
&lt;br /&gt;
*By default, documents in MongoDB can be up to 16 megabytes&lt;br /&gt;
*For what we are doing, nothing should ever exceed this size since we are breaking files up into multiple documents&lt;br /&gt;
*GridFS can be used to store larger documents&lt;br /&gt;
**It will split these larger documents into chunks sized 255 kB each&lt;br /&gt;
&lt;br /&gt;
===Assignment 6===&lt;br /&gt;
&lt;br /&gt;
*This will involve making assignment 5 into a single-page web appliction&lt;br /&gt;
*Look at exam-storage to see an example of how to make a single-page application using client-side code&lt;br /&gt;
&lt;br /&gt;
===Tutorial 9===&lt;br /&gt;
&lt;br /&gt;
*The base application analyzes text entered by the user and counts word frequencies to display in a graph&lt;br /&gt;
*Division between client and server:&lt;br /&gt;
**Server: dumb web server (one route to render index)&lt;br /&gt;
**Client: everything (all the functionality, ie. interact.js)&lt;br /&gt;
*In index.jade, we can see that some client-side scripts are linked and many of the HTML elements have IDs&lt;br /&gt;
**These IDs are used in the client-side script to easily access the elements using jQuery&lt;br /&gt;
*It is important to understand the control flow when clicking on the update button&lt;br /&gt;
**The click event for the button causes a function to be called&lt;br /&gt;
**In this function, the user-entered data is taken from the page using jQuery&lt;br /&gt;
**Based on the settings specified by this data, words frequencies are counted and the graph is created&lt;br /&gt;
*What does the map method do?&lt;br /&gt;
**It processes each element of an array, passing that element in to a function&lt;br /&gt;
**The return value of the function is put into a new array&lt;br /&gt;
&lt;br /&gt;
===Other Notes===&lt;br /&gt;
&lt;br /&gt;
*If VM runs out of space, things may break&lt;br /&gt;
**Be careful, clean up space&lt;br /&gt;
**See the forums for a way to minimize the space MongoDB takes up&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
In this lecture we discussed [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/exam-storage.zip exam-storage], the code for [[WebFund 2016W: Tutorial 10|Tutorial 10]]&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_18&amp;diff=20866</id>
		<title>WebFund 2016W Lecture 18</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_18&amp;diff=20866"/>
		<updated>2016-03-21T02:17:21Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 17, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec18-17Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In Class===&lt;br /&gt;
&lt;br /&gt;
(This is lecture 18, not 17!)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 18&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Symmetric cryptography&lt;br /&gt;
 - everyone uses the same key&lt;br /&gt;
 - think secret decoder rings&lt;br /&gt;
 - or, file encrypted with AES&lt;br /&gt;
 - secret has to be shared between sender and receiver&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Public-key cryptography&lt;br /&gt;
 - but what if we don&#039;t have a shared secret?&lt;br /&gt;
   - your password isn&#039;t good enough, and&lt;br /&gt;
   - websites don&#039;t remember your password&lt;br /&gt;
   - and how would you send them your password in&lt;br /&gt;
     the first place&lt;br /&gt;
 - instead, we split the key into two parts&lt;br /&gt;
   - a public key&lt;br /&gt;
   - a private key&lt;br /&gt;
 - whatever one does, the other can undo&lt;br /&gt;
&lt;br /&gt;
 - if you encrypt with a public key and decrypt with a&lt;br /&gt;
   private key&lt;br /&gt;
     - private one-way communication&lt;br /&gt;
     - e.g., you download Anil&#039;s public key and send&lt;br /&gt;
       Anil a secret message&lt;br /&gt;
 - if you encrypt with a private key and decrypt with &lt;br /&gt;
   the public key&lt;br /&gt;
     - that&#039;s a digital signature&lt;br /&gt;
&lt;br /&gt;
First rule of cryptography&lt;br /&gt;
 - friends don&#039;t let friends implement their own crypto&lt;br /&gt;
 - algorithms OR code!&lt;br /&gt;
&lt;br /&gt;
Why?  You&#039;ll miss protections against attacks.&lt;br /&gt;
Example: timing attacks&lt;br /&gt;
 - can extract secrets by watching execution time&lt;br /&gt;
 - exploits the fact that some numbers take longer to&lt;br /&gt;
   multiply &amp;amp; other operations&lt;br /&gt;
&lt;br /&gt;
Everyone else messes up too&lt;br /&gt;
 - so be prepared to update/replace all crypto-related&lt;br /&gt;
   code&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Additional Notes on OpenStack Instances====&lt;br /&gt;
&lt;br /&gt;
*When selecting a floating IP address, the subnet must correspond to the network that you chose for your instance i.e. If you pick comp2406-net-31 you should use a 134.117.31 address and if you pick comp2406-net-26, use a 134.117.26 address. Mismatching will result in an error&lt;br /&gt;
*&amp;lt;code&amp;gt;sudo setcap ‘cap_net_bind_service=rep’ /usr/bin/node.js&amp;lt;/code&amp;gt; is used to allow the user to bind to a privileged port&lt;br /&gt;
&lt;br /&gt;
====HTTPS====&lt;br /&gt;
&lt;br /&gt;
*HTTP is HyperText Transfer Protocol&lt;br /&gt;
*HTTPS is a secure version of HTTP which uses SSL/TLS&lt;br /&gt;
**SSL - Secure Sockets Layer&lt;br /&gt;
**TLS - Transport Layer Security&lt;br /&gt;
**The current version of SSL is TLS&lt;br /&gt;
**They are security protocols (cryptographic protocols) that encrypt communications to provide a secure connection over a network&lt;br /&gt;
*Normally in a URL, we don&#039;t see HTTP because it is the default so it is omitted&lt;br /&gt;
*With HTTPS, we will see the protocol written in the URL&lt;br /&gt;
*We also see a lock icon with HTTPS&lt;br /&gt;
**It means that the data you are sending is secure and the website you’re accessing has been digitally certified&lt;br /&gt;
**When you click on a lock icon, you can view information about the certificate that the server has&lt;br /&gt;
*If you want to enable SSL/TLS support in a Node application, you can change HTTP to HTTPS (change &amp;lt;code&amp;gt;var http = require(‘http’);&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;var http = require(‘https’);&amp;lt;/code&amp;gt; in bin/www)&lt;br /&gt;
**For the code to work, we have to specify some options (key and certificate) for the HTTPS server&lt;br /&gt;
*You can get it working by modifying the code:&lt;br /&gt;
&lt;br /&gt;
In bin/www:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
//use the app objects and put all the configurations into app, If require http is defined.&lt;br /&gt;
if (app.locals.sslOptions){&lt;br /&gt;
   var http = require(‘https’);&lt;br /&gt;
}&lt;br /&gt;
else{&lt;br /&gt;
   var http-require(‘http’);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//modify the ‘HTTP server’ part of your code with sslOptions.&lt;br /&gt;
//create an object in the app file.&lt;br /&gt;
if (app.locals.sslOptions){&lt;br /&gt;
   var server = http.createServer(app.locals.sslOptions, app);&lt;br /&gt;
}&lt;br /&gt;
else{&lt;br /&gt;
   var server = http.createServer(app);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In app.js, we specify the HTTPS options by loading the key and certificate files:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var fs = require(&#039;fs&#039;);&lt;br /&gt;
&lt;br /&gt;
//put this after app is defined but before it is exported&lt;br /&gt;
app.locals.sslOptions = {&lt;br /&gt;
   key: fs.readFileSync(‘keys/comp2406-test-key.pem’),&lt;br /&gt;
   cert: fs.readFileSync(‘keys/comp2406-test-cert.pem’)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Certificates=====&lt;br /&gt;
&lt;br /&gt;
*Certificates are a form of digital authentication for a web server&lt;br /&gt;
*They contain information about the server and the authority that has signed the certificate&lt;br /&gt;
*Companies such as GlobalSign and Verisign are certificate authorities which are relied upon to sign trustworthy certificates&lt;br /&gt;
*If you do not want to pay a trusted certificate authority to sign your server&#039;s certificate, you can sign it yourself&lt;br /&gt;
**This gives you a usable certificate but it will produce a warning when a browser communicates with your server since it will receive a certificate signed by an untrusted source&lt;br /&gt;
**In order to get our web application to work properly with SSL/TLS, we have to create a key and use it to sign a certificate&lt;br /&gt;
**An alternative to get a trusted certificate is to get a free temporary certificate (from Let&#039;s Encrypt) and update it by using a script to run it in order to obtain new ones upon expiry&lt;br /&gt;
&lt;br /&gt;
=====Symmetric Cryptography=====&lt;br /&gt;
&lt;br /&gt;
*With symmetric cryptography, everyone uses the same key i.e. if you want to send a secret message to another person, you have to have the same secret decoder ring or files encrypted with AES&lt;br /&gt;
**AES is a symmetric block cipher in which the same key is used to encrypt and decrypt a document. It is a block cipher because it encrypts data in blocks as opposed to the whole thing&lt;br /&gt;
*The key to symmetric cryptography is that the secret has to be shared between the sender and receiver to keep it a secret i.e. instead of making the whole document secret, just make a key to keep it secure&lt;br /&gt;
**The difficulty is in initially sharing this secret in a secure fashion&lt;br /&gt;
&lt;br /&gt;
=====Public-key Cryptography=====&lt;br /&gt;
&lt;br /&gt;
*But what if we don’t have a shared secret?&lt;br /&gt;
*Passwords aren’t good enough&lt;br /&gt;
*Websites don’t remember your password&lt;br /&gt;
**How would you send them your password in the first place (in a secure fashion)?&lt;br /&gt;
*Instead, we split the key into two parts: public key and private key&lt;br /&gt;
*In public key cryptography, you have two keys, a matched pair of public and private keys are opposite (inverters) because what you do with one can be undone with the other&lt;br /&gt;
**If you encrypt with a public key and decrypt with a private key, it’s a private one way communication&lt;br /&gt;
***For example, you can send the professor a secret message after downloading his public key and using it for encryption&lt;br /&gt;
**If you encrypt with a private key and decrypt with the public key, that’s a digital signature&lt;br /&gt;
*This is what is used for HTTPS&lt;br /&gt;
&lt;br /&gt;
=====Creating Our Key and Certificate=====&lt;br /&gt;
&lt;br /&gt;
*We can use OpenSSL to create a key and to use that key to sign a certificate&lt;br /&gt;
*Randomness is an important factor in the creation of our key&lt;br /&gt;
**We want this done in a non-deterministic fashion so that nobody can guess what our key is&lt;br /&gt;
**OpenSSL uses Linux drivers which are designed to gather random data from events such as:&lt;br /&gt;
***User actions on the keyboard and mouse&lt;br /&gt;
***CPU temperatures&lt;br /&gt;
***Network activity&lt;br /&gt;
&lt;br /&gt;
====First Rule of Cryptography====&lt;br /&gt;
&lt;br /&gt;
*Friends don’t let friends implement their own cryptography, algorithms or code because it’s hard to do it and be sure you’re secure&lt;br /&gt;
*Why?&lt;br /&gt;
**You’ll miss protections against attacks. E.g. Timing attacks.&lt;br /&gt;
***In a timing attack, the attacker can extract secrets by watching execution time&lt;br /&gt;
***This exploits the fact that some numbers take longer to multiply than others&lt;br /&gt;
*Even professionally developped crypto systems will have bugs, so be prepared to update/replace all crypto-related code&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/analyzeLogs-ssl-template.zip analyzeLogs-ssl-template.zip]&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_18&amp;diff=20865</id>
		<title>WebFund 2016W Lecture 18</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_18&amp;diff=20865"/>
		<updated>2016-03-21T02:12:49Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 17, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec18-17Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In Class===&lt;br /&gt;
&lt;br /&gt;
(This is lecture 18, not 17!)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 18&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Symmetric cryptography&lt;br /&gt;
 - everyone uses the same key&lt;br /&gt;
 - think secret decoder rings&lt;br /&gt;
 - or, file encrypted with AES&lt;br /&gt;
 - secret has to be shared between sender and receiver&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Public-key cryptography&lt;br /&gt;
 - but what if we don&#039;t have a shared secret?&lt;br /&gt;
   - your password isn&#039;t good enough, and&lt;br /&gt;
   - websites don&#039;t remember your password&lt;br /&gt;
   - and how would you send them your password in&lt;br /&gt;
     the first place&lt;br /&gt;
 - instead, we split the key into two parts&lt;br /&gt;
   - a public key&lt;br /&gt;
   - a private key&lt;br /&gt;
 - whatever one does, the other can undo&lt;br /&gt;
&lt;br /&gt;
 - if you encrypt with a public key and decrypt with a&lt;br /&gt;
   private key&lt;br /&gt;
     - private one-way communication&lt;br /&gt;
     - e.g., you download Anil&#039;s public key and send&lt;br /&gt;
       Anil a secret message&lt;br /&gt;
 - if you encrypt with a private key and decrypt with &lt;br /&gt;
   the public key&lt;br /&gt;
     - that&#039;s a digital signature&lt;br /&gt;
&lt;br /&gt;
First rule of cryptography&lt;br /&gt;
 - friends don&#039;t let friends implement their own crypto&lt;br /&gt;
 - algorithms OR code!&lt;br /&gt;
&lt;br /&gt;
Why?  You&#039;ll miss protections against attacks.&lt;br /&gt;
Example: timing attacks&lt;br /&gt;
 - can extract secrets by watching execution time&lt;br /&gt;
 - exploits the fact that some numbers take longer to&lt;br /&gt;
   multiply &amp;amp; other operations&lt;br /&gt;
&lt;br /&gt;
Everyone else messes up too&lt;br /&gt;
 - so be prepared to update/replace all crypto-related&lt;br /&gt;
   code&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Additional Notes on OpenStack Instances====&lt;br /&gt;
&lt;br /&gt;
*When selecting a floating IP address, the subnet must correspond to the network that you chose for your instance i.e. If you pick comp2406-net-31 you should use a 134.117.31 address and if you pick comp2406-net-26, use a 134.117.26 address. Mismatching will result in an error&lt;br /&gt;
*&amp;lt;code&amp;gt;sudo setcap ‘cap_net_bind_service=rep’ /usr/bin/node.js&amp;lt;/code&amp;gt; is used to allow the user to bind to a privileged port&lt;br /&gt;
&lt;br /&gt;
====HTTPS====&lt;br /&gt;
&lt;br /&gt;
*HTTP is HyperText Transfer Protocol&lt;br /&gt;
*HTTPS is a secure version of HTTP which uses SSL/TLS&lt;br /&gt;
**SSL - Secure Sockets Layer&lt;br /&gt;
**TLS - Transport Layer Security&lt;br /&gt;
**The current version of SSL is TLS&lt;br /&gt;
**They are security protocols (cryptographic protocols) that encrypt communications to provide a secure connection over a network&lt;br /&gt;
*Normally in a URL, we don&#039;t see HTTP because it is the default so it is omitted&lt;br /&gt;
*With HTTPS, we will see the protocol written in the URL&lt;br /&gt;
*We also see a lock icon with HTTPS&lt;br /&gt;
**It means that the data you are sending is secure and the website you’re accessing has been digitally certified&lt;br /&gt;
**When you click on a lock icon, you can view information about the certificate that the server has&lt;br /&gt;
*If you want to enable SSL/TLS support in a Node application, you can change HTTP to HTTPS (change &amp;lt;code&amp;gt;var http = require(‘http’);&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;var http = require(‘https’);&amp;lt;/code&amp;gt; in bin/www)&lt;br /&gt;
**For the code to work, we have to specify some options (key and certificate) for the HTTPS server&lt;br /&gt;
*You can get it working by modifying the code:&lt;br /&gt;
&lt;br /&gt;
In bin/www:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
//use the app objects and put all the configurations into app, If require http is defined.&lt;br /&gt;
if (app.locals.sslOptions){&lt;br /&gt;
   var http = require(‘https’);&lt;br /&gt;
}&lt;br /&gt;
else{&lt;br /&gt;
   var http-require(‘http’);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//modify the ‘HTTP server’ part of your code with sslOptions.&lt;br /&gt;
//create an object in the app file.&lt;br /&gt;
if (app.locals.sslOptions){&lt;br /&gt;
   var server = http.createServer(app.locals.sslOptions, app);&lt;br /&gt;
}&lt;br /&gt;
else{&lt;br /&gt;
   var server = http.createServer(app);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In app.js, we specify the HTTPS options by loading the key and certificate files:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var fs = require(&#039;fs&#039;);&lt;br /&gt;
&lt;br /&gt;
//put this after app is defined but before it is exported&lt;br /&gt;
app.locals.sslOptions = {&lt;br /&gt;
   key: fs.readFileSync(‘keys/comp2406-test-key.pem’),&lt;br /&gt;
   cert: fs.readFileSync(‘keys/comp2406-test-cert.pem’)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Certificates=====&lt;br /&gt;
&lt;br /&gt;
*Certificates are a form of digital authentication for a web server&lt;br /&gt;
*They contain information about the server and the authority that has signed the certificate&lt;br /&gt;
*Companies such as GlobalSign and Verisign are certificate authorities which are relied upon to sign trustworthy certificates&lt;br /&gt;
*If you do not want to pay a trusted certificate authority to sign your server&#039;s certificate, you can sign it yourself&lt;br /&gt;
**This gives you a usable certificate but it will produce a warning when a browser communicates with your server since it will receive a certificate signed by an untrusted source&lt;br /&gt;
**In order to get our web application to work properly with SSL/TLS, we have to create a key and use it to sign a certificate&lt;br /&gt;
**An alternative to get a trusted certificate is to get a free temporary certificate (from Let&#039;s Encrypt) and update it by using a script to run it in order to obtain new ones upon expiry&lt;br /&gt;
&lt;br /&gt;
=====Symmetric Cryptography=====&lt;br /&gt;
&lt;br /&gt;
*With symmetric cryptography, everyone uses the same key i.e. if you want to send a secret message to another person, you have to have the same secret decoder ring or files encrypted with AES&lt;br /&gt;
**AES is a symmetric block cipher in which the same key is used to encrypt and decrypt a document. It is a block cipher because it encrypts data in blocks as opposed to the whole thing&lt;br /&gt;
*The key to symmetric cryptography is that the secret has to be shared between the sender and receiver to keep it a secret i.e. instead of making the whole document secret, just make a key to keep it secure&lt;br /&gt;
**The difficulty is in initially sharing this secret in a secure fashion&lt;br /&gt;
&lt;br /&gt;
=====Public-key Cryptography=====&lt;br /&gt;
&lt;br /&gt;
*But what if we don’t have a shared secret?&lt;br /&gt;
*Passwords aren’t good enough&lt;br /&gt;
*Websites don’t remember your password&lt;br /&gt;
**How would you send them your password in the first place (in a secure fashion)?&lt;br /&gt;
*Instead, we split the key into two parts: public key and private key&lt;br /&gt;
*In public key cryptography, you have two keys, a matched pair of public and private keys are opposite (inverters) because what you do with one can be undone with the other&lt;br /&gt;
**If you encrypt with a public key and decrypt with a private key, it’s a private one way communication&lt;br /&gt;
***For example, you can send the professor a secret message after downloading his public key and using it for encryption&lt;br /&gt;
**If you encrypt with a private key and decrypt with the public key, that’s a digital signature&lt;br /&gt;
*This is what is used for HTTPS&lt;br /&gt;
&lt;br /&gt;
====First Rule of Cryptography====&lt;br /&gt;
&lt;br /&gt;
*Friends don’t let friends implement their own cryptography, algorithms or code because it’s hard to do it and be sure you’re secure&lt;br /&gt;
*Why?&lt;br /&gt;
**You’ll miss protections against attacks. E.g. Timing attacks.&lt;br /&gt;
***In a timing attack, the attacker can extract secrets by watching execution time&lt;br /&gt;
***This exploits the fact that some numbers take longer to multiply than others&lt;br /&gt;
*Even professionally developped crypto systems will have bugs, so be prepared to update/replace all crypto-related code&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/analyzeLogs-ssl-template.zip analyzeLogs-ssl-template.zip]&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_18&amp;diff=20864</id>
		<title>WebFund 2016W Lecture 18</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_18&amp;diff=20864"/>
		<updated>2016-03-21T02:12:34Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 17, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec18-17Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In Class===&lt;br /&gt;
&lt;br /&gt;
(This is lecture 18, not 17!)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 18&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Symmetric cryptography&lt;br /&gt;
 - everyone uses the same key&lt;br /&gt;
 - think secret decoder rings&lt;br /&gt;
 - or, file encrypted with AES&lt;br /&gt;
 - secret has to be shared between sender and receiver&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Public-key cryptography&lt;br /&gt;
 - but what if we don&#039;t have a shared secret?&lt;br /&gt;
   - your password isn&#039;t good enough, and&lt;br /&gt;
   - websites don&#039;t remember your password&lt;br /&gt;
   - and how would you send them your password in&lt;br /&gt;
     the first place&lt;br /&gt;
 - instead, we split the key into two parts&lt;br /&gt;
   - a public key&lt;br /&gt;
   - a private key&lt;br /&gt;
 - whatever one does, the other can undo&lt;br /&gt;
&lt;br /&gt;
 - if you encrypt with a public key and decrypt with a&lt;br /&gt;
   private key&lt;br /&gt;
     - private one-way communication&lt;br /&gt;
     - e.g., you download Anil&#039;s public key and send&lt;br /&gt;
       Anil a secret message&lt;br /&gt;
 - if you encrypt with a private key and decrypt with &lt;br /&gt;
   the public key&lt;br /&gt;
     - that&#039;s a digital signature&lt;br /&gt;
&lt;br /&gt;
First rule of cryptography&lt;br /&gt;
 - friends don&#039;t let friends implement their own crypto&lt;br /&gt;
 - algorithms OR code!&lt;br /&gt;
&lt;br /&gt;
Why?  You&#039;ll miss protections against attacks.&lt;br /&gt;
Example: timing attacks&lt;br /&gt;
 - can extract secrets by watching execution time&lt;br /&gt;
 - exploits the fact that some numbers take longer to&lt;br /&gt;
   multiply &amp;amp; other operations&lt;br /&gt;
&lt;br /&gt;
Everyone else messes up too&lt;br /&gt;
 - so be prepared to update/replace all crypto-related&lt;br /&gt;
   code&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Additional Notes on OpenStack Instances====&lt;br /&gt;
&lt;br /&gt;
*When selecting a floating IP address, the subnet must correspond to the network that you chose for your instance i.e. If you pick comp2406-net-31 you should use a 134.117.31 address and if you pick comp2406-net-26, use a 134.117.26 address. Mismatching will result in an error&lt;br /&gt;
*&amp;lt;code&amp;gt;sudo setcap ‘cap_net_bind_service=rep’ /usr/bin/node.js&amp;lt;/code&amp;gt; is used to allow the user to bind to a privileged port&lt;br /&gt;
&lt;br /&gt;
====HTTPS====&lt;br /&gt;
&lt;br /&gt;
*HTTP is HyperText Transfer Protocol&lt;br /&gt;
*HTTPS is a secure version of HTTP which uses SSL/TLS&lt;br /&gt;
**SSL - Secure Sockets Layer&lt;br /&gt;
**TLS - Transport Layer Security&lt;br /&gt;
**The current version of SSL is TLS&lt;br /&gt;
**They are security protocols (cryptographic protocols) that encrypt communications to provide a secure connection over a network&lt;br /&gt;
*Normally in a URL, we don&#039;t see HTTP because it is the default so it is omitted&lt;br /&gt;
*With HTTPS, we will see the protocol written in the URL&lt;br /&gt;
*We also see a lock icon with HTTPS&lt;br /&gt;
**It means that the data you are sending is secure and the website you’re accessing has been digitally certified&lt;br /&gt;
**When you click on a lock icon, you can view information about the certificate that the server has&lt;br /&gt;
*If you want to enable SSL/TLS support in a Node application, you can change HTTP to HTTPS (change &amp;lt;code&amp;gt;var http = require(‘http’);&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;var http = require(‘https’);&amp;lt;/code&amp;gt; in bin/www)&lt;br /&gt;
**For the code to work, we have to specify some options (key and certificate) for the HTTPS server&lt;br /&gt;
*You can get it working by modifying the code:&lt;br /&gt;
&lt;br /&gt;
In bin/www:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
//use the app objects and put all the configurations into app, If require http is defined.&lt;br /&gt;
if (app.locals.sslOptions){&lt;br /&gt;
   var http = require(‘https’);&lt;br /&gt;
}&lt;br /&gt;
else{&lt;br /&gt;
   var http-require(‘http’);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//modify the ‘HTTP server’ part of your code with sslOptions.&lt;br /&gt;
//create an object in the app file.&lt;br /&gt;
if (app.locals.sslOptions){&lt;br /&gt;
   var server = http.createServer(app.locals.sslOptions, app);&lt;br /&gt;
}&lt;br /&gt;
else{&lt;br /&gt;
   var server = http.createServer(app);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In app.js, we specify the HTTPS options by loading the key and certificate files:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var fs = require(&#039;fs&#039;);&lt;br /&gt;
&lt;br /&gt;
//put this after app is defined but before it is exported&lt;br /&gt;
app.locals.sslOptions = {&lt;br /&gt;
   key: fs.readFileSync(‘keys/comp2406-test-key.pem’),&lt;br /&gt;
   cert: fs.readFileSync(‘keys/comp2406-test-cert.pem’)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Certificates=====&lt;br /&gt;
&lt;br /&gt;
*Certificates are a form of digital authentication for a web server&lt;br /&gt;
*They contain information about the server and the authority that has signed the certificate&lt;br /&gt;
*Companies such as GlobalSign and Verisign are certificate authorities which are relied upon to sign trustworthy certificates&lt;br /&gt;
*If you do not want to pay a trusted certificate authority to sign your server&#039;s certificate, you can sign it yourself&lt;br /&gt;
**This gives you a usable certificate but it will produce a warning when a browser communicates with your server since it will receive a certificate signed by an untrusted source&lt;br /&gt;
**In order to get our web application to work properly with SSL/TLS, we have to create a key and use it to sign a certificate&lt;br /&gt;
**An alternative to get a trusted certificate is to get a free temporary certificate (from Let&#039;s Encrypt) and update it by using a script to run it in order to obtain new ones upon expiry&lt;br /&gt;
&lt;br /&gt;
=====Symmetric Cryptography=====&lt;br /&gt;
&lt;br /&gt;
*With symmetric cryptography, everyone uses the same key i.e. if you want to send a secret message to another person, you have to have the same secret decoder ring or files encrypted with AES&lt;br /&gt;
**AES is a symmetric block cipher in which the same key is used to encrypt and decrypt a document. It is a block cipher because it encrypts data in blocks as opposed to the whole thing&lt;br /&gt;
*The key to symmetric cryptography is that the secret has to be shared between the sender and receiver to keep it a secret i.e. instead of making the whole document secret, just make a key to keep it secure&lt;br /&gt;
**The difficulty is in initially sharing this secret in a secure fashion&lt;br /&gt;
&lt;br /&gt;
=====Public-key Cryptography=====&lt;br /&gt;
&lt;br /&gt;
*But what if we don’t have a shared secret?&lt;br /&gt;
*Passwords aren’t good enough&lt;br /&gt;
*Websites don’t remember your password&lt;br /&gt;
**How would you send them your password in the first place (in a secure fashion)?&lt;br /&gt;
*Instead, we split the key into two parts: public key and private key&lt;br /&gt;
*In public key cryptography, you have two keys, a matched pair of public and private keys are opposite (inverters) because what you do with one can be undone with the other&lt;br /&gt;
**If you encrypt with a public key and decrypt with a private key, it’s a private one way communication&lt;br /&gt;
***For example, you can send the professor a secret message after downloading his public key and using it for encryption&lt;br /&gt;
**If you encrypt with a private key and decrypt with the public key, that’s a digital signature&lt;br /&gt;
*This is what is used for HTTPS&lt;br /&gt;
&lt;br /&gt;
===First Rule of Cryptography===&lt;br /&gt;
&lt;br /&gt;
*Friends don’t let friends implement their own cryptography, algorithms or code because it’s hard to do it and be sure you’re secure&lt;br /&gt;
*Why?&lt;br /&gt;
**You’ll miss protections against attacks. E.g. Timing attacks.&lt;br /&gt;
***In a timing attack, the attacker can extract secrets by watching execution time&lt;br /&gt;
***This exploits the fact that some numbers take longer to multiply than others&lt;br /&gt;
*Even professionally developped crypto systems will have bugs, so be prepared to update/replace all crypto-related code&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/analyzeLogs-ssl-template.zip analyzeLogs-ssl-template.zip]&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_18&amp;diff=20863</id>
		<title>WebFund 2016W Lecture 18</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_18&amp;diff=20863"/>
		<updated>2016-03-21T02:07:28Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 17, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec18-17Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In Class===&lt;br /&gt;
&lt;br /&gt;
(This is lecture 18, not 17!)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 18&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Symmetric cryptography&lt;br /&gt;
 - everyone uses the same key&lt;br /&gt;
 - think secret decoder rings&lt;br /&gt;
 - or, file encrypted with AES&lt;br /&gt;
 - secret has to be shared between sender and receiver&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
Public-key cryptography&lt;br /&gt;
 - but what if we don&#039;t have a shared secret?&lt;br /&gt;
   - your password isn&#039;t good enough, and&lt;br /&gt;
   - websites don&#039;t remember your password&lt;br /&gt;
   - and how would you send them your password in&lt;br /&gt;
     the first place&lt;br /&gt;
 - instead, we split the key into two parts&lt;br /&gt;
   - a public key&lt;br /&gt;
   - a private key&lt;br /&gt;
 - whatever one does, the other can undo&lt;br /&gt;
&lt;br /&gt;
 - if you encrypt with a public key and decrypt with a&lt;br /&gt;
   private key&lt;br /&gt;
     - private one-way communication&lt;br /&gt;
     - e.g., you download Anil&#039;s public key and send&lt;br /&gt;
       Anil a secret message&lt;br /&gt;
 - if you encrypt with a private key and decrypt with &lt;br /&gt;
   the public key&lt;br /&gt;
     - that&#039;s a digital signature&lt;br /&gt;
&lt;br /&gt;
First rule of cryptography&lt;br /&gt;
 - friends don&#039;t let friends implement their own crypto&lt;br /&gt;
 - algorithms OR code!&lt;br /&gt;
&lt;br /&gt;
Why?  You&#039;ll miss protections against attacks.&lt;br /&gt;
Example: timing attacks&lt;br /&gt;
 - can extract secrets by watching execution time&lt;br /&gt;
 - exploits the fact that some numbers take longer to&lt;br /&gt;
   multiply &amp;amp; other operations&lt;br /&gt;
&lt;br /&gt;
Everyone else messes up too&lt;br /&gt;
 - so be prepared to update/replace all crypto-related&lt;br /&gt;
   code&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Additional Notes on Openstack Instances====&lt;br /&gt;
&lt;br /&gt;
*When selecting a floating IP address, the subnet must correspond to the network that you chose for your instance i.e. If you pick comp2406-net-31 you should use a 134.117.31 address and if you pick comp2406-net-26, use a 134.117.26 address. Mismatching will result in an error&lt;br /&gt;
*&amp;lt;code&amp;gt;sudo setcap ‘cap_net_bind_service=rep’ /usr/bin/node.js&amp;lt;/code&amp;gt; is used to allow the user to bind to a privileged port&lt;br /&gt;
&lt;br /&gt;
====HTTPS====&lt;br /&gt;
&lt;br /&gt;
*HTTP is HyperText Transfer Protocol&lt;br /&gt;
*HTTPS is a secure version of HTTP which uses SSL/TLS&lt;br /&gt;
**SSL - Secure Sockets Layer&lt;br /&gt;
**TLS - Transport Layer Security&lt;br /&gt;
**The current version of SSL is TLS&lt;br /&gt;
**They are security protocols (cryptographic protocols) that encrypt communications to provide a secure connection over a network&lt;br /&gt;
*Normally in a URL, we don&#039;t see HTTP because it is the default so it is omitted&lt;br /&gt;
*With HTTPS, we will see the protocol written in the URL&lt;br /&gt;
*We also see a lock icon with HTTPS&lt;br /&gt;
**It means that the data you are sending is secure and the website you’re accessing has been digitally certified&lt;br /&gt;
**When you click on a lock icon, you can view information about the certificate that the server has&lt;br /&gt;
*If you want to enable SSL/TLS support in a Node application, you can change HTTP to HTTPS (change &amp;lt;code&amp;gt;var http = require(‘http’);&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;var http = require(‘https’);&amp;lt;/code&amp;gt; in bin/www)&lt;br /&gt;
**For the code to work, we have to specify some options (key and certificate) for the HTTPS server&lt;br /&gt;
*You can get it working by modifying the code:&lt;br /&gt;
&lt;br /&gt;
In bin/www:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
//use the app objects and put all the configurations into app, If require http is defined.&lt;br /&gt;
if (app.locals.sslOptions){&lt;br /&gt;
   var http = require(‘https’);&lt;br /&gt;
}&lt;br /&gt;
else{&lt;br /&gt;
   var http-require(‘http’);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//modify the ‘HTTP server’ part of your code with sslOptions.&lt;br /&gt;
//create an object in the app file.&lt;br /&gt;
if (app.locals.sslOptions){&lt;br /&gt;
   var server = http.createServer(app.locals.sslOptions, app);&lt;br /&gt;
}&lt;br /&gt;
else{&lt;br /&gt;
   var server = http.createServer(app);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In app.js, we specify the HTTPS options by loading the key and certificate files:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var fs = require(&#039;fs&#039;);&lt;br /&gt;
&lt;br /&gt;
//put this after app is defined but before it is exported&lt;br /&gt;
app.locals.sslOptions = {&lt;br /&gt;
   key: fs.readFileSync(‘keys/comp2406-test-key.pem’),&lt;br /&gt;
   cert: fs.readFileSync(‘keys/comp2406-test-cert.pem’)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Certificates=====&lt;br /&gt;
&lt;br /&gt;
*Certificates are a form of digital authentication for a web server&lt;br /&gt;
*They contain information about the server and the authority that has signed the certificate&lt;br /&gt;
*Companies such as GlobalSign and Verisign are certificate authorities which are relied upon to sign trustworthy certificates&lt;br /&gt;
*If you do not want to pay a trusted certificate authority to sign your server&#039;s certificate, you can sign it yourself&lt;br /&gt;
**This gives you a usable certificate but it will produce a warning when a browser communicates with your server since it will receive a certificate signed by an untrusted source&lt;br /&gt;
**In order to get our web application to work properly with SSL/TLS, we have to create a key and use it to sign a certificate&lt;br /&gt;
**An alternative to get a trusted certificate is to get a free temporary certificate (from Let&#039;s Encrypt) and update it by using a script to run it in order to obtain new ones upon expiry&lt;br /&gt;
&lt;br /&gt;
=====Symmetric Cryptography=====&lt;br /&gt;
&lt;br /&gt;
*With symmetric cryptography, everyone uses the same key i.e. if you want to send a secret message to another person, you have to have the same secret decoder ring or files encrypted with AES&lt;br /&gt;
**AES is a symmetric block cipher in which the same key is used to encrypt and decrypt a document. It is a block cipher because it encrypts data in blocks as opposed to the whole thing&lt;br /&gt;
*The key to symmetric cryptography is that the secret has to be shared between the sender and receiver to keep it a secret i.e. instead of making the whole document secret, just make a key to keep it secure&lt;br /&gt;
**The difficulty is in initially sharing this secret in a secure fashion&lt;br /&gt;
&lt;br /&gt;
=====Public-key Cryptography=====&lt;br /&gt;
&lt;br /&gt;
*But what if we don’t have a shared secret?&lt;br /&gt;
*Passwords aren’t good enough&lt;br /&gt;
*Websites don’t remember your password&lt;br /&gt;
**How would you send them your password in the first place (in a secure fashion)?&lt;br /&gt;
*Instead, we split the key into two parts: public key and private key&lt;br /&gt;
*In public key cryptography, you have two keys, a matched pair of public and private keys are opposite (inverters) because what you do with one can be undone with the other&lt;br /&gt;
**If you encrypt with a public key and decrypt with a private key, it’s a private one way communication&lt;br /&gt;
***For example, you can send the professor a secret message after downloading his public key and using it for encryption&lt;br /&gt;
**If you encrypt with a private key and decrypt with the public key, that’s a digital signature&lt;br /&gt;
&lt;br /&gt;
=====First Rule of Cryptography=====&lt;br /&gt;
&lt;br /&gt;
*Friends don’t let friends implement their own cryptography, algorithms or code because it’s hard to do it and be sure you’re secure&lt;br /&gt;
*Why?&lt;br /&gt;
**You’ll miss protections against attacks. E.g. Timing attacks.&lt;br /&gt;
***In a timing attack, the attacker can extract secrets by watching execution time&lt;br /&gt;
***This exploits the fact that some numbers take longer to multiply than others&lt;br /&gt;
*Even professionally developped crypto systems will have bugs, so be prepared to update/replace all crypto-related code&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/analyzeLogs-ssl-template.zip analyzeLogs-ssl-template.zip]&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_17&amp;diff=20848</id>
		<title>WebFund 2016W Lecture 17</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_17&amp;diff=20848"/>
		<updated>2016-03-19T19:49:37Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on March 15, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec17-15Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===Administrative===&lt;br /&gt;
&lt;br /&gt;
*Assignment 5 is due on March 24th&lt;br /&gt;
**The marking scheme has been updated for the assignment (now has 20 marks)&lt;br /&gt;
***2 for uploading logs&lt;br /&gt;
***4 for storing logs in MongoDB properly&lt;br /&gt;
***6 for listing entries in response to a query&lt;br /&gt;
***2 for allowing such listings to be downloaded IN THE ORDER they were in the original file&lt;br /&gt;
***6 for graphing daily frequency of logs&lt;br /&gt;
**This is worth 2 assignments since we now have less assignments&lt;br /&gt;
*Assignment 6 is due on April 7th (last day of class)&lt;br /&gt;
**This is worth 2 assignments since we have less assignments&lt;br /&gt;
*The solutions for our assignment 4 will be posted soon &lt;br /&gt;
*Solutions for assignments 5 &amp;amp; 6 will be posted by the end of the semester&lt;br /&gt;
&lt;br /&gt;
===Assignment 5===&lt;br /&gt;
&lt;br /&gt;
*The template we’re given should give you everything you need to start the assignment&lt;br /&gt;
*Each output type includes instructions with what needs to be displayed&lt;br /&gt;
**‘Download’ displays the log entries as text (similar to queryLogs in web application form)&lt;br /&gt;
**‘Show’ will have similar functionality to ‘Download’ but you’ll be putting the logs onto a text box you can scroll through&lt;br /&gt;
**‘Visualize’ can be done in a similar fashion to the graph tutorial&lt;br /&gt;
***Chart.js is a graphing script ([http://www.chartjs.org/docs/ http://www.chartjs.org/docs/]) that allows you to visualize your data&lt;br /&gt;
***For our assignment, we are setting up all of the data needed for the graph on the server and then passing it into the Jade template to be rendered into a script tag&lt;br /&gt;
***The code in the script tag will be run as client-side code in the web browser to build the graph&lt;br /&gt;
*In the &amp;lt;code&amp;gt;doQuery()&amp;lt;/code&amp;gt; function, you can see that our query types are listed here (visualize, show and download)&lt;br /&gt;
**For ‘Download’, you just send the logs as plain text&lt;br /&gt;
**For ‘Show’, we use the template given for Show.jade&lt;br /&gt;
**For ‘Visualize’, we specify the data as a result of calling &amp;lt;code&amp;gt;analyzeSelected()&amp;lt;/code&amp;gt;&lt;br /&gt;
***The &amp;lt;code&amp;gt;analyzeSelected()&amp;lt;/code&amp;gt; function has to take the logs and analyze them and to generate the values and counts&lt;br /&gt;
***To get the logs you need to implement the &amp;lt;code&amp;gt;getLogs()&amp;lt;/code&amp;gt; function which is included in the template&lt;br /&gt;
***The &amp;lt;code&amp;gt;analyzeSelected()&amp;lt;/code&amp;gt; function does produces a string of JavaScript code with the data object in JSON format&lt;br /&gt;
****This is passed in to the Visualize.jade template&lt;br /&gt;
&lt;br /&gt;
===Introduction to Instances===&lt;br /&gt;
&lt;br /&gt;
*Access the SCS OpenStack web portal at: [https://openstack.scs.carleton.ca/horizon/auth/login/?next=/horizon/ https://openstack.scs.carleton.ca/horizon/auth/login/?next=/horizon/]&lt;br /&gt;
*You can only access the SCS OpenStack machines when you’re on the Carleton Network (CU-Wireless)&lt;br /&gt;
**If you’re attempting to access OpenStack outside the network, you’ll need a VPN (Virtual Private Network) for Carleton’s network&lt;br /&gt;
*Log in using:&lt;br /&gt;
**Username: your MyCarletonOne username&lt;br /&gt;
**Password: your MyCarletonOne username (all lowercase) and your student number (no spaces between the two)&lt;br /&gt;
*To create/launch your own instance in the COMP2406 subcategory&lt;br /&gt;
**You’re going to have to use Google Chrome!&lt;br /&gt;
**Make sure you are in the COMP2406 project (You can change at the top of the page, to the right of the OpenStack logo)&lt;br /&gt;
**Go to Project → Compute → Instances&lt;br /&gt;
**Click the Launch Instance button to bring up the new instance interface&lt;br /&gt;
***You can name your own instance (ex// anilsomayaji-comp2406-1)&lt;br /&gt;
***Instance Boot Source : Boot from snapshot&lt;br /&gt;
***Instance Snapshot: COMP2406-W16&lt;br /&gt;
***Go to the Access &amp;amp; Security tab&lt;br /&gt;
****Check ping, ssh, https and default.&lt;br /&gt;
****Ignore the Key Pair option&lt;br /&gt;
***Go to the Networking tab&lt;br /&gt;
****Under Selected networks, add the “comp2406-net-26” available network (or “comp2406-net-31”)&lt;br /&gt;
***After following this procedure, you’ll be able to launch to create an instance!&lt;br /&gt;
**Find you instance on the list of instances and under the Actions menu, select Allocate Floating IP&lt;br /&gt;
***Select an IP address with a subnet matching the network that you joined (either 134.117.26.xx or 134.117.31.xx)&lt;br /&gt;
*Now you can connect to your instance via ssh&lt;br /&gt;
**In a terminal, enter &amp;lt;code&amp;gt;ssh [floating IP address] -l student&amp;lt;/code&amp;gt;&lt;br /&gt;
**The password is tneduts!&lt;br /&gt;
*You are now connected to your own virtual machine! You can use it to run a web server!&lt;br /&gt;
*Transfer a web application to the instance using wget or some FTP utility&lt;br /&gt;
*When running your server, instead of using localhost, we can use our IP address to access our server&lt;br /&gt;
**We only have access to port 443 for the server on this instance but we need elevated priviledges to use it&lt;br /&gt;
**TO FIX: &amp;lt;code&amp;gt;sudo setcap ‘cap_net_bind_service=+ep’ /user/bin/www/nodejs&amp;lt;/code&amp;gt;&lt;br /&gt;
*Now you can run your server with &amp;lt;code&amp;gt;PORT=443 bin/www&amp;lt;/code&amp;gt; (It should now be listening on port 443)&lt;br /&gt;
*Though all the capabilities of the OpenStack instance can be done on the course virtual machine, using OpenStack allows you to deploy Node.js in a production environment&lt;br /&gt;
&lt;br /&gt;
===Tutorial 7 - Downloading Files===&lt;br /&gt;
&lt;br /&gt;
*First we need to make a download button in account.jade&lt;br /&gt;
**Inside the each loop, we add a form to go along with each file&lt;br /&gt;
**We use a hidden field in the form to store the file name as a value to be sent as form data in the body of the POST request &lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
li #{s.name}: #{s.size} &lt;br /&gt;
   form (method=”post”, action=”/downloadFile”)&lt;br /&gt;
      p #{s.name}: #{s.size} &lt;br /&gt;
      input(type=”hidden”, value=s.name, name=”downloadFile”)&lt;br /&gt;
      input(type=”submit”, value=”Download”, name=”submit”)#{s.name}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Next, in index.js, we add the code to handle this new request&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;		&lt;br /&gt;
function downloadFile(req, res){&lt;br /&gt;
   var theFile = req.body.downloadFile;&lt;br /&gt;
   var allFiles = req.session.files;&lt;br /&gt;
   var i;&lt;br /&gt;
   var sent = false;&lt;br /&gt;
&lt;br /&gt;
   if(theFile){&lt;br /&gt;
      for(i = 0; i &amp;lt; allFiles.length; i++){&lt;br /&gt;
         if(allFiles[i].originalname === theFile){&lt;br /&gt;
            response = Buffer(allFiles[i].buffer.data).toString(‘utf8’);&lt;br /&gt;
            res.send(response);	&lt;br /&gt;
            sent = true;&lt;br /&gt;
            break;&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      if(!sent)&lt;br /&gt;
         res.send(“File not found”);&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
		   &lt;br /&gt;
router.post(“/downloadFile”, downloadFile);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*The &amp;lt;code&amp;gt;downloadFile()&amp;lt;/code&amp;gt; searches through the array of uploaded files from the &amp;lt;code&amp;gt;req.session&amp;lt;/code&amp;gt; object for a file whose name matches the name received in the request&lt;br /&gt;
**If such a file is found, it is sent in the response&lt;br /&gt;
**Note the use of the &amp;lt;code&amp;gt;Buffer()&amp;lt;/code&amp;gt; constructor&lt;br /&gt;
***This is to get a round a bug where the file buffer was not preserved in its Buffer format during storage in memory&lt;br /&gt;
&lt;br /&gt;
==Instructor comments==&lt;br /&gt;
&lt;br /&gt;
In class I had trouble with downloading the file.  Everything worked except that the downloaded file became [Object object].&lt;br /&gt;
&lt;br /&gt;
The problematic line was this, in downloadFile()&lt;br /&gt;
&lt;br /&gt;
  response = allFiles[i].buffer.toString(&#039;utf8&#039;);&lt;br /&gt;
&lt;br /&gt;
This line &#039;&#039;&#039;should&#039;&#039;&#039; work because when we stored the file in uploadText() it did.  But in between (perhaps when stored as a session property) it was changed.&lt;br /&gt;
&lt;br /&gt;
Before, we had:&lt;br /&gt;
&lt;br /&gt;
  { &#039;0&#039;: 123,&lt;br /&gt;
    &#039;1&#039;: 10,&lt;br /&gt;
    &#039;2&#039;: 32,&lt;br /&gt;
    &#039;3&#039;: 32,&lt;br /&gt;
    ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
But now this got changed into:&lt;br /&gt;
&lt;br /&gt;
  { type: &#039;Buffer&#039;,&lt;br /&gt;
    data: &lt;br /&gt;
     [ 123,&lt;br /&gt;
       10,&lt;br /&gt;
       32,&lt;br /&gt;
       32,&lt;br /&gt;
       ...&lt;br /&gt;
     ]&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The former is of type Buffer (as reported by Buffer.isBuffer()), the latter is not.&lt;br /&gt;
&lt;br /&gt;
While this is strange behavior, the fix is easy:&lt;br /&gt;
&lt;br /&gt;
  response = Buffer(allFiles[i].buffer.data).toString(&#039;utf8&#039;);&lt;br /&gt;
&lt;br /&gt;
This is definitely a bug in node or express.  If anyone wants to try isolating this bug and reporting it, let me know!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/upload-demo-class.zip upload-demo with working download button]&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_17&amp;diff=20847</id>
		<title>WebFund 2016W Lecture 17</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_17&amp;diff=20847"/>
		<updated>2016-03-19T19:48:15Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on March 15, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec17-15Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===Administrative===&lt;br /&gt;
&lt;br /&gt;
*Assignment 5 is due on March 24th&lt;br /&gt;
**The marking scheme has been updated for the assignment (now has 20 marks)&lt;br /&gt;
***2 for uploading logs&lt;br /&gt;
***4 for storing logs in MongoDB properly&lt;br /&gt;
***6 for listing entries in response to a query&lt;br /&gt;
***2 for allowing such listings to be downloaded IN THE ORDER they were in the original file&lt;br /&gt;
***6 for graphing daily frequency of logs&lt;br /&gt;
**This is worth 2 assignments since we now have less assignments&lt;br /&gt;
*Assignment 6 is due on April 7th (last day of class)&lt;br /&gt;
**This is worth 2 assignments since we have less assignments&lt;br /&gt;
*The solutions for our assignment 4 will be posted soon &lt;br /&gt;
*Solutions for assignments 5 &amp;amp; 6 will be posted by the end of the semester&lt;br /&gt;
&lt;br /&gt;
===Assignment 5===&lt;br /&gt;
&lt;br /&gt;
*The template we’re given should give you everything you need to start the assignment&lt;br /&gt;
*Each output type includes instructions with what needs to be displayed&lt;br /&gt;
**‘Download’ displays the log entries as text (similar to queryLogs in web application form)&lt;br /&gt;
**‘Show’ will have similar functionality to ‘Download’ but you’ll be putting the logs onto a text box you can scroll through&lt;br /&gt;
**‘Visualize’ can be done in a similar fashion to the graph tutorial&lt;br /&gt;
***Chart.js is a graphing script ([http://www.chartjs.org/docs/ http://www.chartjs.org/docs/]) that allows you to visualize your data&lt;br /&gt;
***For our assignment, we are setting up all of the data needed for the graph on the server and then passing it into the Jade template to be rendered into a script tag&lt;br /&gt;
***The code in the script tag will be run as client-side code in the web browser to build the graph&lt;br /&gt;
*In the &amp;lt;code&amp;gt;doQuery()&amp;lt;/code&amp;gt; function, you can see that our query types are listed here (visualize, show and download)&lt;br /&gt;
**For ‘Download’, you just send the logs as plain text&lt;br /&gt;
**For ‘Show’, we use the template given for Show.jade&lt;br /&gt;
**For ‘Visualize’, we specify the data as a result of calling &amp;lt;code&amp;gt;analyzeSelected()&amp;lt;/code&amp;gt;&lt;br /&gt;
***The &amp;lt;code&amp;gt;analyzeSelected()&amp;lt;/code&amp;gt; function has to take the logs and analyze them and to generate the values and counts&lt;br /&gt;
***To get the logs you need to implement the &amp;lt;code&amp;gt;getLogs()&amp;lt;/code&amp;gt; function which is included in the template&lt;br /&gt;
***The &amp;lt;code&amp;gt;analyzeSelected()&amp;lt;/code&amp;gt; function does produces a string of JavaScript code with the data object in JSON format&lt;br /&gt;
****This is passed in to the Visualize.jade template&lt;br /&gt;
&lt;br /&gt;
===Introduction to Instances===&lt;br /&gt;
&lt;br /&gt;
*Access the SCS OpenStack web portal at: [https://openstack.scs.carleton.ca/horizon/auth/login/?next=/horizon/ https://openstack.scs.carleton.ca/horizon/auth/login/?next=/horizon/]&lt;br /&gt;
*You can only access the SCS OpenStack machines when you’re on the Carleton Network (CU-Wireless)&lt;br /&gt;
**If you’re attempting to access OpenStack outside the network, you’ll need a VPN (Virtual Private Network) for Carleton’s network&lt;br /&gt;
*Log in using:&lt;br /&gt;
**Username: your MyCarletonOne username&lt;br /&gt;
**Password: your MyCarletonOne username (all lowercase) and your student number (no spaces between the two)&lt;br /&gt;
*To create/launch your own instance in the COMP2406 subcategory&lt;br /&gt;
**You’re going to have to use Google Chrome!&lt;br /&gt;
**Make sure you are in the COMP2406 project (You can change at the top of the page, to the right of the OpenStack logo)&lt;br /&gt;
**Go to Project → Compute → Instances&lt;br /&gt;
**Click the Launch Instance button to bring up the new instance interface&lt;br /&gt;
***You can name your own instance (ex// anilsomayaji-comp2406-1)&lt;br /&gt;
***Instance Boot Source : Boot from snapshot&lt;br /&gt;
***Instance Snapshot: COMP2406-W16&lt;br /&gt;
***Go to the Access &amp;amp; Security tab&lt;br /&gt;
****Check ping, ssh, https and default.&lt;br /&gt;
****Ignore the Key Pair option&lt;br /&gt;
***Go to the Networking tab&lt;br /&gt;
****Under Selected networks, add the “comp2406-net-26” available network (or “comp2406-net-31”)&lt;br /&gt;
***After following this procedure, you’ll be able to launch to create an instance!&lt;br /&gt;
**Find you instance on the list of instances and under the Actions menu, select Allocate Floating IP&lt;br /&gt;
***Select an IP address with a subnet matching the network that you joined (either 134.117.26.xx or 134.117.31.xx)&lt;br /&gt;
*Now you can connect to your instance via ssh&lt;br /&gt;
**In a terminal, enter &amp;lt;code&amp;gt;ssh [floating IP address] -l student&amp;lt;/code&amp;gt;&lt;br /&gt;
**The password is tneduts!&lt;br /&gt;
*You are now connected to your own virtual machine! You can use it to run a web server!&lt;br /&gt;
*Transfer a web application to the instance using wget or some FTP utility&lt;br /&gt;
*When running your server, instead of using localhost, we can use our IP address to access our server&lt;br /&gt;
**We only have access to port 443 for the server on this instance but we need elevated priviledges to use it&lt;br /&gt;
**TO FIX: &amp;lt;code&amp;gt;sudo setcap ‘cap_net_bind_service=+ep’ /user/bin/www/nodejs&amp;lt;/code&amp;gt;&lt;br /&gt;
*Now you can run your server with &amp;lt;code&amp;gt;PORT=443 bin/www&amp;lt;/code&amp;gt; (It should now be listening on port 443)&lt;br /&gt;
*Though all the capabilities of the OpenStack instance can be done on the course virtual machine, using OpenStack allows you to deploy Node.js in a production environment&lt;br /&gt;
&lt;br /&gt;
===Tutorial 7 - Downloading Files===&lt;br /&gt;
&lt;br /&gt;
*First we need to make a download button in account.jade&lt;br /&gt;
**Inside the each loop, we add a form to go along with each file&lt;br /&gt;
**We use a hidden field in the form to store the file name as a value to be sent as form data in the body of the POST request &lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
li #{s.name}: #{s.size} &lt;br /&gt;
   form (method=”post”, action=”/downloadFile”)&lt;br /&gt;
      p #{s.name}: #{s.size} &lt;br /&gt;
      input(type=”hidden”, value=s.name, name=”downloadFile”)&lt;br /&gt;
      input(type=”submit”, value=”Download”, name=”submit”)#{s.name}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Next, in index.js, we add the code to handle this new request&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;		&lt;br /&gt;
function downloadFile(req, res){&lt;br /&gt;
   var theFile = req.body.downloadFile;&lt;br /&gt;
   var allFiles = req.session.files;&lt;br /&gt;
   var i;&lt;br /&gt;
   var sent = false;&lt;br /&gt;
&lt;br /&gt;
   if(theFile){&lt;br /&gt;
      for(i = 0; i &amp;lt; allFiles.length; i++){&lt;br /&gt;
         if(allFiles[i].originalname === theFile){&lt;br /&gt;
            response = Buffer(allFiles[i].buffer.data).toString(‘utf8’);&lt;br /&gt;
            res.send(response);	&lt;br /&gt;
            sent = true;&lt;br /&gt;
            break;&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      if(!sent)&lt;br /&gt;
         res.send(“File not found”);&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
		   &lt;br /&gt;
router.post(“/downloadFile”, downloadFile);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*The &amp;lt;code&amp;gt;downloadFile()&amp;lt;/code&amp;gt; searches through the array of uploaded files from the &amp;lt;code&amp;gt;req.session&amp;lt;/code&amp;gt; object for a file whose name matches the name received in the request&lt;br /&gt;
**If such a file is found, it is sent in the response&lt;br /&gt;
**Note the use of the &amp;lt;code&amp;gt;Buffer()&amp;lt;/code&amp;gt; constructor&lt;br /&gt;
***This is to get a round a bug where the file buffer was not preserved in its Buffer format during storage in memory&lt;br /&gt;
&lt;br /&gt;
===Instructor comments===&lt;br /&gt;
&lt;br /&gt;
In class I had trouble with downloading the file.  Everything worked except that the downloaded file became [Object object].&lt;br /&gt;
&lt;br /&gt;
The problematic line was this, in downloadFile()&lt;br /&gt;
&lt;br /&gt;
  response = allFiles[i].buffer.toString(&#039;utf8&#039;);&lt;br /&gt;
&lt;br /&gt;
This line &#039;&#039;&#039;should&#039;&#039;&#039; work because when we stored the file in uploadText() it did.  But in between (perhaps when stored as a session property) it was changed.&lt;br /&gt;
&lt;br /&gt;
Before, we had:&lt;br /&gt;
&lt;br /&gt;
  { &#039;0&#039;: 123,&lt;br /&gt;
    &#039;1&#039;: 10,&lt;br /&gt;
    &#039;2&#039;: 32,&lt;br /&gt;
    &#039;3&#039;: 32,&lt;br /&gt;
    ...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
But now this got changed into:&lt;br /&gt;
&lt;br /&gt;
  { type: &#039;Buffer&#039;,&lt;br /&gt;
    data: &lt;br /&gt;
     [ 123,&lt;br /&gt;
       10,&lt;br /&gt;
       32,&lt;br /&gt;
       32,&lt;br /&gt;
       ...&lt;br /&gt;
     ]&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
The former is of type Buffer (as reported by Buffer.isBuffer()), the latter is not.&lt;br /&gt;
&lt;br /&gt;
While this is strange behavior, the fix is easy:&lt;br /&gt;
&lt;br /&gt;
  response = Buffer(allFiles[i].buffer.data).toString(&#039;utf8&#039;);&lt;br /&gt;
&lt;br /&gt;
This is definitely a bug in node or express.  If anyone wants to try isolating this bug and reporting it, let me know!&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/upload-demo-class.zip upload-demo with working download button]&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_15&amp;diff=20825</id>
		<title>WebFund 2016W Lecture 15</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_15&amp;diff=20825"/>
		<updated>2016-03-14T18:37:08Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on March 8, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec15-08Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
*Midterms are all graded (go check out CULearn)&lt;br /&gt;
*Midterms will be available during tutorials&lt;br /&gt;
&lt;br /&gt;
===Client-Side Data Population===&lt;br /&gt;
&lt;br /&gt;
*On the midterm, many people got the last question about the Jade each loop wrong&lt;br /&gt;
**The loop runs on the server, not in the browser&lt;br /&gt;
**We can look at the source code in the web browser and see that the table data is all populated&lt;br /&gt;
***To make the Jade-rendered HTML display nicely (with proper line break and indentations), pass the &amp;quot;pretty&amp;quot; attribute to the template:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;res.render(‘list’, { title: ‘People listing’, items: state, pretty: true});&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In previous examples we built the table rows using a for each loop in the Jade template. Today we will be moving the functionality to the web browser/client. This will allow our page to be more dynamic in the sense that the table will be rendered after the page has loaded. How does this happen? The list of people (in JSON) is requested from the server using an AJAX request and is then parsed and then put into the table.&lt;br /&gt;
&lt;br /&gt;
*To do this we need to:&lt;br /&gt;
**Get the data to the browser&lt;br /&gt;
**Make the list from the data (using client-side JavaScript)&lt;br /&gt;
&lt;br /&gt;
====Sending List Data to the Browser====&lt;br /&gt;
&lt;br /&gt;
*Add a new route on the server&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;app.get(‘/items’, function(req, res){&lt;br /&gt;
   res.send(state);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*This will send JSON-formatted data (the array) to the requesting client (the web browser)&lt;br /&gt;
**We will need to use jQuery (client-side JavaScript) to make this request and handle the response&lt;br /&gt;
***We&#039;ll write the script for this later&lt;br /&gt;
*We then change the /list route to render a new Jade template&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
app.get(‘/list’, function(req, res){&lt;br /&gt;
   res.render(‘clientlist’, {title: “People listing”, pretty: true});&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====clientlist.jade=====&lt;br /&gt;
&lt;br /&gt;
*We can copy the list.jade template and make some modifications to it&lt;br /&gt;
*We will need to link jQuery and the client-side script that we will be writting&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
block header&lt;br /&gt;
   script(src=“jquery-1.12.0.js”)&lt;br /&gt;
   script(src=“showlist.js”)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*We can remove the body of the old table and instead use&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
tbody#theListing 	(the pound sign indicates an ID)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*We need to give an ID to the tbody because in showlist.js we will need to let it know where we want to add content to the table&lt;br /&gt;
&lt;br /&gt;
====Processing the Data in the Browser====&lt;br /&gt;
&lt;br /&gt;
*Now we need to make the showlist.js script&lt;br /&gt;
*In this script, we will need to get get the array of objects (in JSON) from the server by making an AJAX request&lt;br /&gt;
**We can use &amp;lt;code&amp;gt;$.getJSON(‘/items’, displayItems);&amp;lt;/code&amp;gt; to make this request and then call the &amp;lt;code&amp;gt;displayItems&amp;lt;/code&amp;gt; callback function&lt;br /&gt;
***What is AJAX?&lt;br /&gt;
****It stands for Asynchronous JavaScript and XML&lt;br /&gt;
****It’s a library that communicates between the server and the client. It’s great because of it’s asynchronous nature which means it can do its work without needing to refresh the page.&lt;br /&gt;
**Now we have an array of objects in the browser (the data has moved!)&lt;br /&gt;
**We need to display this data&lt;br /&gt;
**Once displayed they are still not in the source code! (We have updated the DOM but not the source code)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
$(function(){&lt;br /&gt;
   //grab JSON from server&lt;br /&gt;
   //insert data into DOM&lt;br /&gt;
&lt;br /&gt;
   function displayItems(items){&lt;br /&gt;
      var i;&lt;br /&gt;
      for ( i = 0; i &amp;lt; items.length; i++){&lt;br /&gt;
         //Remember the id? This is DOM manipulation&lt;br /&gt;
         $(“#theListing”).append(“&amp;lt;tr&amp;gt;” +&lt;br /&gt;
         “&amp;lt;td&amp;gt;” + items[i].name + “&amp;lt;/td&amp;gt;” +&lt;br /&gt;
         “&amp;lt;td&amp;gt;” + items[i].city + “&amp;lt;/td&amp;gt;” +&lt;br /&gt;
         “&amp;lt;td&amp;gt;” + items[i].country + “&amp;lt;/td&amp;gt;” +&lt;br /&gt;
         “&amp;lt;td&amp;gt;” + items[i].birthday + “&amp;lt;/td&amp;gt;” +&lt;br /&gt;
         “&amp;lt;td&amp;gt;” + items[i].email + “&amp;lt;/td&amp;gt;” +&lt;br /&gt;
         “&amp;lt;/tr&amp;gt;”); &lt;br /&gt;
      }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   $.getJSON(‘/items’, displayItems); //callback&lt;br /&gt;
   // This is the AJAX request (as mentioned above)&lt;br /&gt;
   // We are receiving the array of objects and calling the displayItems call back function.&lt;br /&gt;
   // “items” is the array of objects we got from the server&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Tutorial 6 Solution===&lt;br /&gt;
&lt;br /&gt;
*Make sure you complete this tutorial (super important for next assignment)&lt;br /&gt;
*We will walk through it today…&lt;br /&gt;
*The code already has some saved data it’s displaying&lt;br /&gt;
*The /results route passes the stats object in the the Jade template (we need to build up this stats object dynamically)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
router.post(‘/add’, function(req, res){&lt;br /&gt;
   var answer = { firstname : req.body.firstname};&lt;br /&gt;
	&lt;br /&gt;
   // qlist holds all of the answers to the question (ex. for colours if would have “red”, “green”, etc)&lt;br /&gt;
   var qlist = Object.keys(questions);&lt;br /&gt;
	&lt;br /&gt;
   // for each answer to the question&lt;br /&gt;
   qlist.forEach(function(q){&lt;br /&gt;
      answer[q] = req.body[q];&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   answers.push(answer);&lt;br /&gt;
&lt;br /&gt;
   …the rest is the same…&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Now we need to gather the answers into a stats object&lt;br /&gt;
*When making an object, start with the properties you know it needs to have and add the rest later&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function calcResultsStats(results){&lt;br /&gt;
   var resultsStats = [];&lt;br /&gt;
&lt;br /&gt;
   Object.keys(questions).forEach(function(q){&lt;br /&gt;
      var r = {};&lt;br /&gt;
      var i;&lt;br /&gt;
      var choice;&lt;br /&gt;
&lt;br /&gt;
      r.questionsName = q;&lt;br /&gt;
      r.questionText = questions[q];&lt;br /&gt;
      r.labels = [];&lt;br /&gt;
      r.values = [];&lt;br /&gt;
&lt;br /&gt;
      var count = {}; // mapping of question answers to counts&lt;br /&gt;
&lt;br /&gt;
      for (i  = 0; i &amp;lt; answers.length; i++){&lt;br /&gt;
         choice = answers[i][q];&lt;br /&gt;
&lt;br /&gt;
         if( counts[choice])&lt;br /&gt;
            counts[choice] = counts[choice] + 1;&lt;br /&gt;
         else&lt;br /&gt;
            counts[choice] = 1;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      // Now we want to convert that object into two arrays&lt;br /&gt;
      Object.keys(counts).forEach(function(c){&lt;br /&gt;
         r.labels.push(c);&lt;br /&gt;
         r.values.push(counts[c]);&lt;br /&gt;
      });&lt;br /&gt;
&lt;br /&gt;
      resultsStats.push(r);&lt;br /&gt;
   });&lt;br /&gt;
&lt;br /&gt;
   return resultsStats;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===How Real Is All of This?===&lt;br /&gt;
&lt;br /&gt;
*While the web app was running during class, you could visit http://134.117.222.110:3000/&lt;br /&gt;
**It’s a real web server!&lt;br /&gt;
*What we don&#039;t have is a domain name (we must use the IP address instead)&lt;br /&gt;
**Root Name Servers:&lt;br /&gt;
***Canada (.ca)&lt;br /&gt;
***Carleton (carleton.ca)&lt;br /&gt;
***SCS (scs.carleton.ca)&lt;br /&gt;
***Anil (homeostatis.scs.carleton.ca)&lt;br /&gt;
*To have everything set up for you, you can use a PaaS (Platform as a Service)&lt;br /&gt;
**Node.js Developer Centre:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
nam install azure&lt;br /&gt;
git commit -m “My first Node app”&lt;br /&gt;
git push azure master&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Modulus:&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
nam install modulus&lt;br /&gt;
modulus deploy&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Next time:&lt;br /&gt;
**Get Node running on OpenStack&lt;br /&gt;
**You won’t have to run a VM&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
Full code:&lt;br /&gt;
* [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/examforms-jquery.zip examforms-jquery.zip]&lt;br /&gt;
* [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/graph-demo-sol.zip graph-demo-sol.zip]&lt;br /&gt;
&lt;br /&gt;
===examforms-jquery/examforms-jquery.js===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
var http = require(&#039;http&#039;);&lt;br /&gt;
var express = require(&#039;express&#039;);&lt;br /&gt;
var bodyParser = require(&#039;body-parser&#039;);&lt;br /&gt;
var logger = require(&#039;morgan&#039;);&lt;br /&gt;
var port = 3000;&lt;br /&gt;
var state = [];&lt;br /&gt;
&lt;br /&gt;
var app = express();&lt;br /&gt;
&lt;br /&gt;
app.set(&#039;view engine&#039;, &#039;jade&#039;);&lt;br /&gt;
app.set(&#039;views&#039;, __dirname);&lt;br /&gt;
app.use(logger(&#039;dev&#039;));&lt;br /&gt;
app.use(bodyParser.urlencoded({ extended: false }));&lt;br /&gt;
&lt;br /&gt;
app.use(express.static(&#039;.&#039;));&lt;br /&gt;
&lt;br /&gt;
app.get(&#039;/&#039;, function(req, res, next) {&lt;br /&gt;
    res.render(&#039;index&#039;, { title: &#039;COMP 2406 Exam form demo&#039; });&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
app.post(&#039;/add&#039;, function(req, res) {&lt;br /&gt;
    var obj = { name: req.body.name,&lt;br /&gt;
		city: req.body.city,&lt;br /&gt;
                country: req.body.country,&lt;br /&gt;
                birthday: req.body.birthday,&lt;br /&gt;
                email: req.body.email };&lt;br /&gt;
    state.push(obj);&lt;br /&gt;
    res.redirect(&#039;/list&#039;);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
app.get(&#039;/list&#039;, function(req, res) {&lt;br /&gt;
    // res.render(&#039;list&#039;, { title: &#039;People Listing&#039;,  items: state,&lt;br /&gt;
    // 			 pretty: true});&lt;br /&gt;
    res.render(&#039;clientlist&#039;, { title: &#039;People Listing&#039;, pretty: true});&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
app.get(&#039;/items&#039;, function(req, res) {&lt;br /&gt;
    res.send(state);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
var serverUp = function() {&lt;br /&gt;
    console.log(&amp;quot;ExamForms listening on port &amp;quot; + port);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var serverDown = function() {&lt;br /&gt;
    console.log(&amp;quot;Server shutting down.&amp;quot;);&lt;br /&gt;
    process.exit(0);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var server = http.createServer(app);&lt;br /&gt;
server.listen(port);&lt;br /&gt;
server.on(&#039;listening&#039;, serverUp);&lt;br /&gt;
process.on(&#039;SIGINT&#039;, serverDown);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===examforms-jquery/clientlist.jade===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot; line&amp;gt;&lt;br /&gt;
extends layout&lt;br /&gt;
  &lt;br /&gt;
block header&lt;br /&gt;
    script(src=&amp;quot;jquery-1.12.0.js&amp;quot;)&lt;br /&gt;
    script(src=&amp;quot;showlist.js&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
block content&lt;br /&gt;
    h1= title&lt;br /&gt;
&lt;br /&gt;
    div&lt;br /&gt;
      div&lt;br /&gt;
      table&lt;br /&gt;
        thead&lt;br /&gt;
          th Name&lt;br /&gt;
          th City&lt;br /&gt;
          th Country&lt;br /&gt;
          th Birthday&lt;br /&gt;
          th Email&lt;br /&gt;
        tbody#theListing&lt;br /&gt;
&lt;br /&gt;
    form(method=&amp;quot;get&amp;quot;, action=&amp;quot;/&amp;quot;)&lt;br /&gt;
      button(type=&amp;quot;submit&amp;quot;) Home&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===examforms-jquery/showlist.js===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
$(function() {&lt;br /&gt;
    // grab JSON from server&lt;br /&gt;
    // insert data into DOM&lt;br /&gt;
&lt;br /&gt;
    function displayItems(items) {&lt;br /&gt;
	var i;&lt;br /&gt;
&lt;br /&gt;
	for (i=0; i&amp;lt;items.length; i++) {&lt;br /&gt;
	    $(&amp;quot;#theListing&amp;quot;).append(&amp;quot;&amp;lt;tr&amp;gt;&amp;quot; +&lt;br /&gt;
				    &amp;quot;&amp;lt;td&amp;gt;&amp;quot; + items[i].name + &amp;quot;&amp;lt;/td&amp;gt;&amp;quot; +&lt;br /&gt;
				    &amp;quot;&amp;lt;td&amp;gt;&amp;quot; + items[i].city + &amp;quot;&amp;lt;/td&amp;gt;&amp;quot; +&lt;br /&gt;
				    &amp;quot;&amp;lt;td&amp;gt;&amp;quot; + items[i].country + &amp;quot;&amp;lt;/td&amp;gt;&amp;quot; +&lt;br /&gt;
				    &amp;quot;&amp;lt;td&amp;gt;&amp;quot; + items[i].birthday + &amp;quot;&amp;lt;/td&amp;gt;&amp;quot; +&lt;br /&gt;
				    &amp;quot;&amp;lt;td&amp;gt;&amp;quot; + items[i].email + &amp;quot;&amp;lt;/td&amp;gt;&amp;quot; +&lt;br /&gt;
				    &amp;quot;&amp;lt;/tr&amp;gt;&amp;quot;&lt;br /&gt;
				   );&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    $.getJSON(&#039;/items&#039;, displayItems);&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===graph-demo/routes/index.js===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
var express = require(&#039;express&#039;);&lt;br /&gt;
var router = express.Router();&lt;br /&gt;
&lt;br /&gt;
var answers = [];&lt;br /&gt;
var questions = {&lt;br /&gt;
    color: &amp;quot;Favorite color?&amp;quot;,&lt;br /&gt;
    day: &amp;quot;Favorite day of the week?&amp;quot;,&lt;br /&gt;
    dog: &amp;quot;Favorite dog breed?&amp;quot;,&lt;br /&gt;
    city: &amp;quot;Favorite city?&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function calcResultsStats(results) {&lt;br /&gt;
    // var resultsStats = [&lt;br /&gt;
    // 	{questionName: &amp;quot;color&amp;quot;,&lt;br /&gt;
    // 	 questionText: &amp;quot;Favorite color?&amp;quot;,&lt;br /&gt;
    // 	 labels: [&amp;quot;red&amp;quot;, &amp;quot;blue&amp;quot;, &amp;quot;yellow&amp;quot;, &amp;quot;brown&amp;quot;],&lt;br /&gt;
    // 	 values: [5, 7, 2, 0]&lt;br /&gt;
    // 	},&lt;br /&gt;
    // 	{questionName: &amp;quot;day&amp;quot;,&lt;br /&gt;
    // 	 questionText: &amp;quot;Favorite day of the week?&amp;quot;,&lt;br /&gt;
    // 	 labels: [&amp;quot;Monday&amp;quot;, &amp;quot;Tuesday&amp;quot;, &amp;quot;Wednesday&amp;quot;, &amp;quot;Thursday&amp;quot;, &amp;quot;Friday&amp;quot;,&lt;br /&gt;
    // 		  &amp;quot;Saturday&amp;quot;, &amp;quot;Sunday&amp;quot;],&lt;br /&gt;
    // 	 values: [0, 2, 1, 5, 70, 100, 20]&lt;br /&gt;
    // 	}&lt;br /&gt;
    // ];&lt;br /&gt;
&lt;br /&gt;
    var resultsStats = [];&lt;br /&gt;
&lt;br /&gt;
    Object.keys(questions).forEach(function(q) {&lt;br /&gt;
	var r = {};&lt;br /&gt;
	var i, choice;&lt;br /&gt;
	r.questionName = q;&lt;br /&gt;
	r.questionText = questions[q];&lt;br /&gt;
	r.labels = [];&lt;br /&gt;
	r.values = [];&lt;br /&gt;
	var counts = {}; // mapping of question answers to counts&lt;br /&gt;
	&lt;br /&gt;
	for (i = 0; i&amp;lt;answers.length; i++) {&lt;br /&gt;
	    choice = answers[i][q];&lt;br /&gt;
	    if (counts[choice]) {&lt;br /&gt;
		counts[choice] = counts[choice] + 1;&lt;br /&gt;
	    } else {&lt;br /&gt;
		counts[choice] = 1;&lt;br /&gt;
	    }&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	Object.keys(counts).forEach(function(c) {&lt;br /&gt;
	    r.labels.push(c);&lt;br /&gt;
	    r.values.push(counts[c]);&lt;br /&gt;
	});&lt;br /&gt;
	resultsStats.push(r);&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    return resultsStats;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
router.get(&#039;/&#039;, function(req, res) {&lt;br /&gt;
    res.render(&#039;index&#039;, {title: &#039;COMP 2406 Graphing Demo&#039;,&lt;br /&gt;
			 questions: questions});&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
router.post(&#039;/add&#039;, function(req, res) {&lt;br /&gt;
    var answer = { firstname: req.body.firstname};&lt;br /&gt;
    &lt;br /&gt;
    var qlist = Object.keys(questions);&lt;br /&gt;
&lt;br /&gt;
    qlist.forEach(function(q) {&lt;br /&gt;
	answer[q] = req.body[q];&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    answers.push(answer);&lt;br /&gt;
&lt;br /&gt;
    console.log(answer);&lt;br /&gt;
&lt;br /&gt;
    res.redirect(&#039;/results&#039;);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
router.get(&#039;/results&#039;, function(req, res) {&lt;br /&gt;
    var stats = calcResultsStats(answers);&lt;br /&gt;
    &lt;br /&gt;
    res.render(&#039;results&#039;, { title: &#039;Survey Results&#039;,&lt;br /&gt;
			    stats: stats,&lt;br /&gt;
			    barwidth: 60&lt;br /&gt;
			  });&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
// app.get(&#039;/render&#039;, function (req, res, next) {&lt;br /&gt;
//     res.render(&#039;chart&#039;, {xlabel: &amp;quot;Months&amp;quot;,&lt;br /&gt;
//                          ylabel: &amp;quot;Failure rate (%)&amp;quot;,&lt;br /&gt;
//                          unit: &#039;%&#039;,&lt;br /&gt;
//                          barwidth: 40,&lt;br /&gt;
// 			 pretty: true,&lt;br /&gt;
//                          items: [{value: 10, bin: &amp;quot;Jan&amp;quot;},&lt;br /&gt;
//                                  {value: 20, bin: &amp;quot;Feb&amp;quot;},&lt;br /&gt;
//                                  {value: 40, bin: &amp;quot;Mar&amp;quot;},&lt;br /&gt;
//                                  {value: 40, bin: &amp;quot;Apr&amp;quot;},&lt;br /&gt;
//                                  {value: 40, bin: &amp;quot;May&amp;quot;},&lt;br /&gt;
//                                  {value: 40, bin: &amp;quot;Jun&amp;quot;},&lt;br /&gt;
//                                  {value: 40, bin: &amp;quot;Jul&amp;quot;},&lt;br /&gt;
//                                  {value: 40, bin: &amp;quot;Aug&amp;quot;},&lt;br /&gt;
//                                  {value: 40, bin: &amp;quot;Sep&amp;quot;},&lt;br /&gt;
//                                  {value: 40, bin: &amp;quot;Oct&amp;quot;},&lt;br /&gt;
//                                  {value: 80, bin: &amp;quot;Nov&amp;quot;},&lt;br /&gt;
//                                  {value: 100, bin: &amp;quot;Dec&amp;quot;},&lt;br /&gt;
//                                 ]});&lt;br /&gt;
// });&lt;br /&gt;
&lt;br /&gt;
module.exports = router;&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_16&amp;diff=20818</id>
		<title>WebFund 2016W Lecture 16</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_16&amp;diff=20818"/>
		<updated>2016-03-13T00:09:56Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 10, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec16-10Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===Exam-forms Vulnerabilities===&lt;br /&gt;
&lt;br /&gt;
*The current version of this application is vulnerable to a cross site scripting attack&lt;br /&gt;
*What is a cross site scripting attack?&lt;br /&gt;
**The user supplies data that is actually code which gets interpreted by the browser&lt;br /&gt;
**Example from class: &amp;lt;code&amp;gt;&amp;lt;script&amp;gt;alert(“hi there”)&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt; is typed into the city field of the form and causes a popup displaying “hi there”&lt;br /&gt;
*How is this different from SQL injection?&lt;br /&gt;
**With an SQL injection, code gets interpreted by a back-end database&lt;br /&gt;
*Input must be sanitized to prevent these types of attacks&lt;br /&gt;
*How can we do this?&lt;br /&gt;
**HTML characters can be escaped so that any HTML code in the input gets transformed to be interpreted purely as a string and displayed in its original format&lt;br /&gt;
&lt;br /&gt;
====jQuery Version====&lt;br /&gt;
&lt;br /&gt;
*Problem: takes a raw string and puts it in between tags (this is bad) so it gets interpreted by the browser&lt;br /&gt;
*Solution: use an escape function to transform the raw string into a sanitized version&lt;br /&gt;
**Include the following in your code to escape html tags&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;var _entityMap  = {&lt;br /&gt;
   “&amp;amp;” : “&amp;amp;amp”,&lt;br /&gt;
   “&amp;lt;“ : “&amp;amp;lt”,&lt;br /&gt;
   “&amp;gt;” : “&amp;amp;gt”,&lt;br /&gt;
   “ “ “ : “&amp;amp;quot”,&lt;br /&gt;
   “ ‘ “ : “&amp;amp;#39”,&lt;br /&gt;
   “/“ : “&amp;amp;#x2F”&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
function escapeHtml(string) {&lt;br /&gt;
   return String(string).replace(/[&amp;amp;&amp;lt;&amp;gt;&amp;quot;&#039;\/]/g, function (s) {&lt;br /&gt;
      return entityMap[s];&lt;br /&gt;
   });&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Problems like this are not hard to fix but they are often forgotten or overlooked because the solutions are not built in to the language&lt;br /&gt;
&lt;br /&gt;
====Original Version====&lt;br /&gt;
&lt;br /&gt;
*In the original version of exam-notes, we used only Jade and no client-side JavaScript to set up the HTML&lt;br /&gt;
*When the same cross site scripting attack is tried on this version, we can see that it is not vulnerable&lt;br /&gt;
**The HTML code given as input gets displayed as actual text on the web page&lt;br /&gt;
*Why is it not vulnerable?&lt;br /&gt;
**By default, Jade will escape everything for us&lt;br /&gt;
**For example, &amp;lt;code&amp;gt;#{item.city}&amp;lt;/code&amp;gt; when given a value of &amp;lt;code&amp;gt;&amp;lt;script&amp;gt;alert(“hi there”)&amp;lt;/script&amp;gt;&amp;lt;/code&amp;gt; will be rendered as &amp;lt;code&amp;gt;&amp;amp;amp;lt;script&amp;amp;amp;gt;alert(&amp;amp;amp;quot;hi there&amp;amp;amp;quot;);&amp;amp;amp;lt;/script&amp;amp;amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
*To prevent Jade from escaping HTML like this, use the bang symbol (!)&lt;br /&gt;
**For example: &amp;lt;code&amp;gt;!#{item.city}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Assignment 5===&lt;br /&gt;
&lt;br /&gt;
*All functionality can be found in parts of tutorials 4-7 and past assignments&lt;br /&gt;
*We will start a template with the completed graph-demo code&lt;br /&gt;
*Template details:&lt;br /&gt;
**The index page is the home screen&lt;br /&gt;
**We won&#039;t do any logging in&lt;br /&gt;
**The index page will show stats of uploaded logs&lt;br /&gt;
***How many files&lt;br /&gt;
***How many entries&lt;br /&gt;
**The index page will have a form for uploading a file&lt;br /&gt;
**The index page will have a form for performing query with two radio buttons and a submit button&lt;br /&gt;
***The 1st radio button causes the form submission to do a visualization of the query&lt;br /&gt;
***The 2nd radio button causes the form submission to send the logs to the browser as plain text for download&lt;br /&gt;
**The query form will have fields for&lt;br /&gt;
***Service, message and file as regular expressions&lt;br /&gt;
***Month and day as plain strings&lt;br /&gt;
&lt;br /&gt;
===Other Notes===&lt;br /&gt;
*A favicon is small icon that is displayed in the web page tab&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_14&amp;diff=20794</id>
		<title>WebFund 2016W Lecture 14</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_14&amp;diff=20794"/>
		<updated>2016-03-08T16:57:51Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on March 3, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec14-03Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In Class===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 14&lt;br /&gt;
----------&lt;br /&gt;
* sessions&lt;br /&gt;
* jQuery&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To do sessions securely, you need:&lt;br /&gt;
 (necessary but NOT sufficient)&lt;br /&gt;
* session cookies that cannot be guessed&lt;br /&gt;
  - use a secret&lt;br /&gt;
* secure password storage&lt;br /&gt;
  - need to be hashed at minimum&lt;br /&gt;
  - see bcrypt&lt;br /&gt;
* secure communication with web server&lt;br /&gt;
  - HTTPS (HTTP over SSL/TLS)&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
jQuery and client side JavaScript&lt;br /&gt;
&lt;br /&gt;
jQuery is just a standard library for client-side JS&lt;br /&gt;
 - far from the only one&lt;br /&gt;
&lt;br /&gt;
I could teach you the standard browser interface&lt;br /&gt;
 - but it is ugly and has quirks&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
How you build interfaces&lt;br /&gt;
&lt;br /&gt;
code versus data&lt;br /&gt;
&lt;br /&gt;
* when you build an interface, you have&lt;br /&gt;
  - code that determines the behavior&lt;br /&gt;
  - data that describes the interface appearance&lt;br /&gt;
&lt;br /&gt;
* But code can change the appearance, and data can&lt;br /&gt;
  describe behavior&lt;br /&gt;
&lt;br /&gt;
* how much do you do with each?&lt;br /&gt;
&lt;br /&gt;
* traditionally, you do most everything with code&lt;br /&gt;
&lt;br /&gt;
* When you want end-user customizability, you do more with data&lt;br /&gt;
  - theming&lt;br /&gt;
&lt;br /&gt;
* Even when lots of the interface is in data, the code&lt;br /&gt;
  is in charge...except on the web&lt;br /&gt;
&lt;br /&gt;
* On the web, the data is king, and the code serves the&lt;br /&gt;
  data&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
*There are now fewer assignments. Assignments 5 and 6 are going to be bigger assignments since there are now less.&lt;br /&gt;
*For the graph-demo tutorial, you could have hard coded the solution but the idea was to use loops to make things dynamic&lt;br /&gt;
**Each loops in Jade templates will be important for our next assignment&lt;br /&gt;
*Today&#039;s goals:&lt;br /&gt;
**Sessions&lt;br /&gt;
**jQuery&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Cookies and Sessions====&lt;br /&gt;
&lt;br /&gt;
=====Cookies=====&lt;br /&gt;
&lt;br /&gt;
*When logging in to the sample application shown in class:&lt;br /&gt;
**If you login on one tab, it won&#039;t let you login on another without logging out unless we use a private browser (the new tab appears as already logged in)&lt;br /&gt;
**If we login to different users on both the normal and private browser, then if we check to see who is logged in via either browser, it will tell us that both users are logged in&lt;br /&gt;
*This is done using cookies&lt;br /&gt;
*Cookies are small pieces of data sent from a website and stored in the user&#039;s web browser while the user is browsing. Every time the user loads the website, the browser sends the cookie back to the server to inform it of the user&#039;s previous activity&lt;br /&gt;
**More generally, cookies can be given to a client from any type of server. The client never does anything with the cookie aside from sending it back to the server during future interaction&lt;br /&gt;
*Cookies are why the web browsers (normal and private) were logged in separately. Each had their own cookie from the server &lt;br /&gt;
**The normal and private browsers were seen by the server as different clients whereas two tabs from the same browser were seen as the same client so they used the same cookie&lt;br /&gt;
**If we remove the cookie you will not be logged in any more&lt;br /&gt;
*Many cookies can be received just from visiting one website&lt;br /&gt;
**They are used by web servers for advertisement purposes&lt;br /&gt;
**There are also good cookies that let websites remember who you are so you do not have to constantly re-login every time you visit the website&lt;br /&gt;
&lt;br /&gt;
=====Sessions=====&lt;br /&gt;
&lt;br /&gt;
*In the sample web application, we are using sessions to manage the cookies&lt;br /&gt;
*In app.js we need to include:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var session = require(&#039;express-session&#039;);&lt;br /&gt;
app.use(session({secret: &#039;superSekretHere!&#039;,&lt;br /&gt;
		 resave: false,&lt;br /&gt;
		 saveUnitialized: false}));&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*The secret option is used to prevent people from stealing or guessing your secret. If you don&#039;t have a good secret people could reconstruct your cookies&lt;br /&gt;
*In routes/index.js, we can see the use of a &amp;lt;code&amp;gt;req.session&amp;lt;/code&amp;gt; object&lt;br /&gt;
**This object contains session information about the client that has sent in a request&lt;br /&gt;
*We are using &amp;lt;code&amp;gt;req.session.username&amp;lt;/code&amp;gt; to keep track of whether a user (a client) is logged in or not&lt;br /&gt;
**&amp;lt;code&amp;gt;req.session.username&amp;lt;/code&amp;gt; at first has no value but once you submit a username (in the /login route), this value gets set&lt;br /&gt;
**Whether or not the value is set influences the behaviour of the web app&lt;br /&gt;
***When it is set, we can no longer submit a username to log in but we can logout&lt;br /&gt;
***This is because of the conditional statements in the route functions&lt;br /&gt;
****In the / route, we render the index template (for logging in) if &amp;lt;code&amp;gt;req.session.username&amp;lt;/code&amp;gt; is not set, otherwise we redirect to the /users route&lt;br /&gt;
****Similarly, in the /users route, we render the users template if the values is set, otherwise, we redirect to the / route&lt;br /&gt;
*Sessions are currently being stored in memory but the sessions should ideally be stored in a database&lt;br /&gt;
**To do this we use MongoDB (we will see this later)&lt;br /&gt;
*To handle sessions securely, you need (necessary but NOT sufficient):&lt;br /&gt;
**Session cookies that cannot be guessed&lt;br /&gt;
***Use a secret&lt;br /&gt;
**Secure password storage&lt;br /&gt;
***Passwords need to be hashed at minimum&lt;br /&gt;
***See bcrypt&lt;br /&gt;
**Secure communication with the web server&lt;br /&gt;
***HTTPS (HTTP over SSL/TLS)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Client-Side JavaScript and jQuery====&lt;br /&gt;
&lt;br /&gt;
*In the jQuery demo, we have an interactive interface that has nothing to do with the server and it is implemented with JavaScript&lt;br /&gt;
**This is run completely within the browser (there is not even a server running on the machine)&lt;br /&gt;
**When clicking on the favourite dog breed it automatically changes&lt;br /&gt;
**This is done through the use of client-side JavaScript and the jQuery library&lt;br /&gt;
*jQuery is just a standard library for client side JavaScript and it is far from the only one&lt;br /&gt;
**We could learn the standard browser interface but it is ugly and has quirks&lt;br /&gt;
*To include a client-side script on a web page, we need to add a script tag in the head of the HTML document which points to the right script&lt;br /&gt;
**In the demo, we have one to link the jQuery library and another to link our own client-side script (which uses jQuery)&lt;br /&gt;
&lt;br /&gt;
=====Using jQuery=====&lt;br /&gt;
&lt;br /&gt;
*What is the &amp;quot;$&amp;quot; ?&lt;br /&gt;
**The &amp;quot;$&amp;quot; is equal to jQuery by default&lt;br /&gt;
*The first thing that we do at the start of the script is give jQuery a callback function&lt;br /&gt;
**This callback function contains all of the other code in our demo client-side script and it will run when the page has fully loaded&lt;br /&gt;
**Because web browsers load things on a web page incrementally, we need this callback function to force the script to wait until everything is loaded&lt;br /&gt;
***This ensures that we don&#039;t try to access something that is not there yet&lt;br /&gt;
*We are primarily using jQuery to query things&lt;br /&gt;
**What are we querying?&lt;br /&gt;
***The DOM (Document Object Model)&lt;br /&gt;
**Using the &amp;quot;#&amp;quot; is a way to tell jQuery that we are doing a query based on the ID of an element&lt;br /&gt;
**In order to allow for the demo script to easily access the favourite dog breed elements, they were given IDs in the HTML code&lt;br /&gt;
*Once we have access to an HTML element, we can modify it however we want&lt;br /&gt;
*jQuery is useful for making things nice and easy&lt;br /&gt;
**For example, it has a built in function for fading in and out elements&lt;br /&gt;
*We can access the in-browser console through the dev tools which allows us to alter what we see on our webpage in real time (using a read-eval-print loop)&lt;br /&gt;
**For example, typing in &amp;lt;code&amp;gt;$(&amp;quot;#color&amp;quot;).val(&amp;quot;pink&amp;quot;);&amp;lt;/code&amp;gt; will set the value (in this case the text) of the element with ID &amp;quot;color&amp;quot; (in this case the text field) equal to &amp;quot;pink&amp;quot; so that &amp;quot;pink&amp;quot; will appear in the text field&lt;br /&gt;
*Make sure to check the jQuery documentation for more details on how it works&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Building Interfaces (Code Versus Data)====&lt;br /&gt;
&lt;br /&gt;
*When you build an interface, you have&lt;br /&gt;
**Code that determines behavior&lt;br /&gt;
**Data that describes the interface appearance&lt;br /&gt;
*But code can change the appearance, and data can describe behavior&lt;br /&gt;
**How much do you do with each?&lt;br /&gt;
***Traditionally, you must do everything with code&lt;br /&gt;
***When you want end-user customizability, you do more with data&lt;br /&gt;
****Theming&lt;br /&gt;
***Even when lots of the interface is in the data, the code is in charge... except on the web&lt;br /&gt;
**On the web, the data is king, and the code serves the data&lt;br /&gt;
***The data (the HTML elements) completely define the interface&lt;br /&gt;
***The code (client-side JavaScript) can be used to change things but ultimately we don&#039;t need this code&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
* [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/session-demo.zip session-demo.zip]&lt;br /&gt;
* [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/jquery-test.zip jquery-test.zip]&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_13&amp;diff=20789</id>
		<title>WebFund 2016W Lecture 13</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_13&amp;diff=20789"/>
		<updated>2016-03-06T14:08:46Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: /* JavaScript Closures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on March 1, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec13-01Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In lecture===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 13&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
To get help with assignment 4, look at Assignment 5 from Winter 2015.&lt;br /&gt;
&lt;br /&gt;
closures&lt;br /&gt;
&lt;br /&gt;
DOM&lt;br /&gt;
 - document object model&lt;br /&gt;
 - set of JavaScript objects for accessing the current page in the browser&lt;br /&gt;
 &lt;br /&gt;
client-side JS can access the DOM&lt;br /&gt;
  (data structures representing the current web page)&lt;br /&gt;
&lt;br /&gt;
server-side JS can access the operating system&lt;br /&gt;
&lt;br /&gt;
client-side JS is *limited* in its access&lt;br /&gt;
 - because such access is dangerous&lt;br /&gt;
 - ANYONE can run JavaScript in your browser&lt;br /&gt;
 - so, JavaScript in the browser is sandboxed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
execution sandbox is an environment for running untrusted programs&lt;br /&gt;
 - sandbox limits access and resources&lt;br /&gt;
&lt;br /&gt;
Client-side JavaScript has no native way to&lt;br /&gt;
  (well, it used to...)&lt;br /&gt;
  - access local files&lt;br /&gt;
  - open network connections&lt;br /&gt;
  - run multiple threads&lt;br /&gt;
  - access other windows/programs&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Originally JavaScript was so limited it could do no background processing&lt;br /&gt;
 - could only run when the page was loaded or when the user acted&lt;br /&gt;
&lt;br /&gt;
Microsoft messed it up&lt;br /&gt;
 - Outlook Web Access&lt;br /&gt;
 - they built an ActiveX control which implemented XMLHttpRequest()&lt;br /&gt;
   - but really, it was a way to do GETs and POSTs in the background&lt;br /&gt;
 - other browsers implemented the API natively&lt;br /&gt;
&lt;br /&gt;
Now you could update data in a web page without reloading the page&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====storelogline.js and storelogs.js====&lt;br /&gt;
&lt;br /&gt;
*To use storelogline.js:&lt;br /&gt;
**Find an entry from an existing log file and copy the line&lt;br /&gt;
**Run storelogline.js with the copied line pasted after the script name as command line parameters&lt;br /&gt;
**Optionally, verify that the line was entered in the database by typing the following with the Mongo client:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
mongo&lt;br /&gt;
use log-demo&lt;br /&gt;
db.logs.find()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Writing storelogs.js=====&lt;br /&gt;
&lt;br /&gt;
*We can use the code from storeloglines.js to start building the storelogs.js script for tutorial 5&lt;br /&gt;
*To read in a file specified in the command line, you do &amp;lt;code&amp;gt;var data = fs.readFileSync(process.argv[2], ‘utf-8’);&amp;lt;/code&amp;gt;&lt;br /&gt;
**You use &amp;lt;code&amp;gt;process.argv[2]&amp;lt;/code&amp;gt; because in the terminal your third argument is going to be the file to read&lt;br /&gt;
*To split the data into lines, yo do &amp;lt;code&amp;gt;var lines = data.split(‘\n’);&amp;lt;/code&amp;gt;&lt;br /&gt;
*To parse the lines and store them into an array, you can do something like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var entries = [];&lt;br /&gt;
int i; entry, fields, j;&lt;br /&gt;
&lt;br /&gt;
for(i=0;i&amp;lt;lines.length; i++) {&lt;br /&gt;
   if(lines[i] &amp;amp;&amp;amp;lines[i]!==null) {&lt;br /&gt;
      field=lines[i].split(‘ ‘); //this is used to split the lines at index i wherever the spaces occur&lt;br /&gt;
      console.log(split); //when this is printed you’ll see that it splits between all the spaces as well—&amp;gt;you can regular expression to fix this but we fixed it through the while loop&lt;br /&gt;
      entry={};&lt;br /&gt;
      j=0;&lt;br /&gt;
&lt;br /&gt;
      //Used to delete the spaces&lt;br /&gt;
      while(j&amp;lt;field.length) {&lt;br /&gt;
         if(field.lenght[j]===“ “) {&lt;br /&gt;
            field.splice(j, 1);&lt;br /&gt;
         }else{&lt;br /&gt;
            j++;&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      console.log(field); //—&amp;gt;this prints the logs out without the spaces&lt;br /&gt;
      entry.date=field[0] + “ “ + field[1]; //Than you start populating the array&lt;br /&gt;
      entry.time=field[2];&lt;br /&gt;
      entry.host=field[3];&lt;br /&gt;
      entry.service=field[4].slice(0,-1); //the program will have an error here without the if statement above&lt;br /&gt;
      entry.message=filed.slice(5).join(‘ ‘);&lt;br /&gt;
      //to store in to the entries ( the array )&lt;br /&gt;
      entries.push(entry);&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*To insert into the database you don’t need a loop to iterate. You can use the &amp;lt;code&amp;gt;collection.insert()&amp;lt;/code&amp;gt; method which can take an array (entries) as the argument to insert&lt;br /&gt;
**API’s have documentation and they change because there’s also &amp;lt;code&amp;gt;insertOne()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;insertMany()&amp;lt;/code&amp;gt; that seem to do the same thing&lt;br /&gt;
**For what we want to do &amp;lt;code&amp;gt;insert()&amp;lt;/code&amp;gt; will work&lt;br /&gt;
*For querying the database (assignment 4), there is a trick&lt;br /&gt;
**There are previous assignment solutions that can help you.&lt;br /&gt;
**It&#039;s the previous assignment from last year’s assignment 5&lt;br /&gt;
*Note that when using special characters like [] and () in regular expression, you will need to use a backslash to escape them&lt;br /&gt;
&lt;br /&gt;
====JavaScript Closures====&lt;br /&gt;
&lt;br /&gt;
*JavaScript closures are about nesting functions&lt;br /&gt;
**For example:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var f = function(x) {&lt;br /&gt;
   function g(y) {&lt;br /&gt;
      return x+y;&lt;br /&gt;
   }&lt;br /&gt;
   return g; //The function g can see x&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var q = f(7);&lt;br /&gt;
var h = f(12);&lt;br /&gt;
console.log(“q = “ +q(4)); //prints 11&lt;br /&gt;
console.log(“h(4) = “ + h(4)); //Prints 16&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*This gives different answers because &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; is different in two two instances of &amp;lt;code&amp;gt;g()&amp;lt;/code&amp;gt;&lt;br /&gt;
*You cannot do things like this with C&lt;br /&gt;
*In examforms we have an app.get for list and we have a function in it that has a callback called &amp;lt;code&amp;gt;findCallback()&amp;lt;/code&amp;gt;&lt;br /&gt;
**This callback function has access to the request and response objects because they were in scope at the location where the callback function was defined&lt;br /&gt;
**To have &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; have access to request and response you just need to create different instances of &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; that have their own copies of request and response, therefore one version of &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; will have one request and on another call it will have another request&lt;br /&gt;
*Closure is a concept you need to understand because it’s very handy&lt;br /&gt;
&lt;br /&gt;
====Client-Side JavaScript====&lt;br /&gt;
&lt;br /&gt;
*In JavaScript we’ve been doing everything on the server side so far&lt;br /&gt;
*Using the browser dev tools, you can go to console and play around in it. Its almost like the Node environment in a terminal but with some missing features&lt;br /&gt;
*In the browser there is no &amp;lt;code&amp;gt;require()&amp;lt;/code&amp;gt;&lt;br /&gt;
*In the browser the unit is the page but in the server its the file&lt;br /&gt;
*You can access objects representing the page by typing &amp;lt;code&amp;gt;document&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;window&amp;lt;/code&amp;gt;&lt;br /&gt;
*When you load a webpage, it can load and run scripts&lt;br /&gt;
**By running JavaScript within the browser, you can write code that has access to the webpage (in particular, the DOM)&lt;br /&gt;
***The DOM (Document Object Model) is a tree-like structure of objects representing the HTML elements of the page&lt;br /&gt;
&lt;br /&gt;
=====Differences Between Client-Side and Server-Side JavaScript=====&lt;br /&gt;
&lt;br /&gt;
*Client-side JavaScript can access the DOM (data structure repressing the current web page)&lt;br /&gt;
*Server-side JavaScript can access the operating system&lt;br /&gt;
*In client-side JavaScript, you can’t access everything... It&#039;s *limited*&lt;br /&gt;
**That’s because such access is dangerous, the code could be coming from ANYONE&lt;br /&gt;
**ANYONE can run JavaScript in your browser and you don’t want this code messing with your system&lt;br /&gt;
**The browser is designed to protect your system from bad JavaScript, so JavaScript in the browser is sandboxed&lt;br /&gt;
***An execution sandbox is an environment for running untrusted code&lt;br /&gt;
***The sandbox limits access and resources&lt;br /&gt;
***Client-side JavaScript has no native way to:&lt;br /&gt;
****Access local files&lt;br /&gt;
****Open network connections&lt;br /&gt;
****Run multiple threads&lt;br /&gt;
****Access other windows/programs&lt;br /&gt;
*The model of client-side JavaScript was that it should be limited&lt;br /&gt;
*Originally JavaScript was so limited that it could do no background processing&lt;br /&gt;
**It originally could only run when the webpage was loaded or when the user acted&lt;br /&gt;
**It was singly threaded so you had to limit your execution&lt;br /&gt;
**Microsoft messed it up (in particular, the Outlook Web Access team messed it up)&lt;br /&gt;
***They built an ActiveX control which implemented XMLHttpRequest() —&amp;gt; an object that would send a GET or a POST request and would bring back a document that is assumed to be XML&lt;br /&gt;
***But more importantly, it was a way to do GETs and POSTs in the background&lt;br /&gt;
***Other browsers implemented the API natively&lt;br /&gt;
***Now you could update data in a web page without reloading the page&lt;br /&gt;
***Google really ran with this (with gmail, google maps, etc)&lt;br /&gt;
*Client-side JavaScript is the same JavaScript we have been working with except it has access to a webpage now but you access the things you can access the exact same way&lt;br /&gt;
*If you want to change how the current page looks or works you can type JavaScript directly into the browser console (it uses a read-eval-print loop which will affect the page)&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
===nested.js===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
var f = function(x) {&lt;br /&gt;
    function g(y) {&lt;br /&gt;
	return x + y;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    return g;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var q = f(7);&lt;br /&gt;
var h = f(12);&lt;br /&gt;
&lt;br /&gt;
console.log(&amp;quot;q(4) = &amp;quot; + q(4));&lt;br /&gt;
console.log(&amp;quot;h(4) = &amp;quot; + h(4));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===storeLogs.js===&lt;br /&gt;
&lt;br /&gt;
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/storeLogs.js downloadable version]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
// storeLogs.js&lt;br /&gt;
//&lt;br /&gt;
// node storeLogs.js &amp;lt;logfile&amp;gt;&lt;br /&gt;
//&lt;br /&gt;
//&lt;br /&gt;
// The log messages in the file should be of the format:&lt;br /&gt;
// &amp;lt;month&amp;gt; &amp;lt;day of month&amp;gt; &amp;lt;24-hour time in hh:mm:ss&amp;gt; &amp;lt;host&amp;gt; &amp;lt;service name[pid]&amp;gt;: Actual message&lt;br /&gt;
//&lt;br /&gt;
&lt;br /&gt;
var mc = require(&#039;mongodb&#039;).MongoClient;&lt;br /&gt;
var fs = require(&#039;fs&#039;);&lt;br /&gt;
&lt;br /&gt;
var data = fs.readFileSync(process.argv[2], &#039;utf-8&#039;);&lt;br /&gt;
&lt;br /&gt;
var lines = data.split(&#039;\n&#039;);&lt;br /&gt;
&lt;br /&gt;
var entries = [];&lt;br /&gt;
&lt;br /&gt;
var i, j, entry, field;&lt;br /&gt;
&lt;br /&gt;
for (i=0; i&amp;lt;lines.length; i++) {&lt;br /&gt;
    if (lines[i] &amp;amp;&amp;amp; lines[i] !== &#039;&#039;) {&lt;br /&gt;
	field = lines[i].split(&#039; &#039;);&lt;br /&gt;
	entry = {};&lt;br /&gt;
	j = 0;&lt;br /&gt;
	while (j &amp;lt; field.length) {&lt;br /&gt;
	    if (field[j] === &amp;quot;&amp;quot;) {&lt;br /&gt;
		field.splice(j, 1);&lt;br /&gt;
	    } else {&lt;br /&gt;
		j++;&lt;br /&gt;
	    } &lt;br /&gt;
	}&lt;br /&gt;
	entry.date = field[0] + &amp;quot; &amp;quot; + field[1];&lt;br /&gt;
	entry.time = field[2];&lt;br /&gt;
	entry.host = field[3];&lt;br /&gt;
	entry.service = field[4].slice(0,-1);&lt;br /&gt;
	entry.message = field.slice(5).join(&#039; &#039;);&lt;br /&gt;
	entries.push(entry);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// entry.date = process.argv[2] + &amp;quot; &amp;quot; + process.argv[3];&lt;br /&gt;
// entry.time = process.argv[4];&lt;br /&gt;
// entry.host = process.argv[5];&lt;br /&gt;
// entry.service = process.argv[6].slice(0,-1);  // drop the trailing colon&lt;br /&gt;
// entry.message = process.argv.slice(7).join(&#039; &#039;);&lt;br /&gt;
&lt;br /&gt;
var db;&lt;br /&gt;
&lt;br /&gt;
var reportInserted = function(err, result) {&lt;br /&gt;
    if (err) {&lt;br /&gt;
	throw err;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    console.log(&amp;quot;Inserted the following log record:&amp;quot;);&lt;br /&gt;
    console.log(result.ops);&lt;br /&gt;
    db.close();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var connectCallback = function(err, returnedDB) {&lt;br /&gt;
    if (err) {&lt;br /&gt;
	throw err;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    db = returnedDB;&lt;br /&gt;
 &lt;br /&gt;
    db.collection(&#039;logs&#039;).insert(entries, reportInserted);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
mc.connect(&#039;mongodb://localhost/log-demo&#039;, connectCallback);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_13&amp;diff=20788</id>
		<title>WebFund 2016W Lecture 13</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_13&amp;diff=20788"/>
		<updated>2016-03-06T14:08:19Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: /* JavaScript Closures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on March 1, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec13-01Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In lecture===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 13&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
To get help with assignment 4, look at Assignment 5 from Winter 2015.&lt;br /&gt;
&lt;br /&gt;
closures&lt;br /&gt;
&lt;br /&gt;
DOM&lt;br /&gt;
 - document object model&lt;br /&gt;
 - set of JavaScript objects for accessing the current page in the browser&lt;br /&gt;
 &lt;br /&gt;
client-side JS can access the DOM&lt;br /&gt;
  (data structures representing the current web page)&lt;br /&gt;
&lt;br /&gt;
server-side JS can access the operating system&lt;br /&gt;
&lt;br /&gt;
client-side JS is *limited* in its access&lt;br /&gt;
 - because such access is dangerous&lt;br /&gt;
 - ANYONE can run JavaScript in your browser&lt;br /&gt;
 - so, JavaScript in the browser is sandboxed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
execution sandbox is an environment for running untrusted programs&lt;br /&gt;
 - sandbox limits access and resources&lt;br /&gt;
&lt;br /&gt;
Client-side JavaScript has no native way to&lt;br /&gt;
  (well, it used to...)&lt;br /&gt;
  - access local files&lt;br /&gt;
  - open network connections&lt;br /&gt;
  - run multiple threads&lt;br /&gt;
  - access other windows/programs&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Originally JavaScript was so limited it could do no background processing&lt;br /&gt;
 - could only run when the page was loaded or when the user acted&lt;br /&gt;
&lt;br /&gt;
Microsoft messed it up&lt;br /&gt;
 - Outlook Web Access&lt;br /&gt;
 - they built an ActiveX control which implemented XMLHttpRequest()&lt;br /&gt;
   - but really, it was a way to do GETs and POSTs in the background&lt;br /&gt;
 - other browsers implemented the API natively&lt;br /&gt;
&lt;br /&gt;
Now you could update data in a web page without reloading the page&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====storelogline.js and storelogs.js====&lt;br /&gt;
&lt;br /&gt;
*To use storelogline.js:&lt;br /&gt;
**Find an entry from an existing log file and copy the line&lt;br /&gt;
**Run storelogline.js with the copied line pasted after the script name as command line parameters&lt;br /&gt;
**Optionally, verify that the line was entered in the database by typing the following with the Mongo client:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
mongo&lt;br /&gt;
use log-demo&lt;br /&gt;
db.logs.find()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Writing storelogs.js=====&lt;br /&gt;
&lt;br /&gt;
*We can use the code from storeloglines.js to start building the storelogs.js script for tutorial 5&lt;br /&gt;
*To read in a file specified in the command line, you do &amp;lt;code&amp;gt;var data = fs.readFileSync(process.argv[2], ‘utf-8’);&amp;lt;/code&amp;gt;&lt;br /&gt;
**You use &amp;lt;code&amp;gt;process.argv[2]&amp;lt;/code&amp;gt; because in the terminal your third argument is going to be the file to read&lt;br /&gt;
*To split the data into lines, yo do &amp;lt;code&amp;gt;var lines = data.split(‘\n’);&amp;lt;/code&amp;gt;&lt;br /&gt;
*To parse the lines and store them into an array, you can do something like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var entries = [];&lt;br /&gt;
int i; entry, fields, j;&lt;br /&gt;
&lt;br /&gt;
for(i=0;i&amp;lt;lines.length; i++) {&lt;br /&gt;
   if(lines[i] &amp;amp;&amp;amp;lines[i]!==null) {&lt;br /&gt;
      field=lines[i].split(‘ ‘); //this is used to split the lines at index i wherever the spaces occur&lt;br /&gt;
      console.log(split); //when this is printed you’ll see that it splits between all the spaces as well—&amp;gt;you can regular expression to fix this but we fixed it through the while loop&lt;br /&gt;
      entry={};&lt;br /&gt;
      j=0;&lt;br /&gt;
&lt;br /&gt;
      //Used to delete the spaces&lt;br /&gt;
      while(j&amp;lt;field.length) {&lt;br /&gt;
         if(field.lenght[j]===“ “) {&lt;br /&gt;
            field.splice(j, 1);&lt;br /&gt;
         }else{&lt;br /&gt;
            j++;&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      console.log(field); //—&amp;gt;this prints the logs out without the spaces&lt;br /&gt;
      entry.date=field[0] + “ “ + field[1]; //Than you start populating the array&lt;br /&gt;
      entry.time=field[2];&lt;br /&gt;
      entry.host=field[3];&lt;br /&gt;
      entry.service=field[4].slice(0,-1); //the program will have an error here without the if statement above&lt;br /&gt;
      entry.message=filed.slice(5).join(‘ ‘);&lt;br /&gt;
      //to store in to the entries ( the array )&lt;br /&gt;
      entries.push(entry);&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*To insert into the database you don’t need a loop to iterate. You can use the &amp;lt;code&amp;gt;collection.insert()&amp;lt;/code&amp;gt; method which can take an array (entries) as the argument to insert&lt;br /&gt;
**API’s have documentation and they change because there’s also &amp;lt;code&amp;gt;insertOne()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;insertMany()&amp;lt;/code&amp;gt; that seem to do the same thing&lt;br /&gt;
**For what we want to do &amp;lt;code&amp;gt;insert()&amp;lt;/code&amp;gt; will work&lt;br /&gt;
*For querying the database (assignment 4), there is a trick&lt;br /&gt;
**There are previous assignment solutions that can help you.&lt;br /&gt;
**It&#039;s the previous assignment from last year’s assignment 5&lt;br /&gt;
*Note that when using special characters like [] and () in regular expression, you will need to use a backslash to escape them&lt;br /&gt;
&lt;br /&gt;
====JavaScript Closures====&lt;br /&gt;
&lt;br /&gt;
*JavaScript closures are about nesting functions&lt;br /&gt;
**For example:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var f = function(x) {&lt;br /&gt;
   function g(y) {&lt;br /&gt;
      return x+y;&lt;br /&gt;
   }&lt;br /&gt;
   return g; //The function g can see x&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var q = f(7);&lt;br /&gt;
var h = f(12);&lt;br /&gt;
console.log(“q = “ +q(4)); //prints 11&lt;br /&gt;
console.log(“h(4) = “ + h(4)); //Prints 16&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*This gives different answers because x is different in two two instances of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;&lt;br /&gt;
*You cannot do things like this with C&lt;br /&gt;
*In examforms we have an app.get for list and we have a function in it that has a callback called &amp;lt;code&amp;gt;findCallback()&amp;lt;/code&amp;gt;&lt;br /&gt;
**This callback function has access to the request and response objects because they were in scope at the location where the callback function was defined&lt;br /&gt;
**To have &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; have access to request and response you just need to create different instances of &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; that have their own copies of request and response, therefore one version of &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; will have one request and on another call it will have another request&lt;br /&gt;
*Closure is a concept you need to understand because it’s very handy&lt;br /&gt;
&lt;br /&gt;
====Client-Side JavaScript====&lt;br /&gt;
&lt;br /&gt;
*In JavaScript we’ve been doing everything on the server side so far&lt;br /&gt;
*Using the browser dev tools, you can go to console and play around in it. Its almost like the Node environment in a terminal but with some missing features&lt;br /&gt;
*In the browser there is no &amp;lt;code&amp;gt;require()&amp;lt;/code&amp;gt;&lt;br /&gt;
*In the browser the unit is the page but in the server its the file&lt;br /&gt;
*You can access objects representing the page by typing &amp;lt;code&amp;gt;document&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;window&amp;lt;/code&amp;gt;&lt;br /&gt;
*When you load a webpage, it can load and run scripts&lt;br /&gt;
**By running JavaScript within the browser, you can write code that has access to the webpage (in particular, the DOM)&lt;br /&gt;
***The DOM (Document Object Model) is a tree-like structure of objects representing the HTML elements of the page&lt;br /&gt;
&lt;br /&gt;
=====Differences Between Client-Side and Server-Side JavaScript=====&lt;br /&gt;
&lt;br /&gt;
*Client-side JavaScript can access the DOM (data structure repressing the current web page)&lt;br /&gt;
*Server-side JavaScript can access the operating system&lt;br /&gt;
*In client-side JavaScript, you can’t access everything... It&#039;s *limited*&lt;br /&gt;
**That’s because such access is dangerous, the code could be coming from ANYONE&lt;br /&gt;
**ANYONE can run JavaScript in your browser and you don’t want this code messing with your system&lt;br /&gt;
**The browser is designed to protect your system from bad JavaScript, so JavaScript in the browser is sandboxed&lt;br /&gt;
***An execution sandbox is an environment for running untrusted code&lt;br /&gt;
***The sandbox limits access and resources&lt;br /&gt;
***Client-side JavaScript has no native way to:&lt;br /&gt;
****Access local files&lt;br /&gt;
****Open network connections&lt;br /&gt;
****Run multiple threads&lt;br /&gt;
****Access other windows/programs&lt;br /&gt;
*The model of client-side JavaScript was that it should be limited&lt;br /&gt;
*Originally JavaScript was so limited that it could do no background processing&lt;br /&gt;
**It originally could only run when the webpage was loaded or when the user acted&lt;br /&gt;
**It was singly threaded so you had to limit your execution&lt;br /&gt;
**Microsoft messed it up (in particular, the Outlook Web Access team messed it up)&lt;br /&gt;
***They built an ActiveX control which implemented XMLHttpRequest() —&amp;gt; an object that would send a GET or a POST request and would bring back a document that is assumed to be XML&lt;br /&gt;
***But more importantly, it was a way to do GETs and POSTs in the background&lt;br /&gt;
***Other browsers implemented the API natively&lt;br /&gt;
***Now you could update data in a web page without reloading the page&lt;br /&gt;
***Google really ran with this (with gmail, google maps, etc)&lt;br /&gt;
*Client-side JavaScript is the same JavaScript we have been working with except it has access to a webpage now but you access the things you can access the exact same way&lt;br /&gt;
*If you want to change how the current page looks or works you can type JavaScript directly into the browser console (it uses a read-eval-print loop which will affect the page)&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
===nested.js===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
var f = function(x) {&lt;br /&gt;
    function g(y) {&lt;br /&gt;
	return x + y;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    return g;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var q = f(7);&lt;br /&gt;
var h = f(12);&lt;br /&gt;
&lt;br /&gt;
console.log(&amp;quot;q(4) = &amp;quot; + q(4));&lt;br /&gt;
console.log(&amp;quot;h(4) = &amp;quot; + h(4));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===storeLogs.js===&lt;br /&gt;
&lt;br /&gt;
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/storeLogs.js downloadable version]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
// storeLogs.js&lt;br /&gt;
//&lt;br /&gt;
// node storeLogs.js &amp;lt;logfile&amp;gt;&lt;br /&gt;
//&lt;br /&gt;
//&lt;br /&gt;
// The log messages in the file should be of the format:&lt;br /&gt;
// &amp;lt;month&amp;gt; &amp;lt;day of month&amp;gt; &amp;lt;24-hour time in hh:mm:ss&amp;gt; &amp;lt;host&amp;gt; &amp;lt;service name[pid]&amp;gt;: Actual message&lt;br /&gt;
//&lt;br /&gt;
&lt;br /&gt;
var mc = require(&#039;mongodb&#039;).MongoClient;&lt;br /&gt;
var fs = require(&#039;fs&#039;);&lt;br /&gt;
&lt;br /&gt;
var data = fs.readFileSync(process.argv[2], &#039;utf-8&#039;);&lt;br /&gt;
&lt;br /&gt;
var lines = data.split(&#039;\n&#039;);&lt;br /&gt;
&lt;br /&gt;
var entries = [];&lt;br /&gt;
&lt;br /&gt;
var i, j, entry, field;&lt;br /&gt;
&lt;br /&gt;
for (i=0; i&amp;lt;lines.length; i++) {&lt;br /&gt;
    if (lines[i] &amp;amp;&amp;amp; lines[i] !== &#039;&#039;) {&lt;br /&gt;
	field = lines[i].split(&#039; &#039;);&lt;br /&gt;
	entry = {};&lt;br /&gt;
	j = 0;&lt;br /&gt;
	while (j &amp;lt; field.length) {&lt;br /&gt;
	    if (field[j] === &amp;quot;&amp;quot;) {&lt;br /&gt;
		field.splice(j, 1);&lt;br /&gt;
	    } else {&lt;br /&gt;
		j++;&lt;br /&gt;
	    } &lt;br /&gt;
	}&lt;br /&gt;
	entry.date = field[0] + &amp;quot; &amp;quot; + field[1];&lt;br /&gt;
	entry.time = field[2];&lt;br /&gt;
	entry.host = field[3];&lt;br /&gt;
	entry.service = field[4].slice(0,-1);&lt;br /&gt;
	entry.message = field.slice(5).join(&#039; &#039;);&lt;br /&gt;
	entries.push(entry);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// entry.date = process.argv[2] + &amp;quot; &amp;quot; + process.argv[3];&lt;br /&gt;
// entry.time = process.argv[4];&lt;br /&gt;
// entry.host = process.argv[5];&lt;br /&gt;
// entry.service = process.argv[6].slice(0,-1);  // drop the trailing colon&lt;br /&gt;
// entry.message = process.argv.slice(7).join(&#039; &#039;);&lt;br /&gt;
&lt;br /&gt;
var db;&lt;br /&gt;
&lt;br /&gt;
var reportInserted = function(err, result) {&lt;br /&gt;
    if (err) {&lt;br /&gt;
	throw err;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    console.log(&amp;quot;Inserted the following log record:&amp;quot;);&lt;br /&gt;
    console.log(result.ops);&lt;br /&gt;
    db.close();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var connectCallback = function(err, returnedDB) {&lt;br /&gt;
    if (err) {&lt;br /&gt;
	throw err;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    db = returnedDB;&lt;br /&gt;
 &lt;br /&gt;
    db.collection(&#039;logs&#039;).insert(entries, reportInserted);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
mc.connect(&#039;mongodb://localhost/log-demo&#039;, connectCallback);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_13&amp;diff=20787</id>
		<title>WebFund 2016W Lecture 13</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_13&amp;diff=20787"/>
		<updated>2016-03-06T14:06:51Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: /* Writing storelogs.js */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on March 1, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec13-01Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In lecture===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 13&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
To get help with assignment 4, look at Assignment 5 from Winter 2015.&lt;br /&gt;
&lt;br /&gt;
closures&lt;br /&gt;
&lt;br /&gt;
DOM&lt;br /&gt;
 - document object model&lt;br /&gt;
 - set of JavaScript objects for accessing the current page in the browser&lt;br /&gt;
 &lt;br /&gt;
client-side JS can access the DOM&lt;br /&gt;
  (data structures representing the current web page)&lt;br /&gt;
&lt;br /&gt;
server-side JS can access the operating system&lt;br /&gt;
&lt;br /&gt;
client-side JS is *limited* in its access&lt;br /&gt;
 - because such access is dangerous&lt;br /&gt;
 - ANYONE can run JavaScript in your browser&lt;br /&gt;
 - so, JavaScript in the browser is sandboxed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
execution sandbox is an environment for running untrusted programs&lt;br /&gt;
 - sandbox limits access and resources&lt;br /&gt;
&lt;br /&gt;
Client-side JavaScript has no native way to&lt;br /&gt;
  (well, it used to...)&lt;br /&gt;
  - access local files&lt;br /&gt;
  - open network connections&lt;br /&gt;
  - run multiple threads&lt;br /&gt;
  - access other windows/programs&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Originally JavaScript was so limited it could do no background processing&lt;br /&gt;
 - could only run when the page was loaded or when the user acted&lt;br /&gt;
&lt;br /&gt;
Microsoft messed it up&lt;br /&gt;
 - Outlook Web Access&lt;br /&gt;
 - they built an ActiveX control which implemented XMLHttpRequest()&lt;br /&gt;
   - but really, it was a way to do GETs and POSTs in the background&lt;br /&gt;
 - other browsers implemented the API natively&lt;br /&gt;
&lt;br /&gt;
Now you could update data in a web page without reloading the page&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====storelogline.js and storelogs.js====&lt;br /&gt;
&lt;br /&gt;
*To use storelogline.js:&lt;br /&gt;
**Find an entry from an existing log file and copy the line&lt;br /&gt;
**Run storelogline.js with the copied line pasted after the script name as command line parameters&lt;br /&gt;
**Optionally, verify that the line was entered in the database by typing the following with the Mongo client:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
mongo&lt;br /&gt;
use log-demo&lt;br /&gt;
db.logs.find()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Writing storelogs.js=====&lt;br /&gt;
&lt;br /&gt;
*We can use the code from storeloglines.js to start building the storelogs.js script for tutorial 5&lt;br /&gt;
*To read in a file specified in the command line, you do &amp;lt;code&amp;gt;var data = fs.readFileSync(process.argv[2], ‘utf-8’);&amp;lt;/code&amp;gt;&lt;br /&gt;
**You use &amp;lt;code&amp;gt;process.argv[2]&amp;lt;/code&amp;gt; because in the terminal your third argument is going to be the file to read&lt;br /&gt;
*To split the data into lines, yo do &amp;lt;code&amp;gt;var lines = data.split(‘\n’);&amp;lt;/code&amp;gt;&lt;br /&gt;
*To parse the lines and store them into an array, you can do something like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var entries = [];&lt;br /&gt;
int i; entry, fields, j;&lt;br /&gt;
&lt;br /&gt;
for(i=0;i&amp;lt;lines.length; i++) {&lt;br /&gt;
   if(lines[i] &amp;amp;&amp;amp;lines[i]!==null) {&lt;br /&gt;
      field=lines[i].split(‘ ‘); //this is used to split the lines at index i wherever the spaces occur&lt;br /&gt;
      console.log(split); //when this is printed you’ll see that it splits between all the spaces as well—&amp;gt;you can regular expression to fix this but we fixed it through the while loop&lt;br /&gt;
      entry={};&lt;br /&gt;
      j=0;&lt;br /&gt;
&lt;br /&gt;
      //Used to delete the spaces&lt;br /&gt;
      while(j&amp;lt;field.length) {&lt;br /&gt;
         if(field.lenght[j]===“ “) {&lt;br /&gt;
            field.splice(j, 1);&lt;br /&gt;
         }else{&lt;br /&gt;
            j++;&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      console.log(field); //—&amp;gt;this prints the logs out without the spaces&lt;br /&gt;
      entry.date=field[0] + “ “ + field[1]; //Than you start populating the array&lt;br /&gt;
      entry.time=field[2];&lt;br /&gt;
      entry.host=field[3];&lt;br /&gt;
      entry.service=field[4].slice(0,-1); //the program will have an error here without the if statement above&lt;br /&gt;
      entry.message=filed.slice(5).join(‘ ‘);&lt;br /&gt;
      //to store in to the entries ( the array )&lt;br /&gt;
      entries.push(entry);&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*To insert into the database you don’t need a loop to iterate. You can use the &amp;lt;code&amp;gt;collection.insert()&amp;lt;/code&amp;gt; method which can take an array (entries) as the argument to insert&lt;br /&gt;
**API’s have documentation and they change because there’s also &amp;lt;code&amp;gt;insertOne()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;insertMany()&amp;lt;/code&amp;gt; that seem to do the same thing&lt;br /&gt;
**For what we want to do &amp;lt;code&amp;gt;insert()&amp;lt;/code&amp;gt; will work&lt;br /&gt;
*For querying the database (assignment 4), there is a trick&lt;br /&gt;
**There are previous assignment solutions that can help you.&lt;br /&gt;
**It&#039;s the previous assignment from last year’s assignment 5&lt;br /&gt;
*Note that when using special characters like [] and () in regular expression, you will need to use a backslash to escape them&lt;br /&gt;
&lt;br /&gt;
====JavaScript Closures====&lt;br /&gt;
&lt;br /&gt;
*JavaScript closures are about nesting functions&lt;br /&gt;
*For example:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var f = function(x) {&lt;br /&gt;
   function g(y) {&lt;br /&gt;
      return x+y;&lt;br /&gt;
   }&lt;br /&gt;
   return g; //The function g can see x&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var q = f(7);&lt;br /&gt;
var h = f(12);&lt;br /&gt;
console.log(“q = “ +q(4)); //prints 11&lt;br /&gt;
console.log(“h(4) = “ + h(4)); //Prints 16&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*This gives different answers because x is different in two two instances of g&lt;br /&gt;
*You cannot do things like this with C&lt;br /&gt;
*In examforms we have an app.get for list and we have a function in it that has a callback called &amp;lt;code&amp;gt;findCallback()&amp;lt;/code&amp;gt;&lt;br /&gt;
**This callback function has access to the request and response objects because they were in scope at the location where the callback function was defined&lt;br /&gt;
**To have &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; have access to request and response you just need to create different instances of &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; that have their own copies of request and response, therefore one version of &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; will have one request and on another call it will have another request&lt;br /&gt;
*Closure is a concept you need to understand because it’s very handy&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Client-Side JavaScript====&lt;br /&gt;
&lt;br /&gt;
*In JavaScript we’ve been doing everything on the server side so far&lt;br /&gt;
*Using the browser dev tools, you can go to console and play around in it. Its almost like the Node environment in a terminal but with some missing features&lt;br /&gt;
*In the browser there is no &amp;lt;code&amp;gt;require()&amp;lt;/code&amp;gt;&lt;br /&gt;
*In the browser the unit is the page but in the server its the file&lt;br /&gt;
*You can access objects representing the page by typing &amp;lt;code&amp;gt;document&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;window&amp;lt;/code&amp;gt;&lt;br /&gt;
*When you load a webpage, it can load and run scripts&lt;br /&gt;
**By running JavaScript within the browser, you can write code that has access to the webpage (in particular, the DOM)&lt;br /&gt;
***The DOM (Document Object Model) is a tree-like structure of objects representing the HTML elements of the page&lt;br /&gt;
&lt;br /&gt;
=====Differences Between Client-Side and Server-Side JavaScript=====&lt;br /&gt;
&lt;br /&gt;
*Client-side JavaScript can access the DOM (data structure repressing the current web page)&lt;br /&gt;
*Server-side JavaScript can access the operating system&lt;br /&gt;
*In client-side JavaScript, you can’t access everything... It&#039;s *limited*&lt;br /&gt;
**That’s because such access is dangerous, the code could be coming from ANYONE&lt;br /&gt;
**ANYONE can run JavaScript in your browser and you don’t want this code messing with your system&lt;br /&gt;
**The browser is designed to protect your system from bad JavaScript, so JavaScript in the browser is sandboxed&lt;br /&gt;
***An execution sandbox is an environment for running untrusted code&lt;br /&gt;
***The sandbox limits access and resources&lt;br /&gt;
***Client-side JavaScript has no native way to:&lt;br /&gt;
****Access local files&lt;br /&gt;
****Open network connections&lt;br /&gt;
****Run multiple threads&lt;br /&gt;
****Access other windows/programs&lt;br /&gt;
*The model of client-side JavaScript was that it should be limited&lt;br /&gt;
*Originally JavaScript was so limited that it could do no background processing&lt;br /&gt;
**It originally could only run when the webpage was loaded or when the user acted&lt;br /&gt;
**It was singly threaded so you had to limit your execution&lt;br /&gt;
**Microsoft messed it up (in particular, the Outlook Web Access team messed it up)&lt;br /&gt;
***They built an ActiveX control which implemented XMLHttpRequest() —&amp;gt; an object that would send a GET or a POST request and would bring back a document that is assumed to be XML&lt;br /&gt;
***But more importantly, it was a way to do GETs and POSTs in the background&lt;br /&gt;
***Other browsers implemented the API natively&lt;br /&gt;
***Now you could update data in a web page without reloading the page&lt;br /&gt;
***Google really ran with this (with gmail, google maps, etc)&lt;br /&gt;
*Client-side JavaScript is the same JavaScript we have been working with except it has access to a webpage now but you access the things you can access the exact same way&lt;br /&gt;
*If you want to change how the current page looks or works you can type JavaScript directly into the browser console (it uses a read-eval-print loop which will affect the page)&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
===nested.js===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
var f = function(x) {&lt;br /&gt;
    function g(y) {&lt;br /&gt;
	return x + y;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    return g;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var q = f(7);&lt;br /&gt;
var h = f(12);&lt;br /&gt;
&lt;br /&gt;
console.log(&amp;quot;q(4) = &amp;quot; + q(4));&lt;br /&gt;
console.log(&amp;quot;h(4) = &amp;quot; + h(4));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===storeLogs.js===&lt;br /&gt;
&lt;br /&gt;
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/storeLogs.js downloadable version]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
// storeLogs.js&lt;br /&gt;
//&lt;br /&gt;
// node storeLogs.js &amp;lt;logfile&amp;gt;&lt;br /&gt;
//&lt;br /&gt;
//&lt;br /&gt;
// The log messages in the file should be of the format:&lt;br /&gt;
// &amp;lt;month&amp;gt; &amp;lt;day of month&amp;gt; &amp;lt;24-hour time in hh:mm:ss&amp;gt; &amp;lt;host&amp;gt; &amp;lt;service name[pid]&amp;gt;: Actual message&lt;br /&gt;
//&lt;br /&gt;
&lt;br /&gt;
var mc = require(&#039;mongodb&#039;).MongoClient;&lt;br /&gt;
var fs = require(&#039;fs&#039;);&lt;br /&gt;
&lt;br /&gt;
var data = fs.readFileSync(process.argv[2], &#039;utf-8&#039;);&lt;br /&gt;
&lt;br /&gt;
var lines = data.split(&#039;\n&#039;);&lt;br /&gt;
&lt;br /&gt;
var entries = [];&lt;br /&gt;
&lt;br /&gt;
var i, j, entry, field;&lt;br /&gt;
&lt;br /&gt;
for (i=0; i&amp;lt;lines.length; i++) {&lt;br /&gt;
    if (lines[i] &amp;amp;&amp;amp; lines[i] !== &#039;&#039;) {&lt;br /&gt;
	field = lines[i].split(&#039; &#039;);&lt;br /&gt;
	entry = {};&lt;br /&gt;
	j = 0;&lt;br /&gt;
	while (j &amp;lt; field.length) {&lt;br /&gt;
	    if (field[j] === &amp;quot;&amp;quot;) {&lt;br /&gt;
		field.splice(j, 1);&lt;br /&gt;
	    } else {&lt;br /&gt;
		j++;&lt;br /&gt;
	    } &lt;br /&gt;
	}&lt;br /&gt;
	entry.date = field[0] + &amp;quot; &amp;quot; + field[1];&lt;br /&gt;
	entry.time = field[2];&lt;br /&gt;
	entry.host = field[3];&lt;br /&gt;
	entry.service = field[4].slice(0,-1);&lt;br /&gt;
	entry.message = field.slice(5).join(&#039; &#039;);&lt;br /&gt;
	entries.push(entry);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// entry.date = process.argv[2] + &amp;quot; &amp;quot; + process.argv[3];&lt;br /&gt;
// entry.time = process.argv[4];&lt;br /&gt;
// entry.host = process.argv[5];&lt;br /&gt;
// entry.service = process.argv[6].slice(0,-1);  // drop the trailing colon&lt;br /&gt;
// entry.message = process.argv.slice(7).join(&#039; &#039;);&lt;br /&gt;
&lt;br /&gt;
var db;&lt;br /&gt;
&lt;br /&gt;
var reportInserted = function(err, result) {&lt;br /&gt;
    if (err) {&lt;br /&gt;
	throw err;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    console.log(&amp;quot;Inserted the following log record:&amp;quot;);&lt;br /&gt;
    console.log(result.ops);&lt;br /&gt;
    db.close();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var connectCallback = function(err, returnedDB) {&lt;br /&gt;
    if (err) {&lt;br /&gt;
	throw err;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    db = returnedDB;&lt;br /&gt;
 &lt;br /&gt;
    db.collection(&#039;logs&#039;).insert(entries, reportInserted);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
mc.connect(&#039;mongodb://localhost/log-demo&#039;, connectCallback);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_13&amp;diff=20786</id>
		<title>WebFund 2016W Lecture 13</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_13&amp;diff=20786"/>
		<updated>2016-03-06T14:06:04Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on March 1, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec13-01Mar2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In lecture===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 13&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
To get help with assignment 4, look at Assignment 5 from Winter 2015.&lt;br /&gt;
&lt;br /&gt;
closures&lt;br /&gt;
&lt;br /&gt;
DOM&lt;br /&gt;
 - document object model&lt;br /&gt;
 - set of JavaScript objects for accessing the current page in the browser&lt;br /&gt;
 &lt;br /&gt;
client-side JS can access the DOM&lt;br /&gt;
  (data structures representing the current web page)&lt;br /&gt;
&lt;br /&gt;
server-side JS can access the operating system&lt;br /&gt;
&lt;br /&gt;
client-side JS is *limited* in its access&lt;br /&gt;
 - because such access is dangerous&lt;br /&gt;
 - ANYONE can run JavaScript in your browser&lt;br /&gt;
 - so, JavaScript in the browser is sandboxed&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
execution sandbox is an environment for running untrusted programs&lt;br /&gt;
 - sandbox limits access and resources&lt;br /&gt;
&lt;br /&gt;
Client-side JavaScript has no native way to&lt;br /&gt;
  (well, it used to...)&lt;br /&gt;
  - access local files&lt;br /&gt;
  - open network connections&lt;br /&gt;
  - run multiple threads&lt;br /&gt;
  - access other windows/programs&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Originally JavaScript was so limited it could do no background processing&lt;br /&gt;
 - could only run when the page was loaded or when the user acted&lt;br /&gt;
&lt;br /&gt;
Microsoft messed it up&lt;br /&gt;
 - Outlook Web Access&lt;br /&gt;
 - they built an ActiveX control which implemented XMLHttpRequest()&lt;br /&gt;
   - but really, it was a way to do GETs and POSTs in the background&lt;br /&gt;
 - other browsers implemented the API natively&lt;br /&gt;
&lt;br /&gt;
Now you could update data in a web page without reloading the page&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====storelogline.js and storelogs.js====&lt;br /&gt;
&lt;br /&gt;
*To use storelogline.js:&lt;br /&gt;
**Find an entry from an existing log file and copy the line&lt;br /&gt;
**Run storelogline.js with the copied line pasted after the script name as command line parameters&lt;br /&gt;
**Optionally, verify that the line was entered in the database by typing the following with the Mongo client:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
mongo&lt;br /&gt;
use log-demo&lt;br /&gt;
db.logs.find()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Writing storelogs.js=====&lt;br /&gt;
&lt;br /&gt;
*We can use the code from storeloglines.js to start building the storelogs.js script for tutorial 5&lt;br /&gt;
To read in a file specified in the command line, you do &amp;lt;code&amp;gt;var data = fs.readFileSync(process.argv[2], ‘utf-8’);&amp;lt;/code&amp;gt;&lt;br /&gt;
**You use &amp;lt;code&amp;gt;process.argv[2]&amp;lt;/code&amp;gt; because in the terminal your third argument is going to be the file to read&lt;br /&gt;
*To split the data into lines, yo do &amp;lt;code&amp;gt;var lines = data.split(‘\n’);&amp;lt;/code&amp;gt;&lt;br /&gt;
*To parse the lines and store them into an array, you can do something like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var entries = [];&lt;br /&gt;
int i; entry, fields, j;&lt;br /&gt;
&lt;br /&gt;
for(i=0;i&amp;lt;lines.length; i++) {&lt;br /&gt;
   if(lines[i] &amp;amp;&amp;amp;lines[i]!==null) {&lt;br /&gt;
      field=lines[i].split(‘ ‘); //this is used to split the lines at index i wherever the spaces occur&lt;br /&gt;
      console.log(split); //when this is printed you’ll see that it splits between all the spaces as well—&amp;gt;you can regular expression to fix this but we fixed it through the while loop&lt;br /&gt;
      entry={};&lt;br /&gt;
      j=0;&lt;br /&gt;
&lt;br /&gt;
      //Used to delete the spaces&lt;br /&gt;
      while(j&amp;lt;field.length) {&lt;br /&gt;
         if(field.lenght[j]===“ “) {&lt;br /&gt;
            field.splice(j, 1);&lt;br /&gt;
         }else{&lt;br /&gt;
            j++;&lt;br /&gt;
         }&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      console.log(field); //—&amp;gt;this prints the logs out without the spaces&lt;br /&gt;
      entry.date=field[0] + “ “ + field[1]; //Than you start populating the array&lt;br /&gt;
      entry.time=field[2];&lt;br /&gt;
      entry.host=field[3];&lt;br /&gt;
      entry.service=field[4].slice(0,-1); //the program will have an error here without the if statement above&lt;br /&gt;
      entry.message=filed.slice(5).join(‘ ‘);&lt;br /&gt;
      //to store in to the entries ( the array )&lt;br /&gt;
      entries.push(entry);&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*To insert into the database you don’t need a loop to iterate. You can use the &amp;lt;code&amp;gt;collection.insert()&amp;lt;/code&amp;gt; method which can take an array (entries) as the argument to insert&lt;br /&gt;
**API’s have documentation and they change because there’s also &amp;lt;code&amp;gt;insertOne()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;insertMany()&amp;lt;/code&amp;gt; that seem to do the same thing&lt;br /&gt;
**For what we want to do &amp;lt;code&amp;gt;insert()&amp;lt;/code&amp;gt; will work&lt;br /&gt;
*For querying the database (assignment 4), there is a trick&lt;br /&gt;
**There are previous assignment solutions that can help you.&lt;br /&gt;
**It&#039;s the previous assignment from last year’s assignment 5&lt;br /&gt;
*Note that when using special characters like [] and () in regular expression, you will need to use a backslash to escape them&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====JavaScript Closures====&lt;br /&gt;
&lt;br /&gt;
*JavaScript closures are about nesting functions&lt;br /&gt;
*For example:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var f = function(x) {&lt;br /&gt;
   function g(y) {&lt;br /&gt;
      return x+y;&lt;br /&gt;
   }&lt;br /&gt;
   return g; //The function g can see x&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var q = f(7);&lt;br /&gt;
var h = f(12);&lt;br /&gt;
console.log(“q = “ +q(4)); //prints 11&lt;br /&gt;
console.log(“h(4) = “ + h(4)); //Prints 16&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*This gives different answers because x is different in two two instances of g&lt;br /&gt;
*You cannot do things like this with C&lt;br /&gt;
*In examforms we have an app.get for list and we have a function in it that has a callback called &amp;lt;code&amp;gt;findCallback()&amp;lt;/code&amp;gt;&lt;br /&gt;
**This callback function has access to the request and response objects because they were in scope at the location where the callback function was defined&lt;br /&gt;
**To have &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; have access to request and response you just need to create different instances of &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; that have their own copies of request and response, therefore one version of &amp;lt;code&amp;gt;findCallback&amp;lt;/code&amp;gt; will have one request and on another call it will have another request&lt;br /&gt;
*Closure is a concept you need to understand because it’s very handy&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Client-Side JavaScript====&lt;br /&gt;
&lt;br /&gt;
*In JavaScript we’ve been doing everything on the server side so far&lt;br /&gt;
*Using the browser dev tools, you can go to console and play around in it. Its almost like the Node environment in a terminal but with some missing features&lt;br /&gt;
*In the browser there is no &amp;lt;code&amp;gt;require()&amp;lt;/code&amp;gt;&lt;br /&gt;
*In the browser the unit is the page but in the server its the file&lt;br /&gt;
*You can access objects representing the page by typing &amp;lt;code&amp;gt;document&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;window&amp;lt;/code&amp;gt;&lt;br /&gt;
*When you load a webpage, it can load and run scripts&lt;br /&gt;
**By running JavaScript within the browser, you can write code that has access to the webpage (in particular, the DOM)&lt;br /&gt;
***The DOM (Document Object Model) is a tree-like structure of objects representing the HTML elements of the page&lt;br /&gt;
&lt;br /&gt;
=====Differences Between Client-Side and Server-Side JavaScript=====&lt;br /&gt;
&lt;br /&gt;
*Client-side JavaScript can access the DOM (data structure repressing the current web page)&lt;br /&gt;
*Server-side JavaScript can access the operating system&lt;br /&gt;
*In client-side JavaScript, you can’t access everything... It&#039;s *limited*&lt;br /&gt;
**That’s because such access is dangerous, the code could be coming from ANYONE&lt;br /&gt;
**ANYONE can run JavaScript in your browser and you don’t want this code messing with your system&lt;br /&gt;
**The browser is designed to protect your system from bad JavaScript, so JavaScript in the browser is sandboxed&lt;br /&gt;
***An execution sandbox is an environment for running untrusted code&lt;br /&gt;
***The sandbox limits access and resources&lt;br /&gt;
***Client-side JavaScript has no native way to:&lt;br /&gt;
****Access local files&lt;br /&gt;
****Open network connections&lt;br /&gt;
****Run multiple threads&lt;br /&gt;
****Access other windows/programs&lt;br /&gt;
*The model of client-side JavaScript was that it should be limited&lt;br /&gt;
*Originally JavaScript was so limited that it could do no background processing&lt;br /&gt;
**It originally could only run when the webpage was loaded or when the user acted&lt;br /&gt;
**It was singly threaded so you had to limit your execution&lt;br /&gt;
**Microsoft messed it up (in particular, the Outlook Web Access team messed it up)&lt;br /&gt;
***They built an ActiveX control which implemented XMLHttpRequest() —&amp;gt; an object that would send a GET or a POST request and would bring back a document that is assumed to be XML&lt;br /&gt;
***But more importantly, it was a way to do GETs and POSTs in the background&lt;br /&gt;
***Other browsers implemented the API natively&lt;br /&gt;
***Now you could update data in a web page without reloading the page&lt;br /&gt;
***Google really ran with this (with gmail, google maps, etc)&lt;br /&gt;
*Client-side JavaScript is the same JavaScript we have been working with except it has access to a webpage now but you access the things you can access the exact same way&lt;br /&gt;
*If you want to change how the current page looks or works you can type JavaScript directly into the browser console (it uses a read-eval-print loop which will affect the page)&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
===nested.js===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
var f = function(x) {&lt;br /&gt;
    function g(y) {&lt;br /&gt;
	return x + y;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    return g;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var q = f(7);&lt;br /&gt;
var h = f(12);&lt;br /&gt;
&lt;br /&gt;
console.log(&amp;quot;q(4) = &amp;quot; + q(4));&lt;br /&gt;
console.log(&amp;quot;h(4) = &amp;quot; + h(4));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===storeLogs.js===&lt;br /&gt;
&lt;br /&gt;
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/storeLogs.js downloadable version]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
// storeLogs.js&lt;br /&gt;
//&lt;br /&gt;
// node storeLogs.js &amp;lt;logfile&amp;gt;&lt;br /&gt;
//&lt;br /&gt;
//&lt;br /&gt;
// The log messages in the file should be of the format:&lt;br /&gt;
// &amp;lt;month&amp;gt; &amp;lt;day of month&amp;gt; &amp;lt;24-hour time in hh:mm:ss&amp;gt; &amp;lt;host&amp;gt; &amp;lt;service name[pid]&amp;gt;: Actual message&lt;br /&gt;
//&lt;br /&gt;
&lt;br /&gt;
var mc = require(&#039;mongodb&#039;).MongoClient;&lt;br /&gt;
var fs = require(&#039;fs&#039;);&lt;br /&gt;
&lt;br /&gt;
var data = fs.readFileSync(process.argv[2], &#039;utf-8&#039;);&lt;br /&gt;
&lt;br /&gt;
var lines = data.split(&#039;\n&#039;);&lt;br /&gt;
&lt;br /&gt;
var entries = [];&lt;br /&gt;
&lt;br /&gt;
var i, j, entry, field;&lt;br /&gt;
&lt;br /&gt;
for (i=0; i&amp;lt;lines.length; i++) {&lt;br /&gt;
    if (lines[i] &amp;amp;&amp;amp; lines[i] !== &#039;&#039;) {&lt;br /&gt;
	field = lines[i].split(&#039; &#039;);&lt;br /&gt;
	entry = {};&lt;br /&gt;
	j = 0;&lt;br /&gt;
	while (j &amp;lt; field.length) {&lt;br /&gt;
	    if (field[j] === &amp;quot;&amp;quot;) {&lt;br /&gt;
		field.splice(j, 1);&lt;br /&gt;
	    } else {&lt;br /&gt;
		j++;&lt;br /&gt;
	    } &lt;br /&gt;
	}&lt;br /&gt;
	entry.date = field[0] + &amp;quot; &amp;quot; + field[1];&lt;br /&gt;
	entry.time = field[2];&lt;br /&gt;
	entry.host = field[3];&lt;br /&gt;
	entry.service = field[4].slice(0,-1);&lt;br /&gt;
	entry.message = field.slice(5).join(&#039; &#039;);&lt;br /&gt;
	entries.push(entry);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// entry.date = process.argv[2] + &amp;quot; &amp;quot; + process.argv[3];&lt;br /&gt;
// entry.time = process.argv[4];&lt;br /&gt;
// entry.host = process.argv[5];&lt;br /&gt;
// entry.service = process.argv[6].slice(0,-1);  // drop the trailing colon&lt;br /&gt;
// entry.message = process.argv.slice(7).join(&#039; &#039;);&lt;br /&gt;
&lt;br /&gt;
var db;&lt;br /&gt;
&lt;br /&gt;
var reportInserted = function(err, result) {&lt;br /&gt;
    if (err) {&lt;br /&gt;
	throw err;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    console.log(&amp;quot;Inserted the following log record:&amp;quot;);&lt;br /&gt;
    console.log(result.ops);&lt;br /&gt;
    db.close();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var connectCallback = function(err, returnedDB) {&lt;br /&gt;
    if (err) {&lt;br /&gt;
	throw err;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    db = returnedDB;&lt;br /&gt;
 &lt;br /&gt;
    db.collection(&#039;logs&#039;).insert(entries, reportInserted);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
mc.connect(&#039;mongodb://localhost/log-demo&#039;, connectCallback);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_12&amp;diff=20760</id>
		<title>WebFund 2016W Lecture 12</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_12&amp;diff=20760"/>
		<updated>2016-03-01T16:53:14Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on February 25, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec12-25Feb2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
Below is examforms.js modified to have MongoDB support.  You can also [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/examforms-mongodb.zip download the full version].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
var http = require(&#039;http&#039;);&lt;br /&gt;
var express = require(&#039;express&#039;);&lt;br /&gt;
var bodyParser = require(&#039;body-parser&#039;);&lt;br /&gt;
var logger = require(&#039;morgan&#039;);&lt;br /&gt;
var port = 3000;&lt;br /&gt;
&lt;br /&gt;
var mc = require(&#039;mongodb&#039;).MongoClient;&lt;br /&gt;
var db, formsCollection;&lt;br /&gt;
&lt;br /&gt;
var state = [];&lt;br /&gt;
&lt;br /&gt;
var app = express();&lt;br /&gt;
&lt;br /&gt;
app.set(&#039;view engine&#039;, &#039;jade&#039;);&lt;br /&gt;
app.set(&#039;views&#039;, __dirname);&lt;br /&gt;
app.use(logger(&#039;dev&#039;));&lt;br /&gt;
app.use(bodyParser.urlencoded({ extended: false }));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
var connectCallback = function(err, returnedDB) {&lt;br /&gt;
    if (err) {&lt;br /&gt;
	throw err;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    db = returnedDB;&lt;br /&gt;
    &lt;br /&gt;
    formsCollection = db.collection(&#039;forms&#039;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
mc.connect(&#039;mongodb://localhost/examforms&#039;, connectCallback);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
app.get(&#039;/&#039;, function(req, res, next) {&lt;br /&gt;
  res.render(&#039;index&#039;, { title: &#039;COMP 2406 Exam form demo&#039; });&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
app.post(&#039;/add&#039;, function(req, res) {&lt;br /&gt;
    var obj = { name: req.body.name,&lt;br /&gt;
		city: req.body.city,&lt;br /&gt;
                country: req.body.country,&lt;br /&gt;
                birthday: req.body.birthday,&lt;br /&gt;
                email: req.body.email };&lt;br /&gt;
    state.push(obj);&lt;br /&gt;
&lt;br /&gt;
    formsCollection.insert(obj, null);&lt;br /&gt;
    res.redirect(&#039;/list&#039;);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
app.get(&#039;/list&#039;, function(req, res) {&lt;br /&gt;
&lt;br /&gt;
    var findCallback = function(err, dbstate) {	&lt;br /&gt;
	if (err) {&lt;br /&gt;
	    console.log(&amp;quot;Couldn&#039;t query database for some reason&amp;quot;);&lt;br /&gt;
	    return;&lt;br /&gt;
	}&lt;br /&gt;
	res.render(&#039;list&#039;, { title: &#039;People Listing&#039;,  items: dbstate});&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    formsCollection.find({}).toArray(findCallback);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
var serverUp = function() {&lt;br /&gt;
    console.log(&amp;quot;ExamForms listening on port &amp;quot; + port);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var serverDown = function() {&lt;br /&gt;
    console.log(&amp;quot;Server shutting down.&amp;quot;);&lt;br /&gt;
    process.exit(0);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var server = http.createServer(app);&lt;br /&gt;
server.listen(port);&lt;br /&gt;
server.on(&#039;listening&#039;, serverUp);&lt;br /&gt;
process.on(&#039;SIGINT&#039;, serverDown);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===Midterms Solutions===&lt;br /&gt;
*Make sure to read the questions carefully as they may use slightly different patterns from the things you have seen previously in class and in the tutorials and assignments&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;strong&amp;gt;1.	 What is the type of the values associated with all of the symbols referenced in line examforms.js:44? Specifically, what is the type of the values bound to server, on, and serverUp?&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: server: object, on: function, serverUp: function&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;2.  If I create a line 46 in examforms.js (i.e., add a line to the end of the file) that says ’console.log(”Finished!”);’, when will finished! be printed relative to the other output of the program? Explain your reasoning.&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: “Finished!” should generally be printed first, before any other output, because all of the other output is produced by callbacks and they are normally executed after the main body of the file has been evaluated. It is acceptable to say that you don’t actually know when it will be printed since you can’t know for certain when callbacks will be invoked; however, the mainly single-threaded computation model of JavaScript makes this sort of interleaving very unlikely (unless you use something like web workers).&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;em&amp;gt;We had an example like this from class before the Midterm:&amp;lt;/em&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;3. If you change the app.post() call to an app.get() call on examforms.js:19, how would the program no longer function properly? Is it possible to repair it without changing this line back to app.get()? (yes or no)&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: It would no longer function properly because the form submission generates a “POST /add” HTTP request and that HTTP request is no longer recognized by the server. Yes. (If you change the form’s method (index.jade:8) to get and make the function access parameters as passed by GETs (req.query rather than req.body) then it would be fixed.)&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;4. (2) As written, examforms returns a 404 error for requests to style.css. What is wrong with examforms? How could you fix it?&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: examforms has no way of serving static files. You’d either have to create a custom route that would load the style sheet manually, or you’d have to include the line normally used in express node applications to add static server support. (Specifically, you’d add app.use (express.static( dirname)); around examforms.js:14.) In other words, the line that adds functionality to serve static files is currently missing.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;5. The request object (req) passed in to route handlers has a “headers” property, just as the request objects in tinywebserver.js. Given this fact, how could you change examforms so that it uses a different style sheet when receiving requests from iPhones? (Describe the general strategy, no need for all the implementation details.)&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;em&amp;gt;//We saw this previously for assignment 3 and this question wasn’t a question of coding it to answer, you should simply understand the concepts behind it.&amp;lt;/em&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: We would just have to check the value of req.headers[“user-agent”] to see if it contained the string “iPhone” in each route handling function. We’d then have to either use different jade files for iPhones that used the different stylesheet, or we’d move the stylesheet from layout.jade and instead pass it in like we do the title of the page.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;6. When a web browser loads a standard web page, what type of HTTP request does it make of the server? And, if the requested URL is invalid (i.e., page not found), what is the standard response code?&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: HTTP GET; 404&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;7. How could you change examforms so you could specify the PORT on the command line, e.g. you’d run node examforms.js 7000 to have it listen on port 7000? If no port is specified it should listen on port 3000.&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;em&amp;gt;//Student question: Will there be any marks for talking about environment variables because it was what was learned in tutorial?&amp;lt;br&amp;gt; &lt;br /&gt;
//Answer: No, the question has nothing to do with environment variables. You must answer specifically what the question asked.&amp;lt;/em&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: Add a line to set the port variable based on the value of process.argv[2], e.g. var port = process.argv[2] || 3000;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;8. What is the purpose of the Content-Type: HTTP response header?&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: It tells the browser the type of content being returned, whether it is a plain text file (text/plain), an HTML document (text/HTML), an image (image/jpeg), etc.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;9. When the call to app.get on lines 15–17 of examforms.js returns, what work has it accomplished? Specifically what data (if any) has been returned to the requesting web client? Why?&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;em&amp;gt;//Note that the function is only being defined, not run. It is only run when a request is received (which is not what has happened here)!!&amp;lt;/em&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: After these lines have been written a function has been defined and it has been registered as a callback for an HTTP GET request. The anonymous function has not been called; it will only be invoked when there is an HTTP GET request.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;10. When is the function serverDown(), on lines 37-40 of examforms.js, called? How can a user cause this function to be called?&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: This function is called when the server receives a SIGINT signal. This signal is generated when a user presses Ctrl-C at the command line.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;11. What would happen to examforms if we deleted layout.jade:6 (block header)? What about layout.jade:8 (block content)?&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: If you delete line 6, nothing would happen as this block isn’t used in any of the templates. Deleting line 8 would make the body of all pages blank.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;12. The each statement on list.jade:16 is a loop. What does this loop do? Where is it executed (server or browser)?&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: This loop generates a table line for every person stored in the state array (passed to the template on examforms.js:30). It runs/ is EXECUTED on the server as it is a Jade loop. The browser simply gets a fully populated table.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;13. What would happen (if anything) if you removed two spaces from the beginning of list.jade:25, button(type=&amp;quot;submit&amp;quot;) Home?&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: By removing these spaces the button is no longer indented under the form on line 24; thus now it is a submit button that is no longer enclosed inside of an HTML form. Thus while the button will still be present in the page, clicking it will now do nothing.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;14. Do web browsers normally run/interpret Jade code? Explain briefly.&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: Web browsers do not interpret Jade code; instead, they interpret the HTML that is produced by compiling Jade templates.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;strong&amp;gt;15. What does res.redirect(/list); on line 26 of examforms.js do? Specifically, what response is (eventually or immediately) sent back to the browser after this line is executed? Explain briefly.&amp;lt;/strong&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A: This line causes a 302 (permanent redirect) response to be sent back to the browser with /list as the new location. The browser then proceeds to do an HTTP GET /list which then causes the callback for /list to be run on the server (and return the corresponding page).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using a Database with Examforms===&lt;br /&gt;
&lt;br /&gt;
*Question: We want to write records to the database, how?&lt;br /&gt;
*We will need to modify the code in order to give it the ability to interact with the database&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Establishing a Connection====&lt;br /&gt;
&lt;br /&gt;
*First we add the code needed to establish a connection to the database:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var mc = require(‘Mongodb’).MongoClient;&lt;br /&gt;
mc.connect(‘mongodb://localhost/examforms’, connectCallback);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*This allows you to connect to the examforms database via the Mongo server running on localhost using the mongodb protocol (as specified by the first argument to the &amp;lt;code&amp;gt;connect()&amp;lt;/code&amp;gt; method)&lt;br /&gt;
*Note that we need the Mongo server running in order for this code to run properly&lt;br /&gt;
*Recall that if you’re using the course VM, Mongo is installed; if using you’re own machine, you must install Mongo to be able to use it&lt;br /&gt;
**We can verify that Mongo is running by opening a new terminal and typing &amp;lt;code&amp;gt;mongo&amp;lt;/code&amp;gt; to run the terminal client&lt;br /&gt;
**If the Mongo server is not running, we will see a message stating that the connection has failed&lt;br /&gt;
**We would get a similar error if our application tries to establish a connection while Mongo is not running&lt;br /&gt;
*Next, we need to implement connectCallback&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var connectCallback = function(err, returnedDB) {&lt;br /&gt;
    if (err) {&lt;br /&gt;
	throw err;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    db = returnedDB;&lt;br /&gt;
    formsCollection = db.collection(&#039;forms&#039;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*&amp;lt;code&amp;gt;db&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;formsCollection&amp;lt;/code&amp;gt; should be defined beforehand at the global scope so that we will have access to them in other callback functions later&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Inserting Documents into the Database/Collection====&lt;br /&gt;
&lt;br /&gt;
*Now we can add code to store the data that we receive from form submissions (currently stored in the &amp;lt;code&amp;gt;obj&amp;lt;/code&amp;gt; variable)&lt;br /&gt;
*This code should be added inside the function defined for the /add route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
formsCollection.insert(obj, null); //pass null because we are being explicit and don’t want to pass anything as the callback function&lt;br /&gt;
res.redirect(‘/list’);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*To verify if we have properly inserted anything into the database, we can use the terminal client&lt;br /&gt;
*Mongo has many commands that can be used to tell you the current state of your database&lt;br /&gt;
*The db.serverStatus() command (from the terminal client) returns an overview of the status of the database. Includes disk usage, memory use, connection, journaling, and index access&lt;br /&gt;
*For other administration requirements that you may run across visit [https://docs.mongodb.org/manual/administration/monitoring/ https://docs.mongodb.org/manual/administration/monitoring/]&lt;br /&gt;
*To check if the document was actually stored in the collection, in the terminal client type:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
use examforms&lt;br /&gt;
db.forms.find()&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*This will display the documents in the forms collection within the examforms database&lt;br /&gt;
*To clear data from the collection, you can use the &amp;lt;code&amp;gt;drop()&amp;lt;/code&amp;gt; method of the collection in order to drop (remove) the entire collection&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Querying the Database/Collection====&lt;br /&gt;
&lt;br /&gt;
*In order to now display the list of people that have been entered via the form, we need to be able to query the database&lt;br /&gt;
*We can add the following code to the function defined for the /list route&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
formsCollection.find({}).toArray(findCallback);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*The argument passed to the &amp;lt;code&amp;gt;find()&amp;lt;/code&amp;gt; method is our query&lt;br /&gt;
**Here, the query is an empty object so every document will be matched&lt;br /&gt;
*The return value of the &amp;lt;code&amp;gt;find()&amp;lt;/code&amp;gt; method is a cursor object used to iterate through the results&lt;br /&gt;
*We are calling the &amp;lt;code&amp;gt;toArray()&amp;lt;/code&amp;gt; method of the cursor object to get all the results as an array&lt;br /&gt;
**The argument to this method is the callback function where we will have access to the array&lt;br /&gt;
**Since the callback function is the only place where we have access to the results, we must send a response to the client from there&lt;br /&gt;
***This means that the callback function must be defined within the scope of the response object&lt;br /&gt;
*Look into MongoDB API online for help with assignment 4, specifically &amp;lt;code&amp;gt;toArray()&amp;lt;/code&amp;gt;: [https://mongodb.github.io/node-mongodb-native/api-generated/cursor.html#toarray https://mongodb.github.io/node-mongodb-native/api-generated/cursor.html#toarray]&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_11&amp;diff=20749</id>
		<title>WebFund 2016W Lecture 11</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_11&amp;diff=20749"/>
		<updated>2016-02-28T21:49:07Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on February 23, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec11-23Feb2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In-class Notes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 11&lt;br /&gt;
----------&lt;br /&gt;
&lt;br /&gt;
Databases&lt;br /&gt;
&lt;br /&gt;
/var/spool/mail&lt;br /&gt;
  - where email is stored&lt;br /&gt;
  - one file per user&lt;br /&gt;
&lt;br /&gt;
my mail inbox in /var/spool/mail/soma&lt;br /&gt;
&lt;br /&gt;
Who accesses this file?&lt;br /&gt;
  - mail client&lt;br /&gt;
  - mail server&lt;br /&gt;
&lt;br /&gt;
What happens if the mail server delivers mail while I&#039;m going&lt;br /&gt;
through my email (deleting, filing)&lt;br /&gt;
 - unless you lock it, you lose mail, mail client will overwrite&lt;br /&gt;
   inbox and delete new messages&lt;br /&gt;
 - solution: lock the inbox&lt;br /&gt;
     create a soma.lock file&lt;br /&gt;
     (inside it has the process ID of the accessing process)&lt;br /&gt;
&lt;br /&gt;
This is a bad idea that does not scale&lt;br /&gt;
Instead, use a database&lt;br /&gt;
 - concurrent access&lt;br /&gt;
 - data persistence&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What kind of database?&lt;br /&gt;
 - key-value stores&lt;br /&gt;
    like a filesystem for very small files,&lt;br /&gt;
    with search&lt;br /&gt;
 - document store  &amp;lt;-- we&#039;ll use this&lt;br /&gt;
    values have structure&lt;br /&gt;
    can index on values&lt;br /&gt;
 - relational database&lt;br /&gt;
    sets of tables&lt;br /&gt;
    tables have rows and columns&lt;br /&gt;
    relations between tables&lt;br /&gt;
    example: names and addresses&lt;br /&gt;
      - rows are individual people&lt;br /&gt;
      - columns are first name, last name, address, city, etc.&lt;br /&gt;
    relation example: customers and purchases&lt;br /&gt;
      - each purchase (invoice) has item purchased, price, and&lt;br /&gt;
        customer ID&lt;br /&gt;
      - customer table connects customer ID and name, address, etc&lt;br /&gt;
&lt;br /&gt;
We&#039;re going to use MongoDB, a document store, not a relational&lt;br /&gt;
database.&lt;br /&gt;
  - stores JSON-like documents&lt;br /&gt;
  - scriptable in JavaScript&lt;br /&gt;
&lt;br /&gt;
If we were to use a relational database, we&#039;d have to learn SQL&lt;br /&gt;
  - Structured Query Language&lt;br /&gt;
  - many man implementations&lt;br /&gt;
     Oracle, MySQL (MariaDB), PostgreSQL, MS SQL Server, SQLite, etc&lt;br /&gt;
  - you mainly need a relational database to handle transactions&lt;br /&gt;
&lt;br /&gt;
Transaction&lt;br /&gt;
 - what if a change to a database involves multiple tables?&lt;br /&gt;
 - a transaction ensures all tables are changed or none are&lt;br /&gt;
&lt;br /&gt;
Classic example: airline tickets&lt;br /&gt;
&lt;br /&gt;
Tables:&lt;br /&gt;
 - seat reservations&lt;br /&gt;
 - payment&lt;br /&gt;
 - customer info&lt;br /&gt;
&lt;br /&gt;
When you buy a ticket, you want to change all three&lt;br /&gt;
&lt;br /&gt;
A transaction ensures atomic behavior&lt;br /&gt;
  all happen or none happen&lt;br /&gt;
&lt;br /&gt;
MongoDB        Relational DB&lt;br /&gt;
Document   =&amp;gt;  row&lt;br /&gt;
Collection =&amp;gt;  table&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web Architecture&lt;br /&gt;
----------------&lt;br /&gt;
&lt;br /&gt;
web browser (interprets HTML, CSS, JavaScript)&lt;br /&gt;
&lt;br /&gt;
  |&lt;br /&gt;
  |  HTTP&lt;br /&gt;
  |&lt;br /&gt;
&lt;br /&gt;
web server (runs arbitrary code, in this class JavaScript in node)&lt;br /&gt;
&lt;br /&gt;
  |&lt;br /&gt;
  |  SQL or MongoDB protocol&lt;br /&gt;
  |&lt;br /&gt;
&lt;br /&gt;
database (persistent store, allows concurrent access safely)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
====Databases====&lt;br /&gt;
=====Problems with Examforms=====&lt;br /&gt;
*Previously, we used and array (&amp;lt;code&amp;gt;var state = [];&amp;lt;/code&amp;gt;) to store the data entered in the form&lt;br /&gt;
*The data stored in &amp;lt;code&amp;gt;state&amp;lt;/code&amp;gt; is not persistent meaning it is lost when the server is shut down (stored in RAM)&lt;br /&gt;
*We could try storing the data in a file instead&lt;br /&gt;
**This quickly becomes problematic when we have multiple machines trying to work with the same file (concurrent access)&lt;br /&gt;
&lt;br /&gt;
=====General Motivation=====&lt;br /&gt;
*&amp;lt;code&amp;gt;/var/spool/mail&amp;lt;/code&amp;gt;&lt;br /&gt;
**The place where email is stored and one file is assigned per user&lt;br /&gt;
**My Inbox: &amp;lt;code&amp;gt;/var/spool/mail/soma&amp;lt;/code&amp;gt;&lt;br /&gt;
*Who have the ability to access the file?&lt;br /&gt;
**Mail client&lt;br /&gt;
**Mail server&lt;br /&gt;
*What happens if the mail server delivers mail while I’m going through my email?&lt;br /&gt;
**(deleting, filing)?&lt;br /&gt;
**Unless we don’t lock it, we will lose the mail&lt;br /&gt;
**The mail client will overwrite inbox and delete new incoming messages&lt;br /&gt;
*Solution: lock the inbox&lt;br /&gt;
**Create a soma.lock file (Inside it has the process id of the accessing process)&lt;br /&gt;
**This is bad idea that does not scale&lt;br /&gt;
&lt;br /&gt;
=====A Database As a Solution=====&lt;br /&gt;
*A database offers:&lt;br /&gt;
**Concurrent access&lt;br /&gt;
**Data persistence&lt;br /&gt;
*Now we know what the database does and why should we use it. But what kind of database should we use?&lt;br /&gt;
&lt;br /&gt;
======Key Value Store======&lt;br /&gt;
*Simple association of keys with values&lt;br /&gt;
*Like a file system for a small files, with search&lt;br /&gt;
&lt;br /&gt;
======Document Store======&lt;br /&gt;
*Similar to key value stores&lt;br /&gt;
*Values have a structure&lt;br /&gt;
*Able to do indexing&lt;br /&gt;
*We’re going to use MongoDB, a document store&lt;br /&gt;
**Stores JSON-like documents (actually BSON)&lt;br /&gt;
**Scriptable in JavaScript&lt;br /&gt;
&lt;br /&gt;
======Relational Database (Gold Standard)======&lt;br /&gt;
*Sets of tables&lt;br /&gt;
*Tables have rows and columns&lt;br /&gt;
*Relations between tables&lt;br /&gt;
*Example: names and addresses&lt;br /&gt;
**Where rows are individual people and columns are first name, last name, address, city etc.&lt;br /&gt;
*Another example: customers and purchases&lt;br /&gt;
**Each purchase is a row in the purchases table (the row has item purchased, price and customer ID)&lt;br /&gt;
**The customer table connects name, address etc via the customer ID&lt;br /&gt;
*If we were to use a relational database, we’d have to lean SQL (Structured Query Language)&lt;br /&gt;
**There are a lot of implementations:&lt;br /&gt;
***Oracle&lt;br /&gt;
***MySQL(MariaDB)&lt;br /&gt;
***PostgresQL&lt;br /&gt;
***MS SQL Server&lt;br /&gt;
***SQLite&lt;br /&gt;
*You mainly need a relational database to handle transactions&lt;br /&gt;
**What if a change to a database involves a change to multiple tables?&lt;br /&gt;
**A transaction ensures all tables are changed or none are&lt;br /&gt;
**Classic example: airline tickets&lt;br /&gt;
***Tables:&lt;br /&gt;
****Seat reservations&lt;br /&gt;
****Payment&lt;br /&gt;
****Customer info&lt;br /&gt;
***When you buy a ticket, you  would want to change all three tables&lt;br /&gt;
***A transaction ensures atomic behavior&lt;br /&gt;
***All changes happen or none happen&lt;br /&gt;
&lt;br /&gt;
====Storageline.js====&lt;br /&gt;
&lt;br /&gt;
=====MongoDB=====&lt;br /&gt;
*We will be working with MongoDB as the database that our web applications will use&lt;br /&gt;
*When compared to a relational database, the collections of MongoDB are like tables and the documents of MongoDB are like rows&lt;br /&gt;
*MongoDB is already installed and is running on the course VM&lt;br /&gt;
**If you are using your own machine, you will have to install it&lt;br /&gt;
**You can run MongoDB by typing &amp;lt;code&amp;gt;mongod&amp;lt;/code&amp;gt; in the terminal&lt;br /&gt;
*MongoDB listens on 27017 port by default&lt;br /&gt;
*You can run a Mongo client from the terminal by typing &amp;lt;code&amp;gt;mongo&amp;lt;/code&amp;gt;&lt;br /&gt;
**From here you can interact with the database&lt;br /&gt;
&lt;br /&gt;
=====Interacting With the Database from a Script=====&lt;br /&gt;
*We use the npm module &amp;lt;code&amp;gt;mongodb&amp;lt;/code&amp;gt; to make our web application interact as a client with the MongoDB server&lt;br /&gt;
*The Mongo server (MongoDB) must be running in order for the application to be able to communicate with it (the same goes for the terminal client)&lt;br /&gt;
*In this script, we are saving log file entries into the database&lt;br /&gt;
**Logs are records left by the system and other applications&lt;br /&gt;
**We will be using a syslog-like logs in the tutorial&lt;br /&gt;
**Our goal is to parse and store it the log entries in a Mongo collection&lt;br /&gt;
*Notice the use of &amp;lt;code&amp;gt;mc.connect()&amp;lt;/code&amp;gt; in the script which is used to establish a connection to the database. It takes 2 arguments:&lt;br /&gt;
**A string with the address of the database to access &lt;br /&gt;
**A callback function which runs once the connection is established (or the application has failed to establish a connection)&lt;br /&gt;
*From within the callback function, we can now access the collections of the database that we have connected to&lt;br /&gt;
*Here, we use the &amp;lt;code&amp;gt;insert()&amp;lt;/code&amp;gt; method of the collection to store a log entry as a document&lt;br /&gt;
*Every document in a collection has to have a unique ID&lt;br /&gt;
**The ID is generated automatically during an insertion&lt;br /&gt;
**It is also known as a primary key which helps us to distinguish between documents&lt;br /&gt;
&lt;br /&gt;
=====Checking the Content of a Collection=====&lt;br /&gt;
*An example of looking up documents in a collection might look like:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
mongo //launches the Mongo client&lt;br /&gt;
show dbs //lists all databases&lt;br /&gt;
use demo-log //indicates which database we wish to use&lt;br /&gt;
show collections //lists all collections in the current databse&lt;br /&gt;
db.logs.find() //shows all documents in the logs collection&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Web Architecture=====&lt;br /&gt;
*The database acts as a server which our web server talks to&lt;br /&gt;
*Our web server is thus a client of the database server&lt;br /&gt;
*A web browser talks to our server using HTTP&lt;br /&gt;
*Our server talks to the Database using SQL or a database-specific protocol (such as the MongoDB protocol)&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_10&amp;diff=20698</id>
		<title>WebFund 2016W Lecture 10</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_10&amp;diff=20698"/>
		<updated>2016-02-10T15:29:35Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on February 9, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec10-09Feb2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===Assignment 3 Solutions===&lt;br /&gt;
&lt;br /&gt;
*Solutions to the assignment questions have been posted on the [assignment 3 page http://homeostasis.scs.carleton.ca/wiki/index.php/WebFund_2016W:_Assignment_3]&lt;br /&gt;
*Below is a review of these solutions&lt;br /&gt;
&lt;br /&gt;
====Q1====&lt;br /&gt;
*What is the type of:&lt;br /&gt;
**&amp;lt;code&amp;gt;line&amp;lt;/code&amp;gt;: lines may be classified as an object but more specifically it is an array&lt;br /&gt;
**&amp;lt;code&amp;gt;rawContents&amp;lt;/code&amp;gt;: rawContents is of type string&lt;br /&gt;
**&amp;lt;code&amp;gt;split()&amp;lt;/code&amp;gt;: is a function. It is also acceptable to say that it is a method&lt;br /&gt;
&lt;br /&gt;
====Q2====&lt;br /&gt;
*Line 24 should be printed first&lt;br /&gt;
*Since JavaScript is single threaded there is a high guarantee it is printed first since the program runs asynchronously&lt;br /&gt;
*In general, you can consider the program to do all of its main sequential work first and to then check for callback functions from asynchronous calls that must be run&lt;br /&gt;
&lt;br /&gt;
====Q3====&lt;br /&gt;
*A one line change would be adding a line at line 42 by setting &amp;lt;code&amp;gt;status = 404;&amp;lt;/code&amp;gt;&lt;br /&gt;
*We can also change the docroot but it&#039;s an unsafe way&lt;br /&gt;
&lt;br /&gt;
====Q4====&lt;br /&gt;
*Just before line 92 we can add another if statement&lt;br /&gt;
*We check to see if the user-agent headers exists and whether it contains iPhone as a substring&lt;br /&gt;
**If the condition is met, we respond with 403&lt;br /&gt;
&lt;br /&gt;
====Q5====&lt;br /&gt;
=====A=====	&lt;br /&gt;
*By commenting out this line, &amp;lt;code&amp;gt;app&amp;lt;/code&amp;gt; will no longer be defined&lt;br /&gt;
*Anything referencing app will produce a reference error so the program crashes on the first line that uses app&lt;br /&gt;
&lt;br /&gt;
======Important Point======&lt;br /&gt;
*What is the difference between JavaScript&#039;s &amp;lt;code&amp;gt;require&amp;lt;/code&amp;gt; and C&#039;s &amp;lt;code&amp;gt;include&amp;lt;/code&amp;gt;?&lt;br /&gt;
**In C &amp;lt;code&amp;gt;include&amp;lt;/code&amp;gt; looks similar to a &amp;lt;code&amp;gt;require&amp;lt;/code&amp;gt; but in reality it’s a very different mechanism.&lt;br /&gt;
**The &amp;lt;code&amp;gt;include&amp;lt;/code&amp;gt; in C pre-processes the included file to put all of its code into the file that included it&lt;br /&gt;
***Symbols are thus defined for us but there is no visible code! We cant tell what kind of symbols include has brought in&lt;br /&gt;
**In JavaScript the file we require is pure JavaScript code!&lt;br /&gt;
***The required script will run and put some value into its module.exports which is then returned via the require call&lt;br /&gt;
***We know what symbol we are working with because we assign the return value of the require call ourselves&lt;br /&gt;
&lt;br /&gt;
=====B=====&lt;br /&gt;
*Setting var port = 3000;&lt;br /&gt;
*It works perfectly fine but we can no longer change the port using the PORT environment variable&lt;br /&gt;
&lt;br /&gt;
=====C=====&lt;br /&gt;
*Since we change the directory, it can no longer find “views”&lt;br /&gt;
*This means that the server will not be able to find any templates to render&lt;br /&gt;
&lt;br /&gt;
=====D=====&lt;br /&gt;
*Commenting out line 24&lt;br /&gt;
*We are no longer exporting anything from the file&lt;br /&gt;
*Any calls to require the file will not get an expected return value from the require call and so the server will not work properly&lt;br /&gt;
&lt;br /&gt;
=====E=====&lt;br /&gt;
*This line acts as a placeholder for child templates to put content into the body of this template&lt;br /&gt;
*By removing it, the child templates have nowhere to put the content in the body&lt;br /&gt;
&lt;br /&gt;
=====F=====&lt;br /&gt;
*When we indent the code in this way, the submit button is no longer a part of the form&lt;br /&gt;
*The button will therefore not cause the form to be submitted&lt;br /&gt;
*Essentially, the button no longer does anything&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Possible questions for the Midterm===&lt;br /&gt;
&lt;br /&gt;
*What is the purpose of app.post? What if &amp;lt;code&amp;gt;app.post(‘/add’, ...)&amp;lt;/code&amp;gt; were be changed to &amp;lt;code&amp;gt;app.get(&#039;/add&#039;, ...)&amp;lt;/code&amp;gt;?&lt;br /&gt;
**Answer: The server will no longer know how to deal with any incoming POST requests for /add so it will not properly handle the form submission&lt;br /&gt;
***Instead, the server could handle an incoming GET request for the form submission (but there are some other problems that would need to be fixed for parsing the incoming data)&lt;br /&gt;
*How can we keep it a get and still make it work?&lt;br /&gt;
**Answer: Change the method in the form from method “post” to “get”&lt;br /&gt;
***POST requests have the form data in their body whereas a GET request stores it in a query strings&lt;br /&gt;
***So we can change &amp;lt;code&amp;gt;req.body&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;req.query&amp;lt;/code&amp;gt; in the routing function to access the data properly&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*What is &amp;lt;code&amp;gt;app.use()&amp;lt;/code&amp;gt;?&lt;br /&gt;
**What happens when we comment out the line &amp;lt;code&amp;gt;app.use( ‘logger&#039;..)&amp;lt;/code&amp;gt;?&lt;br /&gt;
***The server no longer logs incoming request to the console&lt;br /&gt;
**What if we comment out &amp;lt;code&amp;gt;app.use(body.parser...)&amp;lt;/code&amp;gt;?&lt;br /&gt;
***The program would crash because it wouldn’t be able to find &amp;lt;code&amp;gt;req.body&amp;lt;/code&amp;gt; since the body of the request has no been parsed&lt;br /&gt;
**What is the purpose of &amp;lt;code&amp;gt;app.use(express.static...)&amp;lt;/code&amp;gt;?&lt;br /&gt;
***Its tells the server to use a static web server to serve basic webpages and other static resources&lt;br /&gt;
*So &amp;lt;code&amp;gt;app.use()&amp;lt;/code&amp;gt; is telling the server to use a given module to do something with the incoming requests&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_6&amp;diff=20694</id>
		<title>WebFund 2016W Lecture 6</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_6&amp;diff=20694"/>
		<updated>2016-02-09T21:02:01Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on January 26, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec06-26Jan2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In-class Notes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture #6&lt;br /&gt;
---------&lt;br /&gt;
&lt;br /&gt;
PLAN&lt;br /&gt;
* Assignment 2&lt;br /&gt;
  - aliases&lt;br /&gt;
  - headers&lt;br /&gt;
  - other clarifications?&lt;br /&gt;
&lt;br /&gt;
* Objects&lt;br /&gt;
  - associative hashes with inheritance&lt;br /&gt;
  - prototypes using Object.create&lt;br /&gt;
  - &amp;quot;global&amp;quot; object&lt;br /&gt;
  - &amp;quot;Object&amp;quot; object&lt;br /&gt;
  - Object.prototype&lt;br /&gt;
  - hasOwnProperty and Object.hasOwnProperty&lt;br /&gt;
&lt;br /&gt;
* Function calling and &amp;quot;this&amp;quot;&lt;br /&gt;
 - functions in regular scope&lt;br /&gt;
 - methods&lt;br /&gt;
 - constructors with new&lt;br /&gt;
 - call/apply&lt;br /&gt;
 - functions in global scope&lt;br /&gt;
&lt;br /&gt;
* form demo!&lt;br /&gt;
&lt;br /&gt;
------------------&lt;br /&gt;
&lt;br /&gt;
If a function starts with a capital letter, it is an object&lt;br /&gt;
constructor that you call with new.&lt;br /&gt;
 - why?  because it keeps JavaScript programmers sort of sane&lt;br /&gt;
&lt;br /&gt;
if (x.a) { blah}     instead do &lt;br /&gt;
&lt;br /&gt;
if (x.hasOwnProperty(a) &amp;amp;&amp;amp; x.a) better, but&lt;br /&gt;
&lt;br /&gt;
if (hasOwnProperty.call(x,a) &amp;amp;&amp;amp; x.a) is even better&lt;br /&gt;
&lt;br /&gt;
Why not tinywebserver?&lt;br /&gt;
&lt;br /&gt;
* templates: otherwise, have to manually generate web pages using&lt;br /&gt;
  variable data&lt;br /&gt;
* routing: otherwise, you&#039;ll be using regexp&#039;s to match each &lt;br /&gt;
  incoming URL and then call the right function&lt;br /&gt;
  &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====Assignment 2====&lt;br /&gt;
*Aliases: A fake name used instead of something the real name&lt;br /&gt;
**In the assignment, we take one URL and replace with another URL&lt;br /&gt;
*Headers: Headers on the incoming request&lt;br /&gt;
**Stored in a headers object in the request object. The request object has lots of things, one of those is request.headers&lt;br /&gt;
**For the header logging, just iterate through the array of headers to log and check if it is in the request.headers object  &lt;br /&gt;
&lt;br /&gt;
====Objects====&lt;br /&gt;
&lt;br /&gt;
=====JSON=====&lt;br /&gt;
*In Javascript, we can define an object using the following syntax:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;var x = {a: 5, b: true};&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
*For JSON, we must use quotes as follows:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;{“a”: “5”, “b”: “true”}&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Dashes in Attribute Names=====&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
//This must be quoted, otherwise it will produce an error&lt;br /&gt;
x.anil-memory = “long ago”;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
//The correct syntax is shown here. Use this when the attribute name contains irregular characters&lt;br /&gt;
x[”anil-memory”] = “long ago”;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
//Quotes must also be used when creating an object name with a dash during the definition of an object&lt;br /&gt;
var x = {a: 5, b: true, &amp;quot;anil-memory&amp;quot; : &amp;quot;long ago&amp;quot;};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Prototypes=====&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
//This is an example of how inheritance works in JavaScript, is uses prototypes instead of being class based&lt;br /&gt;
//For another example see: http://robertnyman.com/2008/10/06/javascript-inheritance-how-and-why/)&lt;br /&gt;
Object -&amp;gt; [Function Object]&lt;br /&gt;
Object.prototype -&amp;gt; {}&lt;br /&gt;
Object.prototype.what = “yep”; &lt;br /&gt;
x.what -&amp;gt; yep&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*To make use of object prototyping, use &amp;lt;code&amp;gt;Object.create()&amp;lt;/code&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
//Object.create() is used in a similar fashion to the way &amp;quot;new&amp;quot; is used, it creates a new object.&lt;br /&gt;
//(x is going to be prototype for z)&lt;br /&gt;
z = Object.create(x); -&amp;gt; {}&lt;br /&gt;
z -&amp;gt; {}&lt;br /&gt;
z.whoami -&amp;gt; x&lt;br /&gt;
z[“anil-memory”] -&amp;gt; ‘long ago’&lt;br /&gt;
z.b -&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*What is &amp;quot;new&amp;quot; for? What does &amp;quot;new&amp;quot; do: &lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var f = function(a) {this.myval = a};&lt;br /&gt;
var g = new f(7);&lt;br /&gt;
g -&amp;gt; {myval: 7}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*What actually happened here was “this” became an empty object. When you use &amp;quot;new&amp;quot;, &amp;quot;this&amp;quot; becomes and empty object with the prototype of the object it was constructed from. Don’t use &amp;quot;new&amp;quot;, too confusing. Use &amp;lt;code&amp;gt;Object.create()&amp;lt;/code&amp;gt; instead. &lt;br /&gt;
*How to tell if you are supposed to use &amp;quot;new&amp;quot;: if a function starts with a capital letter, it is meant to be used as a constructor function. In this case, use &amp;quot;new&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=====Checking Object Properties=====&lt;br /&gt;
*Method for checking just its own property:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
x.hasOwnProperty(“what”); -&amp;gt; false&lt;br /&gt;
x.hasOwnProperty(“b”) -&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
*This matters is when users are going to provide the keys for the object&lt;br /&gt;
&lt;br /&gt;
=====Use of &amp;quot;this&amp;quot;=====&lt;br /&gt;
*When used from within a call to a method, &amp;lt;code&amp;gt;this&amp;lt;/code&amp;gt; refers to the object that the method belongs to&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var f = function(p) { console.log(&amp;quot;this.whoami = &amp;quot; + this.whoami); }&lt;br /&gt;
f -&amp;gt; [Function]&lt;br /&gt;
x.f = f; -&amp;gt; [Function]&lt;br /&gt;
x.f(); -&amp;gt; this.whoami = x;&lt;br /&gt;
&lt;br /&gt;
var y = {p:2, whoami: “y”};&lt;br /&gt;
y.f = f;&lt;br /&gt;
y.f(); -&amp;gt; this.whoami = y&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*The call method calls a function using the argument provided as the value of &amp;lt;code&amp;gt;this&amp;lt;/code&amp;gt;&lt;br /&gt;
*Example: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Function/call &lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var k = {k:7, whoami: “k”};&lt;br /&gt;
f.call(k); -&amp;gt; this.whoami = k&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*When &amp;lt;code&amp;gt;this&amp;lt;/code&amp;gt; is used in a function, it refers to the global object&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var f = function(a) {this.myval = a};&lt;br /&gt;
f -&amp;gt; [Function]&lt;br /&gt;
var a = {};&lt;br /&gt;
a.f = f;&lt;br /&gt;
a.f(3);&lt;br /&gt;
a -&amp;gt; {f: [Function], myval: 3}&lt;br /&gt;
f(5); -&amp;gt; undefined&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*The last line of the code above sets the myval attribute of the global object to 5.&lt;br /&gt;
*When used not in context of an object, you get the global object (which is almost always what you do NOT want to do)&lt;br /&gt;
&lt;br /&gt;
====Form Demo====&lt;br /&gt;
&lt;br /&gt;
=====Setup=====&lt;br /&gt;
&lt;br /&gt;
*We will be using the Express framework to set up a web application&lt;br /&gt;
*In a terminal, enter:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
mkdir testapp&lt;br /&gt;
cd testapp&lt;br /&gt;
sudo npm install express-generator –g&lt;br /&gt;
express –version&lt;br /&gt;
express&lt;br /&gt;
npm install&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*npm is the Node packet manager&lt;br /&gt;
**&amp;quot;npm install&amp;quot; installs all the needed files in order for programs to run, it knows how to do this because the dependencies of the application are stored inside the package.json file&lt;br /&gt;
*To run the application, you can enter &amp;lt;code&amp;gt;bin/www&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Folder Structure=====&lt;br /&gt;
*Routes: code that’s going to run for different incoming requests&lt;br /&gt;
**This code will be in Routes/index.js&lt;br /&gt;
*Views: templates&lt;br /&gt;
**We will be working with Jade templates&lt;br /&gt;
*Routes/index.js :This directs which scripts to be called for when you go to certain pages.&lt;br /&gt;
*bin: this contains the startup script for the server&lt;br /&gt;
*Public: This is where all of the static resources are stored&lt;br /&gt;
**We can put HTML, stylesheets, images, etc in here&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_9&amp;diff=20690</id>
		<title>WebFund 2016W Lecture 9</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_9&amp;diff=20690"/>
		<updated>2016-02-09T05:42:22Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on February 4, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec09-04Feb2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===Agenda===&lt;br /&gt;
&lt;br /&gt;
*We are now a week away from the midterm (February 11th, 2016)&lt;br /&gt;
*The solutions for Assignment 3 will be reviewed in class on Tuesday, February 9th&lt;br /&gt;
*In this class the focus was on extending the functionality of tinyweb.js to that of form-demo.js and examforms.js in lieu of introducing new topics before the midterm&lt;br /&gt;
&lt;br /&gt;
===Version Control===&lt;br /&gt;
&lt;br /&gt;
*Question: How many people have used CVS or Github previously? Has version control been covered in previous courses?&lt;br /&gt;
**Majority of class indicated that version control has not been a topic covered in previous courses. &lt;br /&gt;
*Pros of version control:	&lt;br /&gt;
**The ability to create backups and branch code&lt;br /&gt;
**Github is a great resource for powerful collaboration, code review, and code management for open source and private projects. Public projects are always free. (https://github.com/ )&lt;br /&gt;
*Git was originally developed for the Linux kernel.&lt;br /&gt;
*Code Academy has a Github tutorial - https://www.codecademy.com/learn/learn-git &lt;br /&gt;
&lt;br /&gt;
*Diff command was recommended – analyzes two files and prints the lines that different. (diff file1.js file2.js)  &lt;br /&gt;
**e.g diff ~/Documents/Class/WebFund/2016W/code tinwebserver.js tinywebserver_solution&lt;br /&gt;
&lt;br /&gt;
===tinyweb.js vs form-demo.js===&lt;br /&gt;
*They can both serve static files&lt;br /&gt;
*tinyweb.js:&lt;br /&gt;
**tinyweb.js serves static content from the directory specified as the options directory&lt;br /&gt;
**A file on disk is served for a particular URL. As a result, the content always remains the same (static content)&lt;br /&gt;
*form-demo.js:&lt;br /&gt;
**form-demo serves static content form a public directory&lt;br /&gt;
**Can handle form submissions... this depends upon handling dynamic content since the whole point is to change the state of the web server&lt;br /&gt;
**Supports jade templates&lt;br /&gt;
**Can run code to generate pages... meaning that there are functions that are run every time a request is made for a particular URL (dynamic content)&lt;br /&gt;
***e.g in form-demo/routes/index.js:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
router.get(&#039;/&#039;, function(req, res, next) {&lt;br /&gt;
   res.render(&#039;index&#039;, { title: &#039;COMP 2406 Simple form demo&#039; });&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Coding Exercise - Adding More Functionality To tinyweb.js===&lt;br /&gt;
*To get started, it’s better to initially create data structures and then create code to support the data structures&lt;br /&gt;
*First step of emulating form-demo was to create the framework for the routing functions (‘/’ , ‘/add’, “/list’).&lt;br /&gt;
**Must follow the definition of &amp;lt;code&amp;gt;var route_get...&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;var route_post...&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;var_route_list...&amp;lt;/code&amp;gt; functions in order to avoid the generation of “Type Error: the Route is not a function error.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
// routes is a mapping of the url’s we want to come in and the code we want to run when the url’s are run.&lt;br /&gt;
var routes = {&lt;br /&gt;
   ‘/’: route_index,&lt;br /&gt;
   ‘/add’ : route_add,&lt;br /&gt;
   ‘/list’ : route_list&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*The following code was copied from index.js:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
router.get(&#039;/&#039;, function(req, res, next) {&lt;br /&gt;
   res.render(&#039;index&#039;, { title: &#039;COMP 2406 Simple form demo&#039; });&lt;br /&gt;
});&lt;br /&gt;
 &lt;br /&gt;
router.post(&#039;/add&#039;, function(req, res) {&lt;br /&gt;
   var obj = { name: req.body.name,&lt;br /&gt;
	       city: req.body.city,&lt;br /&gt;
               country: req.body.country,&lt;br /&gt;
               birthday: req.body.birthday,&lt;br /&gt;
               email: req.body.email };&lt;br /&gt;
   state.push(obj);&lt;br /&gt;
   res.render(&#039;add&#039;, { title: &#039;Person just added&#039;, item: obj });&lt;br /&gt;
});&lt;br /&gt;
 &lt;br /&gt;
router.get(&#039;/list&#039;, function(req, res) {&lt;br /&gt;
   res.render(&#039;list&#039;, { title: &#039;People Listing&#039;,  items: state});&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*The above code was pasted into tinyweb-forms.js after &amp;lt;code&amp;gt;var options;&amp;lt;/code&amp;gt; and before the definition of the &amp;lt;code&amp;gt;routes&amp;lt;/code&amp;gt; object&lt;br /&gt;
*The code was adapted as shown below&lt;br /&gt;
**Note the &amp;lt;code&amp;gt;next&amp;lt;/code&amp;gt; argument was removed from functions since this argument is not needed in our version&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
//The purpose of these functions is to render a template using jade and respond with the rendered HTML&lt;br /&gt;
//We will be making modifications to achieve this&lt;br /&gt;
&lt;br /&gt;
//This one has been partially changed and now provides a response with a static web page&lt;br /&gt;
var route_index  = function(req, res) {&lt;br /&gt;
   //res.render(&#039;index&#039;, { title: &#039;COMP 2406 Simple form demo&#039; });&lt;br /&gt;
   respond(req, res, 200, “&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;&amp;lt;h1&amp;gt;Success!&amp;lt;/h1&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;”,”text/html”); //tiny web page &lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
var route_add = (function(req, res) {&lt;br /&gt;
   var obj = { name: req.body.name,&lt;br /&gt;
	       city: req.body.city,&lt;br /&gt;
               country: req.body.country,&lt;br /&gt;
               birthday: req.body.birthday,&lt;br /&gt;
               email: req.body.email };&lt;br /&gt;
   state.push(obj);&lt;br /&gt;
   res.render(&#039;add&#039;, { title: &#039;Person just added&#039;, item: obj });&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
var route_list = (function(req, res) {&lt;br /&gt;
   res.render(&#039;list&#039;, { title: &#039;People Listing&#039;,  items: state});&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*To get the above functions called, the code to tinyweb_forms.js was modified in the &amp;lt;code&amp;gt;request_handler&amp;lt;/code&amp;gt; function just after the following if statement:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
if(options.aliases.hasOwnProperty(url)){&lt;br /&gt;
   myConsole.log(“Aliasing “ + url + “ to “ + options.aliases[url]);&lt;br /&gt;
   url = options.aliases[url];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*The code that was added is:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
if (routes.hasOwnProperty(url)) {&lt;br /&gt;
   theRoute = routes[url]; // look it up and send the info the return and response&lt;br /&gt;
   return theRoute(request, response); // arguments used from var request_handler&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*We do not use the serve file function since we are generating content dynamically&lt;br /&gt;
*To make sure the code ran correctly, the tinyweb-options.json, error.html and tinyweb-forms.js files were moved to the same folder&lt;br /&gt;
*Furthermore, the tinyweb-options.json file was modified from&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
   “host”: “localhost”,&lt;br /&gt;
   “port”: 8000,&lt;br /&gt;
   ”index”: ”index.html”,&lt;br /&gt;
   ”docroot” : “.”,&lt;br /&gt;
   “logged-headers”: [”user-agent”, “referrer”],&lt;br /&gt;
   ”errorpage”: ”error.html”,&lt;br /&gt;
   ”aliases”: {&lt;br /&gt;
 	”/index.html”: “/hello.html”,&lt;br /&gt;
 	“/”: “/hello.html”,&lt;br /&gt;
	“/really/cool.html”: “/notcool.html”&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*To:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
   “host”: “localhost”,&lt;br /&gt;
   “port”: 8000,&lt;br /&gt;
   ”index”: ”index.html”,&lt;br /&gt;
   ”docroot” : “.”,&lt;br /&gt;
   “logged-headers”: [”user-agent”, “referrer”],&lt;br /&gt;
   ”errorpage”: ”error.html”,&lt;br /&gt;
   ”aliases”: {&lt;br /&gt;
 	”/index.html”: “/”,&lt;br /&gt;
 	“/hello.html”: “/”,&lt;br /&gt;
	“/really/cool.html”: “/notcool.html”&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*In order to render the Jade template, the following line was added at the top of the script:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;var jade = require(‘jade’);&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*In order to load Jade in this way, you must run the following command in the folder that contains the tinyweb-forms files:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;npm install jade&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*A node_modules sub folder will be created &lt;br /&gt;
*The jade files from form-demo/views(templates) can now be moved over to the tinyweb-forms folder (main folder)&lt;br /&gt;
*The &amp;lt;code&amp;gt;route_index&amp;lt;/code&amp;gt; function is then modified as follows to in order to use the Jade template&lt;br /&gt;
**Note that an async function is used to ensure this is done in a non-blocking fashion... we want the webserver to remain responsive for other incoming requests&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var route_index  = function(req, res) {&lt;br /&gt;
  &lt;br /&gt;
   var content, template, options;&lt;br /&gt;
&lt;br /&gt;
   var render_template = function(err, template){&lt;br /&gt;
      content = jade.render(template, options);&lt;br /&gt;
      respond(req, res, 200, content, ”text/html”); &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   options = {pretty: true,&lt;br /&gt;
              title: &#039;COMP 2406 Tinyweb Forms Demo&#039;};&lt;br /&gt;
&lt;br /&gt;
   return fs.readFile(“index.jade”, ”utf-8”, render_template);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*When the code above was run errors that were generated were:&lt;br /&gt;
**Error #1:&lt;br /&gt;
***Error: Jade:1&lt;br /&gt;
***&amp;gt;1 extends layout….the “filename” option is required to use ”extends” with ””relative” paths... because it doesn’t know what file to load&lt;br /&gt;
**Solution #1: add a reference to index.jade&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var route_index = function(req, res) {&lt;br /&gt;
  &lt;br /&gt;
   var content, template, options;&lt;br /&gt;
&lt;br /&gt;
   var render_template = function(err, template){&lt;br /&gt;
      content = jade.render(template, jade_options);&lt;br /&gt;
      respond(req, res, 200, content,”text/html”); &lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   jade_options = {pretty: true,&lt;br /&gt;
                   title: &#039;COMP 2406 Tinyweb Forms Demo&#039;,&lt;br /&gt;
                   filename: ‘index.jade’};&lt;br /&gt;
&lt;br /&gt;
   return fs.readFile(jade_options.filename, ”utf-8”, render_template);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// The calls to fs.readFile(), jade.render() and respond() are all needed to get the functionality of the res.render() which was contained exam-forms&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
**Error #2:&lt;br /&gt;
***A 404 error was generated for style.css&lt;br /&gt;
**Solution #2:&lt;br /&gt;
***Copy the stylesheet over to the form-demo folder&lt;br /&gt;
***Once this was done, prettier fonts were generated and the 404 error was no longer generated&lt;br /&gt;
**Error and Solution #3: Unexpected identifier&lt;br /&gt;
***Generated because there was a comma missing at the end of title: &amp;lt;code&amp;gt;&#039;COMP 2406 Tinyweb Forms Demo&#039;,&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;jade_options&amp;lt;/code&amp;gt; object&lt;br /&gt;
*Next, we moved on to modifying code to redirect the /add route to /list&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var route_add = (function(req, res) {&lt;br /&gt;
   // var obj = { name: req.body.name,&lt;br /&gt;
   //		  city: req.body.city,&lt;br /&gt;
   //             country: req.body.country,&lt;br /&gt;
   //             birthday: req.body.birthday,&lt;br /&gt;
   //             email: req.body.email };&lt;br /&gt;
   //state.push(obj);&lt;br /&gt;
   //res.render(&#039;add&#039;, { title: &#039;Person just added&#039;, item: obj });&lt;br /&gt;
&lt;br /&gt;
   myConsole.log(“302” + “\t” + req.method + “\t”  + req.url);&lt;br /&gt;
&lt;br /&gt;
   res.writeHead(302, {&lt;br /&gt;
       “Content-Type”: “text/html”&lt;br /&gt;
       “Location”: “/list”&lt;br /&gt;
   });&lt;br /&gt;
   return res.end();&lt;br /&gt;
 &lt;br /&gt;
   respond(req, res, 200, content, “text/html”);&lt;br /&gt;
}&lt;br /&gt;
 &lt;br /&gt;
var route_list = (function(req, res) {&lt;br /&gt;
   res.render(&#039;list&#039;, { title: &#039;People Listing&#039;,  items: state});&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*By setting the appropriate HTTP response code and response headers, we indicate to the client&#039;s browser that it should make another request in order to achieve the redirection&lt;br /&gt;
*In examforms.js, &amp;lt;code&amp;gt;res.redirect(‘/list’);&amp;lt;/code&amp;gt; handles this same functionality for redirection&lt;br /&gt;
&lt;br /&gt;
====To Summarize====&lt;br /&gt;
&lt;br /&gt;
*The changes that we made in this exercise include:&lt;br /&gt;
**Giving tinyweb the ability to run code to dynamically generate response for incoming requests&lt;br /&gt;
**Bringing in the functionality to render templates and respond with the rendered HTML&lt;br /&gt;
**Implementing redirection in response to some requests&lt;br /&gt;
*The code in this exercise does not:&lt;br /&gt;
**Save the state of the data (the data does not persist after the server is shut down)&lt;br /&gt;
**Store the state object (the people added in the form)&lt;br /&gt;
**Render the contents of the list&lt;br /&gt;
&lt;br /&gt;
===Additional Notes===&lt;br /&gt;
*Don’t use globals for data unless you are thinking explicitly that the data will be shared&lt;br /&gt;
**e.g the &amp;lt;code&amp;gt;state&amp;lt;/code&amp;gt; array used in this code may not be well-suited as a global variable&lt;br /&gt;
*Web servers have databases so that they can persistently store their data (don’t want the data to go away) and concurrently access the data&lt;br /&gt;
**This is a better alternative for storing the state data&lt;br /&gt;
*The code modifications made in this exercise to bridge the gap between tinyweb.js and examforms.js were very specific&lt;br /&gt;
**examforms.js is far more robust in terms of how it’s coded&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_8&amp;diff=20689</id>
		<title>WebFund 2016W Lecture 8</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_8&amp;diff=20689"/>
		<updated>2016-02-05T15:11:11Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: /* Using Jade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on February 2, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec08-02Feb2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===Agenda===&lt;br /&gt;
&lt;br /&gt;
* Spoke/explained the solutions for [[WebFund 2016W: Assignment 2|assignment 2]].&lt;br /&gt;
* Answered questions about the upcoming [[WebFund 2016W: Assignment 3|assignment 3]].&lt;br /&gt;
&lt;br /&gt;
===Topics (from assignment 2 &amp;amp; 3)===&lt;br /&gt;
&lt;br /&gt;
* What does the regular expression on line 92 of tinywebserver.js do? (Answer: Prevents breaking out of the current directory by using &amp;lt;tt&amp;gt;..&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;~&amp;lt;/tt&amp;gt;)&lt;br /&gt;
* What does path.normalize do? (Answer: Tries to fix a malformed path. e.g. &amp;lt;tt&amp;gt;/home//dave/&amp;lt;/tt&amp;gt; becomes &amp;lt;tt&amp;gt;/home/dave/&amp;lt;/tt&amp;gt;)&lt;br /&gt;
* What is Jade? (Answer: Templating language.)&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===General===&lt;br /&gt;
*Code from Tutorial 4 will be used on the midterm &lt;br /&gt;
*Know and understand what the code does and master it&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
*One way to deal with potential errors is to surround code with a try/catch block&lt;br /&gt;
*To further track down the problem, use the Node debugger&lt;br /&gt;
*In class, there was a problem when reading/parsing the JSON option file&lt;br /&gt;
**Using the debugger, we saw that everything was fine parsing it&lt;br /&gt;
**The issue was that the errorpage did not exist from the working directory&lt;br /&gt;
**Make sure to have all the required files in the correct directory&lt;br /&gt;
**also, make sure you are working on the correct version of your code (in class, we were modifying the wrong code while debugging)&lt;br /&gt;
&lt;br /&gt;
===Jade===&lt;br /&gt;
&lt;br /&gt;
====General====&lt;br /&gt;
*Jade is a templating language for HTML&lt;br /&gt;
*HTML and Jade have a one-to-one correspondence (for the most part)&lt;br /&gt;
*See: [http://jade-lang.com jade-lang.com] for documentation&lt;br /&gt;
*Jade is parsed on the server side, NOT the client side&lt;br /&gt;
*In our example (form-demo), form data comes from the body (object) of the post request (object)&lt;br /&gt;
**The bodyparser is used to parse form data from the body of the request that has been received. The parsed data is then passed into a Jade template to be rendered along with the other Jade code into HTML&lt;br /&gt;
&lt;br /&gt;
====The extends keyword====&lt;br /&gt;
*How does the &amp;lt;code&amp;gt;extends&amp;lt;/code&amp;gt; keyword work in Jade?&lt;br /&gt;
**We write: &amp;lt;code&amp;gt;extends JADE_FILE&amp;lt;/code&amp;gt;&lt;br /&gt;
**This allows a template to extend a parent template. It can then override fill in pre-defined blocks of content&lt;br /&gt;
**Jade supports template inheritance via the &amp;lt;code&amp;gt;block&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;extends&amp;lt;/code&amp;gt; keywords. A block acts as a placeholder in the parent template which can be replaced in the child templates&lt;br /&gt;
**Allows for reuse of Jade templates&lt;br /&gt;
&lt;br /&gt;
====Using Jade====&lt;br /&gt;
*To render template with the Jade engine, we must tell the app to use the engine&lt;br /&gt;
**Specify that the app should use Jade: &amp;lt;code&amp;gt;app.set(&#039;view engine&#039;, &#039;jade&#039;);&amp;lt;/code&amp;gt;&lt;br /&gt;
**Specify where to find the Jade templates: &amp;lt;code&amp;gt;app.set(&#039;views&#039;, __dirname);&amp;lt;/code&amp;gt;&lt;br /&gt;
*To render a Jade code, you can use &amp;lt;code&amp;gt;jade.render(&#039;Jade code...&#039;, {/*Options go here*/})&amp;lt;/code&amp;gt;&lt;br /&gt;
**These options can be found on the documentation site: [http://jade-lang.com/api/ http://jade-lang.com/api/]&lt;br /&gt;
*To render a template, you can use &amp;lt;code&amp;gt;jade.renderFile(&#039;Template&#039;, {/*Options go here*/})&amp;lt;/code&amp;gt;&lt;br /&gt;
*To respond to a request with a rendered page, you can use &amp;lt;code&amp;gt;res.render(&#039;Template&#039;, {/*Data to pass to the template goes here*/});&amp;lt;/code&amp;gt;&lt;br /&gt;
**Note, you don&#039;t specify a .jade extension&lt;br /&gt;
*The &amp;lt;code&amp;gt;render()&amp;lt;/code&amp;gt; method will convert the jade code or template to HTML&lt;br /&gt;
&lt;br /&gt;
===module.exports and require()===&lt;br /&gt;
*You can set what will be exported form a file using &amp;lt;code&amp;gt;module.exports = SOME_OBJECT;&amp;lt;/code&amp;gt;&lt;br /&gt;
*&amp;lt;code&amp;gt;module.exports&amp;lt;/code&amp;gt; is the data &amp;quot;returned&amp;quot; by the file so that it can be used in other .js files&lt;br /&gt;
*This is loaded using &amp;lt;code&amp;gt;require(FILE)&amp;lt;/code&amp;gt;&lt;br /&gt;
**Note that this can only be used to load the same file once&lt;br /&gt;
&lt;br /&gt;
===Code Modifications===&lt;br /&gt;
*Removing the line: &amp;lt;code&amp;gt;app.use(bodyParser.urlencoded({extended:false}));&amp;lt;/code&amp;gt;&lt;br /&gt;
**The list page has no user people listed&lt;br /&gt;
**The form data wasn&#039;t parsed, so the page didn&#039;t have values to display&lt;br /&gt;
*removing &amp;lt;code&amp;gt;module.exports=router;&amp;lt;/code&amp;gt; from index.js:&lt;br /&gt;
**The following line in app.js will no longer have the expected return value to store in the routes variable: &amp;lt;code&amp;gt;var routes = require(&#039;./routes/index&#039;);&amp;lt;/code&amp;gt;&lt;br /&gt;
**This means that none of the routing will work&lt;br /&gt;
&lt;br /&gt;
===HTML Button Types===&lt;br /&gt;
*submit -&amp;gt; submits form data to server(default option)&lt;br /&gt;
*reset -&amp;gt; resets all controls to initial values&lt;br /&gt;
*button -&amp;gt; no default behaviour (usually used for client-side JavaScript)&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_8&amp;diff=20688</id>
		<title>WebFund 2016W Lecture 8</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_8&amp;diff=20688"/>
		<updated>2016-02-05T15:10:03Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: /* Using Jade */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on February 2, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec08-02Feb2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===Agenda===&lt;br /&gt;
&lt;br /&gt;
* Spoke/explained the solutions for [[WebFund 2016W: Assignment 2|assignment 2]].&lt;br /&gt;
* Answered questions about the upcoming [[WebFund 2016W: Assignment 3|assignment 3]].&lt;br /&gt;
&lt;br /&gt;
===Topics (from assignment 2 &amp;amp; 3)===&lt;br /&gt;
&lt;br /&gt;
* What does the regular expression on line 92 of tinywebserver.js do? (Answer: Prevents breaking out of the current directory by using &amp;lt;tt&amp;gt;..&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;~&amp;lt;/tt&amp;gt;)&lt;br /&gt;
* What does path.normalize do? (Answer: Tries to fix a malformed path. e.g. &amp;lt;tt&amp;gt;/home//dave/&amp;lt;/tt&amp;gt; becomes &amp;lt;tt&amp;gt;/home/dave/&amp;lt;/tt&amp;gt;)&lt;br /&gt;
* What is Jade? (Answer: Templating language.)&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===General===&lt;br /&gt;
*Code from Tutorial 4 will be used on the midterm &lt;br /&gt;
*Know and understand what the code does and master it&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
*One way to deal with potential errors is to surround code with a try/catch block&lt;br /&gt;
*To further track down the problem, use the Node debugger&lt;br /&gt;
*In class, there was a problem when reading/parsing the JSON option file&lt;br /&gt;
**Using the debugger, we saw that everything was fine parsing it&lt;br /&gt;
**The issue was that the errorpage did not exist from the working directory&lt;br /&gt;
**Make sure to have all the required files in the correct directory&lt;br /&gt;
**also, make sure you are working on the correct version of your code (in class, we were modifying the wrong code while debugging)&lt;br /&gt;
&lt;br /&gt;
===Jade===&lt;br /&gt;
&lt;br /&gt;
====General====&lt;br /&gt;
*Jade is a templating language for HTML&lt;br /&gt;
*HTML and Jade have a one-to-one correspondence (for the most part)&lt;br /&gt;
*See: [http://jade-lang.com jade-lang.com] for documentation&lt;br /&gt;
*Jade is parsed on the server side, NOT the client side&lt;br /&gt;
*In our example (form-demo), form data comes from the body (object) of the post request (object)&lt;br /&gt;
**The bodyparser is used to parse form data from the body of the request that has been received. The parsed data is then passed into a Jade template to be rendered along with the other Jade code into HTML&lt;br /&gt;
&lt;br /&gt;
====The extends keyword====&lt;br /&gt;
*How does the &amp;lt;code&amp;gt;extends&amp;lt;/code&amp;gt; keyword work in Jade?&lt;br /&gt;
**We write: &amp;lt;code&amp;gt;extends JADE_FILE&amp;lt;/code&amp;gt;&lt;br /&gt;
**This allows a template to extend a parent template. It can then override fill in pre-defined blocks of content&lt;br /&gt;
**Jade supports template inheritance via the &amp;lt;code&amp;gt;block&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;extends&amp;lt;/code&amp;gt; keywords. A block acts as a placeholder in the parent template which can be replaced in the child templates&lt;br /&gt;
**Allows for reuse of Jade templates&lt;br /&gt;
&lt;br /&gt;
====Using Jade====&lt;br /&gt;
*To render template with the Jade engine, we must tell the app to use the engine&lt;br /&gt;
**Specify that the app should use Jade: &amp;lt;code&amp;gt;app.set(&#039;view engine&#039;, &#039;jade&#039;);&amp;lt;/code&amp;gt;&lt;br /&gt;
**Specify where to find the Jade templates: &amp;lt;code&amp;gt;app.set(&#039;views&#039;, __dirname);&amp;lt;/code&amp;gt;&lt;br /&gt;
*To render a Jade code, you can use &amp;lt;code&amp;gt;jade.render(&#039;Jade code...&#039;, {/*Options go here*/})&amp;lt;/code&amp;gt;&lt;br /&gt;
**These options can be found on the documentation site: [http://jade-lang.com/api/ http://jade-lang.com/api/]&lt;br /&gt;
*To render a template, you can use &amp;lt;code&amp;gt;jade.renderFile(&#039;Template&#039;, {/*Options go here*/})&amp;lt;/code&amp;gt;&lt;br /&gt;
**Note, you don&#039;t specify a .jade extension&lt;br /&gt;
*To respond to a request with a rendered page, you can use &amp;lt;code&amp;gt;res.render(&#039;file&#039;, {/*Data to pass to the template goes here*/});&amp;lt;/code&amp;gt;&lt;br /&gt;
*The &amp;lt;code&amp;gt;render()&amp;lt;/code&amp;gt; method will convert the jade code or template to HTML&lt;br /&gt;
&lt;br /&gt;
===module.exports and require()===&lt;br /&gt;
*You can set what will be exported form a file using &amp;lt;code&amp;gt;module.exports = SOME_OBJECT;&amp;lt;/code&amp;gt;&lt;br /&gt;
*&amp;lt;code&amp;gt;module.exports&amp;lt;/code&amp;gt; is the data &amp;quot;returned&amp;quot; by the file so that it can be used in other .js files&lt;br /&gt;
*This is loaded using &amp;lt;code&amp;gt;require(FILE)&amp;lt;/code&amp;gt;&lt;br /&gt;
**Note that this can only be used to load the same file once&lt;br /&gt;
&lt;br /&gt;
===Code Modifications===&lt;br /&gt;
*Removing the line: &amp;lt;code&amp;gt;app.use(bodyParser.urlencoded({extended:false}));&amp;lt;/code&amp;gt;&lt;br /&gt;
**The list page has no user people listed&lt;br /&gt;
**The form data wasn&#039;t parsed, so the page didn&#039;t have values to display&lt;br /&gt;
*removing &amp;lt;code&amp;gt;module.exports=router;&amp;lt;/code&amp;gt; from index.js:&lt;br /&gt;
**The following line in app.js will no longer have the expected return value to store in the routes variable: &amp;lt;code&amp;gt;var routes = require(&#039;./routes/index&#039;);&amp;lt;/code&amp;gt;&lt;br /&gt;
**This means that none of the routing will work&lt;br /&gt;
&lt;br /&gt;
===HTML Button Types===&lt;br /&gt;
*submit -&amp;gt; submits form data to server(default option)&lt;br /&gt;
*reset -&amp;gt; resets all controls to initial values&lt;br /&gt;
*button -&amp;gt; no default behaviour (usually used for client-side JavaScript)&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_8&amp;diff=20669</id>
		<title>WebFund 2016W Lecture 8</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_8&amp;diff=20669"/>
		<updated>2016-02-04T23:06:41Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on February 2, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec08-02Feb2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===Agenda===&lt;br /&gt;
&lt;br /&gt;
* Spoke/explained the solutions for [[WebFund 2016W: Assignment 2|assignment 2]].&lt;br /&gt;
* Answered questions about the upcoming [[WebFund 2016W: Assignment 3|assignment 3]].&lt;br /&gt;
&lt;br /&gt;
===Topics (from assignment 2 &amp;amp; 3)===&lt;br /&gt;
&lt;br /&gt;
* What does the regular expression on line 92 of tinywebserver.js do? (Answer: Prevents breaking out of the current directory by using &amp;lt;tt&amp;gt;..&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;~&amp;lt;/tt&amp;gt;)&lt;br /&gt;
* What does path.normalize do? (Answer: Tries to fix a malformed path. e.g. &amp;lt;tt&amp;gt;/home//dave/&amp;lt;/tt&amp;gt; becomes &amp;lt;tt&amp;gt;/home/dave/&amp;lt;/tt&amp;gt;)&lt;br /&gt;
* What is Jade? (Answer: Templating language.)&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===General===&lt;br /&gt;
*Code from Tutorial 4 will be used on the midterm &lt;br /&gt;
*Know and understand what the code does and master it&lt;br /&gt;
&lt;br /&gt;
===Debugging===&lt;br /&gt;
*One way to deal with potential errors is to surround code with a try/catch block&lt;br /&gt;
*To further track down the problem, use the Node debugger&lt;br /&gt;
*In class, there was a problem when reading/parsing the JSON option file&lt;br /&gt;
**Using the debugger, we saw that everything was fine parsing it&lt;br /&gt;
**The issue was that the errorpage did not exist from the working directory&lt;br /&gt;
**Make sure to have all the required files in the correct directory&lt;br /&gt;
**also, make sure you are working on the correct version of your code (in class, we were modifying the wrong code while debugging)&lt;br /&gt;
&lt;br /&gt;
===Jade===&lt;br /&gt;
&lt;br /&gt;
====General====&lt;br /&gt;
*Jade is a templating language for HTML&lt;br /&gt;
*HTML and Jade have a one-to-one correspondence (for the most part)&lt;br /&gt;
*See: [http://jade-lang.com jade-lang.com] for documentation&lt;br /&gt;
*Jade is parsed on the server side, NOT the client side&lt;br /&gt;
*In our example (form-demo), form data comes from the body (object) of the post request (object)&lt;br /&gt;
**The bodyparser is used to parse form data from the body of the request that has been received. The parsed data is then passed into a Jade template to be rendered along with the other Jade code into HTML&lt;br /&gt;
&lt;br /&gt;
====The extends keyword====&lt;br /&gt;
*How does the &amp;lt;code&amp;gt;extends&amp;lt;/code&amp;gt; keyword work in Jade?&lt;br /&gt;
**We write: &amp;lt;code&amp;gt;extends JADE_FILE&amp;lt;/code&amp;gt;&lt;br /&gt;
**This allows a template to extend a parent template. It can then override fill in pre-defined blocks of content&lt;br /&gt;
**Jade supports template inheritance via the &amp;lt;code&amp;gt;block&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;extends&amp;lt;/code&amp;gt; keywords. A block acts as a placeholder in the parent template which can be replaced in the child templates&lt;br /&gt;
**Allows for reuse of Jade templates&lt;br /&gt;
&lt;br /&gt;
====Using Jade====&lt;br /&gt;
*To render template with the Jade engine, we must tell the app to use the engine&lt;br /&gt;
**Specify that the app should use Jade: &amp;lt;code&amp;gt;app.set(&#039;view engine&#039;, &#039;jade&#039;);&amp;lt;/code&amp;gt;&lt;br /&gt;
**Specify where to find the Jade templates: &amp;lt;code&amp;gt;app.set(&#039;views&#039;, __dirname);&amp;lt;/code&amp;gt;&lt;br /&gt;
*To render a template, you can use &amp;lt;code&amp;gt;jade.render(&#039;file&#039;, {/*Options go here*/})&amp;lt;/code&amp;gt;&lt;br /&gt;
**These options can be found on the documentation site: [http://jade-lang.com/api/ http://jade-lang.com/api/]&lt;br /&gt;
**Note, you don&#039;t specify a .jade extension&lt;br /&gt;
**To respond to a request with a rendered page, you can use &amp;lt;code&amp;gt;res.render(&#039;file&#039;, {/*Data to pass to the template goes here*/});&amp;lt;/code&amp;gt;&lt;br /&gt;
*The &amp;lt;code&amp;gt;render()&amp;lt;/code&amp;gt; method will convert the jade template to HTML&lt;br /&gt;
&lt;br /&gt;
===module.exports and require()===&lt;br /&gt;
*You can set what will be exported form a file using &amp;lt;code&amp;gt;module.exports = SOME_OBJECT;&amp;lt;/code&amp;gt;&lt;br /&gt;
*&amp;lt;code&amp;gt;module.exports&amp;lt;/code&amp;gt; is the data &amp;quot;returned&amp;quot; by the file so that it can be used in other .js files&lt;br /&gt;
*This is loaded using &amp;lt;code&amp;gt;require(FILE)&amp;lt;/code&amp;gt;&lt;br /&gt;
**Note that this can only be used to load the same file once&lt;br /&gt;
&lt;br /&gt;
===Code Modifications===&lt;br /&gt;
*Removing the line: &amp;lt;code&amp;gt;app.use(bodyParser.urlencoded({extended:false}));&amp;lt;/code&amp;gt;&lt;br /&gt;
**The list page has no user people listed&lt;br /&gt;
**The form data wasn&#039;t parsed, so the page didn&#039;t have values to display&lt;br /&gt;
*removing &amp;lt;code&amp;gt;module.exports=router;&amp;lt;/code&amp;gt; from index.js:&lt;br /&gt;
**The following line in app.js will no longer have the expected return value to store in the routes variable: &amp;lt;code&amp;gt;var routes = require(&#039;./routes/index&#039;);&amp;lt;/code&amp;gt;&lt;br /&gt;
**This means that none of the routing will work&lt;br /&gt;
&lt;br /&gt;
===HTML Button Types===&lt;br /&gt;
*submit -&amp;gt; submits form data to server(default option)&lt;br /&gt;
*reset -&amp;gt; resets all controls to initial values&lt;br /&gt;
*button -&amp;gt; no default behaviour (usually used for client-side JavaScript)&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_7&amp;diff=20630</id>
		<title>WebFund 2016W Lecture 7</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_7&amp;diff=20630"/>
		<updated>2016-01-31T17:25:08Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on January 28, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec07-28Jan2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In-class Poll===&lt;br /&gt;
&lt;br /&gt;
https://pollev.com/anilsomayaji565&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;javascript&amp;quot; line&amp;gt;&lt;br /&gt;
var http = require(&#039;http&#039;);&lt;br /&gt;
&lt;br /&gt;
var request_handler = function(request, response) {&lt;br /&gt;
&lt;br /&gt;
    var body_content;&lt;br /&gt;
&lt;br /&gt;
    console.log(&amp;quot;I got a request for: &amp;quot; + request.url);&lt;br /&gt;
&lt;br /&gt;
    response.writeHead(404, {&lt;br /&gt;
        &amp;quot;Content-Type&amp;quot;: &amp;quot;text/html&amp;quot;&lt;br /&gt;
    });&lt;br /&gt;
&lt;br /&gt;
    body_content = &amp;quot;You requested: &amp;quot; + request.url;&lt;br /&gt;
    &lt;br /&gt;
    response.write(&amp;quot;&amp;lt;html&amp;gt;\n&amp;lt;head&amp;gt;\n&amp;lt;title&amp;gt;Hello!&amp;lt;/title&amp;gt;\n&amp;lt;/head&amp;gt;&amp;quot; +&lt;br /&gt;
		   &amp;quot;&amp;lt;body&amp;gt;&amp;lt;h1&amp;gt;Hello world!&amp;lt;/h1&amp;gt;&amp;quot; +&lt;br /&gt;
		   &amp;quot;&amp;lt;p&amp;gt;&amp;quot; + body_content + &amp;quot;&amp;lt;/p&amp;gt;&amp;quot; +&lt;br /&gt;
		   &amp;quot;&amp;lt;script&amp;gt;alert(&#039;where does this print&#039;);&amp;lt;/script&amp;gt; &amp;quot; +&lt;br /&gt;
		   &amp;quot;\n&amp;lt;/body&amp;gt;\n&amp;lt;/html&amp;gt;\n&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    return response.end();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
var server = http.createServer(request_handler);&lt;br /&gt;
&lt;br /&gt;
server.listen(8000, &amp;quot;localhost&amp;quot;, function() {&lt;br /&gt;
    return console.log(&amp;quot;Server listening at http://localhost:8000/&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
console.log(&amp;quot;Finished!&amp;quot;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Student Notes==&lt;br /&gt;
&lt;br /&gt;
===Even Smaller Web Server===&lt;br /&gt;
*Can we go smaller than tiny?&lt;br /&gt;
*Bare bones: &lt;br /&gt;
**http module&lt;br /&gt;
**server&lt;br /&gt;
***Made using &amp;lt;code&amp;gt;http.createServer()&amp;lt;/code&amp;gt;&lt;br /&gt;
***We include the port number and host in the &amp;lt;code&amp;gt;server.listen()&amp;lt;/code&amp;gt; to actually start up the server&lt;br /&gt;
**request handler&lt;br /&gt;
***A function provided as an argument to the &amp;lt;code&amp;gt;createServer()&amp;lt;/code&amp;gt; method which is call for every request that comes in&lt;br /&gt;
*With these pieces, the server can accept requests but does not yet send any responses&lt;br /&gt;
*We need responses so we can hard code something and put it in request handler&lt;br /&gt;
**We can make the server respond with a webpage (html file), using &amp;lt;code&amp;gt;response.writeHead()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;response.write&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;response.end()&amp;lt;/code&amp;gt;&lt;br /&gt;
*At this point we have a very basic web server that works&lt;br /&gt;
*We can also display what the request was, using &amp;lt;code&amp;gt;request.url&amp;lt;/code&amp;gt;&lt;br /&gt;
*All tiny web server has over this is error handling, dealing with files and providing different responses based on the requests&lt;br /&gt;
**This additional functionality can be easily added&lt;br /&gt;
**Providing different responses based on the requests, for example, can be achieved by using the &amp;lt;code&amp;gt;request.url&amp;lt;/code&amp;gt; to decide how to respond&lt;br /&gt;
&lt;br /&gt;
===Client Side JavaScript===&lt;br /&gt;
(We will be playing with this much more after the midterm)&lt;br /&gt;
*We can include client-side JavaScript in a response by using the &amp;lt;code&amp;gt;&amp;amp;lt;script&amp;amp;gt;&amp;lt;/code&amp;gt; tag within the HTML code that we send to the client&lt;br /&gt;
*This will allow for JavaScript code to be embedded in the webpage and run in the client&#039;s browser&lt;br /&gt;
*Example:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&amp;lt;script&amp;gt;alert(‘where does this print?’);&amp;lt;/script&amp;gt;&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Polls===&lt;br /&gt;
*When are A and B added?&lt;br /&gt;
&amp;lt;code&amp;gt;y = f(A+B);&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A and B are added before &amp;lt;code&amp;gt;f()&amp;lt;/code&amp;gt; is called. It needs the value of &amp;lt;code&amp;gt;A+B&amp;lt;/code&amp;gt; as an argument to the function.&lt;br /&gt;
&lt;br /&gt;
*In what order do these execute?&lt;br /&gt;
&amp;lt;code&amp;gt;writeFile(“/tmp/foo.txt”, myData, console.log(“Finished!”));&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Finished is printed then &amp;lt;code&amp;gt;myData&amp;lt;/code&amp;gt; is written to &amp;lt;code&amp;gt;/tmp/foo.txt&amp;lt;/code&amp;gt;. No callback function is given (&amp;lt;code&amp;gt;console.log()&amp;lt;/code&amp;gt; returns &amp;lt;code&amp;gt;undefined&amp;lt;/code&amp;gt;). It evaluates the value of all the arguments before calling the function.&lt;br /&gt;
&lt;br /&gt;
===Express===&lt;br /&gt;
*We use an &amp;lt;code&amp;gt;app&amp;lt;/code&amp;gt; object in Express to represent the server as an application&lt;br /&gt;
*Don’t actually call &amp;lt;code&amp;gt;createServer()&amp;lt;/code&amp;gt; or explicitly write &amp;lt;code&amp;gt;server.listen()&amp;lt;/code&amp;gt;&lt;br /&gt;
*No request handler. Instead have &amp;lt;code&amp;gt;app.get()&amp;lt;/code&amp;gt; (essentially the same thing though)&lt;br /&gt;
*How do we use this like tiny web server to find files in directories and then just serve them?&lt;br /&gt;
**&amp;lt;code&amp;gt;app.use(express.static(‘.’));&amp;lt;/code&amp;gt;&lt;br /&gt;
***This makes a static web server that just serves files from whatever directory you want&lt;br /&gt;
**Besides setup, tiny web server is essentially this one line in Express&lt;br /&gt;
*Express does some basic error handling for us&lt;br /&gt;
&lt;br /&gt;
===Node Debugger===&lt;br /&gt;
*When running the Node debugger, it breaks on first line of program by default&lt;br /&gt;
*You can write &amp;lt;code&amp;gt;debugger;&amp;lt;/code&amp;gt; in your code to create additional break points&lt;br /&gt;
**This is useful to see if your code is actually running at particular points&lt;br /&gt;
&lt;br /&gt;
===Code Examples===&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;fs.readFile(“classtest”, “utf-8”, callback);&lt;br /&gt;
console.log(“Finished!”);&lt;br /&gt;
5;&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
This prints:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;Finished!&lt;br /&gt;
5&lt;br /&gt;
Got data: I have to put something here (callback function)&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
It prints in this order because the callback function passed as an argument is not executed immediately (no parentheses means it is not being called). The application therefore continues to do other work while it waits for the callback to be called.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;var f = function(x) { console.log(“Inside f, x = ” + x); return x + 30; };&lt;br /&gt;
console.log(“Running f: “ + f(3));&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
This prints:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;Inside f, x = 3&lt;br /&gt;
Running f: 30&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
The argument inside the &amp;lt;code&amp;gt;console.log()&amp;lt;/code&amp;gt; function gets evaluated first, resulting in &amp;lt;code&amp;gt;“Inside f, x = 3”&amp;lt;/code&amp;gt; being printed before &amp;lt;code&amp;gt;“Running f: 30”&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_7&amp;diff=20624</id>
		<title>WebFund 2016W Lecture 7</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_7&amp;diff=20624"/>
		<updated>2016-01-29T01:55:57Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on January 28, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec07-28Jan2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In-class Poll===&lt;br /&gt;
&lt;br /&gt;
https://pollev.com/anilsomayaji565&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_5&amp;diff=20585</id>
		<title>WebFund 2016W Lecture 5</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_5&amp;diff=20585"/>
		<updated>2016-01-24T19:51:22Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on January 21, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec05-21Jan2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===formtest.html===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;html4strict&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;head&amp;gt;&lt;br /&gt;
   &amp;lt;title&amp;gt;A simple web page&amp;lt;/title&amp;gt;&lt;br /&gt;
  &amp;lt;/head&amp;gt;&lt;br /&gt;
  &amp;lt;body&amp;gt;&lt;br /&gt;
    &amp;lt;h1&amp;gt;A SIMPLE WEB PAGE (really)&amp;lt;/h1&amp;gt;&lt;br /&gt;
    &amp;lt;p&amp;gt;It isn&#039;t so bad.&amp;lt;/p&amp;gt;&lt;br /&gt;
    &amp;lt;form action=&amp;quot;useName&amp;quot; method=&amp;quot;POST&amp;quot;&amp;gt;&lt;br /&gt;
      What is your name?&lt;br /&gt;
      &amp;lt;input name=&amp;quot;theName&amp;quot; type=&amp;quot;text&amp;quot;&amp;gt;&amp;lt;/input&amp;gt;&lt;br /&gt;
      &amp;lt;input type=&amp;quot;submit&amp;quot;value=&amp;quot;Done!&amp;quot; /&amp;gt;&lt;br /&gt;
    &amp;lt;/form&amp;gt;&lt;br /&gt;
  &amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
====Assignment 1====&lt;br /&gt;
NOTE: The solutions for assignment 1 are posted on the wiki&lt;br /&gt;
&lt;br /&gt;
*In the async version &amp;quot;Finished&amp;quot; should be output using a callback function for &amp;lt;code&amp;gt;wrtieFile()&amp;lt;/code&amp;gt; so that it only runs when the work is truly complete&lt;br /&gt;
**Note that writing &amp;lt;code&amp;gt;fs.writeFile(&#039;output.txt&#039;, data, console.log(&#039;Finished&#039;));&amp;lt;/code&amp;gt; will not use the &amp;lt;code&amp;gt;console.log()&amp;lt;/code&amp;gt; as a callback. Instead, this passes the return value of the &amp;lt;code&amp;gt;console.log()&amp;lt;/code&amp;gt;, which is undefined, as the callback.&lt;br /&gt;
*If you want something to happen after a specific function you have to use a callback&lt;br /&gt;
*Values given to the arguments of callback functions come from the function that calls the callback, in other words, the function to which the callback was passed as an argument&lt;br /&gt;
*To find out what the specific arguments for a given callback are you can check the documentation for that function&lt;br /&gt;
*Always cite any sources you use!! &lt;br /&gt;
*Don&#039;t use fancy things that you don&#039;t understand while coding. It is a bad habit and can lead to errors that you won&#039;t know how to solve&lt;br /&gt;
*There might be a midterm question about the &amp;lt;code&amp;gt;filter()&amp;lt;/code&amp;gt; method&lt;br /&gt;
&lt;br /&gt;
====Quick Talk About Functions====&lt;br /&gt;
*Functions can be written in different ways&lt;br /&gt;
&amp;lt;code&amp;gt;function theFunction(a,b){...}&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;function(err, param){...}&amp;lt;/code&amp;gt; --&amp;gt; anonymous function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;var theFunction = function(a,b){...}&amp;lt;/code&amp;gt; --&amp;gt; this syntax is a reminder that functions are just objects&lt;br /&gt;
*Anonymous functions are useful for one time use&lt;br /&gt;
*You can make your own callback functions&lt;br /&gt;
&lt;br /&gt;
====Forms====&lt;br /&gt;
What is a form used for?&lt;br /&gt;
*A form is used when you want to send information to the server&lt;br /&gt;
*The HTML syntax for a from is simply&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;form action=&amp;quot;someAction&amp;quot; method=&amp;quot;GET or POST&amp;quot;&amp;gt;&lt;br /&gt;
     What is your name? &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;theName&amp;quot;/&amp;gt;&lt;br /&gt;
     &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Done!&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*A form has a method attribute and an action attribute&lt;br /&gt;
**The method is used to specify the type of HTTP request that will be sent when the form is submitted (usually either a POST or a GET)&lt;br /&gt;
***POST is used to send data to the server&lt;br /&gt;
***GET is used to ask the server for data &lt;br /&gt;
**The action is similar to a path on the server where the request is sent&lt;br /&gt;
*Tinywebserver.js can&#039;t handle data being sent to it (can&#039;t handle POST)&lt;br /&gt;
*Tinywebserver.js can currently only handle GET requests&lt;br /&gt;
**There is no code in it that can process POST requests&lt;br /&gt;
**Only processes files&lt;br /&gt;
*A form also has nested input tags&lt;br /&gt;
**A self closing tag (&amp;lt;code&amp;gt;&amp;lt;input ... /&amp;gt;&amp;lt;/code&amp;gt;) can be used when there is no content that would appear between the opening and closing tags&lt;br /&gt;
**An input tag (of type text) displays an input field that the user can use to enter data&lt;br /&gt;
**For example, after the question &amp;quot;What is your name?&amp;quot; there is an input tag, which is being used to display an input field&lt;br /&gt;
**The second input tag, &amp;lt;code&amp;gt;&amp;lt;input type=&amp;quot;submit&amp;quot;...&amp;gt;&amp;lt;/code&amp;gt;, is used to display a button that will submit the data to the server&lt;br /&gt;
**The &#039;name&#039; attribute of the input tag identifies the name of the input&lt;br /&gt;
***This becomes part of the URL (when the method is GET) and is paired with the data that was entered in the input field&lt;br /&gt;
***The name of the field gets sent to the server along with the value&lt;br /&gt;
***Ideally we don&#039;t want this to all be in the URL&lt;br /&gt;
****By using a POST request instead, the data is stored elsewhere in the request when it is sent to the server&lt;br /&gt;
****You can see what is sent by using the developer tools on your browser and looking under &#039;Params&#039;&lt;br /&gt;
*We need code in the Web server to handle forms&lt;br /&gt;
*The first way to process forms was by using Perl&lt;br /&gt;
**The problem with Perl is that it&#039;s not efficient&lt;br /&gt;
**Need to run Perl for every incoming request&lt;br /&gt;
**Solution was to run Perl continuously and integrate it into the server&lt;br /&gt;
*Then PHP was used&lt;br /&gt;
**People liked PHP in the beginning because it was integrated with HTML&lt;br /&gt;
***PHP allowed for programmatic parts to be added to HTML documents&lt;br /&gt;
**A lot of the modern Web uses PHP (Facebook, Wordpress)&lt;br /&gt;
***They have separate files that are just PHP, it is not embedded in the webpage.&lt;br /&gt;
*Why is PHP not a good thing?&lt;br /&gt;
**It is messy because there is code mixed in with the document&lt;br /&gt;
*The modern solution is to separate the code and the HTML by leaving place holders in the document&lt;br /&gt;
&lt;br /&gt;
====Jade====&lt;br /&gt;
*Has PHP-like functionality&lt;br /&gt;
*This is a template engine that we will be using in class&lt;br /&gt;
*It uses HTML and its own embedded programming language&lt;br /&gt;
*Instead of nesting Jade uses indentation&lt;br /&gt;
*Instead of start and end tags Jade has only starts tags and without angle brackets&lt;br /&gt;
*The server runs the Jade code&lt;br /&gt;
*The browser only sees HTML&lt;br /&gt;
*Anything that can be done in HTML can be done in Jade&lt;br /&gt;
&lt;br /&gt;
====Express====&lt;br /&gt;
*This is a Web application framework we will be using in class&lt;br /&gt;
*This is useful because we won&#039;t have to work from scratch&lt;br /&gt;
*Node.js is the platform that Express runs on&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_2&amp;diff=20579</id>
		<title>WebFund 2016W Lecture 2</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_2&amp;diff=20579"/>
		<updated>2016-01-22T15:36:47Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on January 12, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec02-12Jan2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In-class Notes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture #2&lt;br /&gt;
----------&lt;br /&gt;
JavaScript&lt;br /&gt;
&lt;br /&gt;
* no declared types&lt;br /&gt;
  - strings&lt;br /&gt;
  - objects&lt;br /&gt;
  - arrays&lt;br /&gt;
  - numbers (floats)&lt;br /&gt;
  - boolean&lt;br /&gt;
&lt;br /&gt;
With Node&lt;br /&gt;
 - asynchronous (non-blocking) I/O&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
* Administration Reminders&lt;br /&gt;
** [[Fundamentals_of_Web_Applications:_Winter_2016_Course_Outline#Course_Information|Office hours have been added.]]&lt;br /&gt;
** [[Fundamentals_of_Web_Applications:_Winter_2016_Course_Outline#Grading|Midterm date has been tentatively created.]]&lt;br /&gt;
** [[WebFund_2016W:_Assignment_1|Assignment #1 has been posted.]]&lt;br /&gt;
&lt;br /&gt;
====JavaScript: As a Language====&lt;br /&gt;
&lt;br /&gt;
* JavaScript has &#039;&#039;&#039;nothing&#039;&#039;&#039; to do with Java.&lt;br /&gt;
** Why is it even called JavaScript then? At first it wasn&#039;t; it was developed as Mocha, changed to LiveScript and then finally renamed to JavaScript due to an agreement with Sun (acquired by Oracle).&amp;lt;ref&amp;gt;[http://stackoverflow.com/a/2475528 Stack Overflow - Why is JavaScript called JavaScript, since it has nothing to do with Java?]&amp;lt;/ref&amp;gt;&lt;br /&gt;
* The only similarity between Java/JavaScript is some of the syntax, but that can be said for a lot of languages.&lt;br /&gt;
&lt;br /&gt;
* What&#039;s different about JavaScript?&lt;br /&gt;
** No declared data types; it&#039;s a loosely typed language.&lt;br /&gt;
*** Instead of int i = 0, it&#039;s simply var i = 0.&lt;br /&gt;
** There&#039;s seven data types&amp;lt;ref&amp;gt;[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures MDN - JavaScript Data Structures]&amp;lt;/ref&amp;gt; (examples in brackets)&lt;br /&gt;
*** Boolean (true, false)&lt;br /&gt;
*** Number (3.1337, 1337, etc. There&#039;s no integer or long, everything is a float)&lt;br /&gt;
*** String (&amp;quot;Dave&amp;quot;)&lt;br /&gt;
*** Symbol&lt;br /&gt;
*** Object&lt;br /&gt;
*** Null&lt;br /&gt;
*** Undefined&lt;br /&gt;
&lt;br /&gt;
====How should I learn JavaScript?====&lt;br /&gt;
&lt;br /&gt;
* Practice! While attending the lectures is a great start, you &#039;&#039;will&#039;&#039; have to practice outside of class. This will not be graded, but will ultimately affect your performance in the course (and in the workplace).&lt;br /&gt;
** [https://www.codecademy.com/learn/javascript Codecademy] has a lot of good resources for beginners.&lt;br /&gt;
** For more advanced students, check on [https://github.com/search?l=javascript&amp;amp;o=desc&amp;amp;q=stars%3A%3E1&amp;amp;s=stars&amp;amp;type=Repositories GitHub] to see if there&#039;s any projects you&#039;re interested in contributing to. If you plan to work in web development, it&#039;s very likely you will be asked in an interview if you&#039;ve contributed to any open source projects (having a good portfolio will help a &#039;&#039;lot&#039;&#039; in getting a first job).&lt;br /&gt;
* The Mozilla Developer Network maintains a [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference JavaScript reference] that is recommend. &#039;&#039;(Note: There is no official documentation for Google Chrome&#039;s V8 engine, although there is an [http://v8.paulfryzel.com/docs/master/index.html unofficial reference].)&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
====Working with Variables====&lt;br /&gt;
&lt;br /&gt;
If you&#039;ve previously worked with Java, the first large difference you&#039;ve noticed is likely that variables are very &amp;quot;flexible&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
* Trying to use things that have not been declared can produce some behaviour that you might not expect:&lt;br /&gt;
** Referencing a variable that has not been declared will produce a reference error&lt;br /&gt;
** Assigning a value to  a variable that has not been declared will implicitly declare the variable at the global level (avoid doing this by explicitly declaring your variables)&lt;br /&gt;
** Accessing a property that does not exist in an object will give you &amp;lt;code&amp;gt;undefined&amp;lt;/code&amp;gt;&lt;br /&gt;
* Similar to how in other languages, a symbol x can refer to an object which can encapsulate other object or types of data, in JavaScript, an object can hold various types of data in its properties (e.g., &amp;lt;tt&amp;gt;x.y&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;x.y.z&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;x.y.x.qwerty&amp;lt;/tt&amp;gt;, etc).&lt;br /&gt;
* &amp;lt;tt&amp;gt;x.size&amp;lt;/tt&amp;gt; (dot notation) is the same as &amp;lt;tt&amp;gt;x[&amp;quot;size&amp;quot;]&amp;lt;/tt&amp;gt; (array notation).&lt;br /&gt;
* Functions are also a special type of object and they can be stored in a variable.&lt;br /&gt;
* JavaScript engines use a garbage collector, so you do not need to manually deallocate memory (in C you need to). Unlike Java, you can &amp;quot;reuse&amp;quot; a variable name and use a completely different type; e.g. creating &amp;lt;tt&amp;gt;var x = 1337&amp;lt;/tt&amp;gt; and then &amp;lt;tt&amp;gt;x = &amp;quot;Dave&amp;quot;&amp;lt;/tt&amp;gt; later is completely valid.&lt;br /&gt;
&lt;br /&gt;
====Regular Expressions (Regex)====&lt;br /&gt;
&lt;br /&gt;
While this topic is technically outside of the course material, you will likely need to use it in some assignments. Regex is the same across different languages, so you will not need to relearn it all for every language.&lt;br /&gt;
&lt;br /&gt;
https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions&lt;br /&gt;
&lt;br /&gt;
====Synchronous vs. Asynchronous====&lt;br /&gt;
&lt;br /&gt;
* Synchronous (blocking) functions are where a task must be fully complete before the next task can be begin.&lt;br /&gt;
** Examples:&lt;br /&gt;
*** Waiting on a single network connection before accepting additions requests.&lt;br /&gt;
*** Waiting for a file to be fully loaded before displaying it in a text editor.&lt;br /&gt;
* Asynchronous (non-blocking) functions are where the program is allowed to continue with other tasks while another one is in process.&lt;br /&gt;
** Examples:&lt;br /&gt;
*** Accepting new network connections while another connection is already in progress.&lt;br /&gt;
&lt;br /&gt;
====Node.js====&lt;br /&gt;
&lt;br /&gt;
Node.js is designed to provide a server side environment of JavaScript. Instead of using Google&#039;s V8 JavaScript inside Chrome/Chromium, Node.js uses the V8 engine directly. Because Node.js is meant to be used outside of the browser, there&#039;s some slight differences.&lt;br /&gt;
&lt;br /&gt;
* Contains a package manager called npm to manage dependencies (libraries).&lt;br /&gt;
* Has specialized libraries for networking, file system access, etc that you usually wouldn&#039;t find in a browser (mostly for security reasons). These functions have their own set of [https://nodejs.org/api/ documentation].&lt;br /&gt;
* Has no default graphic frontend such as WebKit (although there are libraries to provide that if you wish).&lt;br /&gt;
&lt;br /&gt;
=====Why use Node.js?=====&lt;br /&gt;
&lt;br /&gt;
* JavaScript makes it easier to write asynchronous functions.&lt;br /&gt;
* You can reuse your knowledge on client-side JavaScript to apply to server-side Node.js applications.&lt;br /&gt;
&lt;br /&gt;
=====Getting started with Node.js=====&lt;br /&gt;
&lt;br /&gt;
To open up Read-Eval-Print-Loop (REPL), run &amp;lt;tt&amp;gt;node&amp;lt;/tt&amp;gt; in a terminal. For more details on setting your environment up, see [[WebFund_2016W:_Tutorial_1#Running_the_VM|Tutorial 1]].&lt;br /&gt;
&lt;br /&gt;
====References====&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_4&amp;diff=20559</id>
		<title>WebFund 2016W Lecture 4</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_4&amp;diff=20559"/>
		<updated>2016-01-21T15:36:28Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on January 19, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec04-19Jan2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In-class Notes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 4&lt;br /&gt;
---------&lt;br /&gt;
Web servers&lt;br /&gt;
&lt;br /&gt;
What does a web server do?&lt;br /&gt;
Listens on port 80, 443, or other port&lt;br /&gt;
INPUT: HTTP requests&lt;br /&gt;
OUTPUT: HTTP responses &amp;amp; requested documents&lt;br /&gt;
&lt;br /&gt;
tinywebserver.js&lt;br /&gt;
 - written in JavaScript (Node)&lt;br /&gt;
 - can only serve files&lt;br /&gt;
 - understands only a few file types&lt;br /&gt;
&lt;br /&gt;
So tinywebserver is how web servers worked in 1995&lt;br /&gt;
&lt;br /&gt;
What changed since then is now web servers run code to generate responses&lt;br /&gt;
&lt;br /&gt;
Why use frameworks?&lt;br /&gt;
 - because we don&#039;t want to build up HTML from scratch&lt;br /&gt;
 - and, we want logic that is abstracted away&lt;br /&gt;
 - and, we want routing&lt;br /&gt;
   - have someone else decide which function to call in response to requests&lt;br /&gt;
&lt;br /&gt;
But for this week, bare bones&lt;br /&gt;
&lt;br /&gt;
Debugging&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
====Announcements====&lt;br /&gt;
*Assignment 1 due tomorrow (now pushed back to Thursday before class)&lt;br /&gt;
*Basics for tutorial 2 and assignment 2 are up!&lt;br /&gt;
&lt;br /&gt;
====Web Servers====&lt;br /&gt;
*What does a web server do? &lt;br /&gt;
**Listens on port 80 (usually HTTP), 443 (usually HTTPS) or some other port.&lt;br /&gt;
***If the web server is listening on a non-standard port, you must specify the port in the URL (using something like “localhost:8080”)&lt;br /&gt;
**Input: HTTP requests&lt;br /&gt;
**Output: HTTP responses with requested documents&lt;br /&gt;
***HTTP responses always have a response code&lt;br /&gt;
****200: OK, used for successful request&lt;br /&gt;
****404: Not Found&lt;br /&gt;
**In other words it services requests&lt;br /&gt;
*Need root privileges to listen on ports &amp;lt;~1024&lt;br /&gt;
**Do not do development as root if you can, it is risky!&lt;br /&gt;
*Popular Web Servers: Apache, NginX&lt;br /&gt;
&lt;br /&gt;
====Tiny Web Server====&lt;br /&gt;
*Written in JavaScript(Node)&lt;br /&gt;
*Can only serve files! (most modern servers can do more)&lt;br /&gt;
*Understands limited file types&lt;br /&gt;
*Similar to web servers from 1995 &lt;br /&gt;
*To try to understand what the code is doing, we can start by looking at the bottom of the file&lt;br /&gt;
&lt;br /&gt;
=====http.createServer()=====&lt;br /&gt;
*Apart from the loading of some modules and the definitions of some objects and functions, the first real code to run in this script is the &amp;lt;code&amp;gt;createServer()&amp;lt;/code&amp;gt; method&lt;br /&gt;
*We are calling a method of the &amp;lt;code&amp;gt;http&amp;lt;/code&amp;gt; object (loaded from a module) in order to create an object which will act as the server (which we then store in the &amp;lt;code&amp;gt;server&amp;lt;/code&amp;gt; variable)&lt;br /&gt;
*This method takes a single argument, &amp;lt;code&amp;gt;request_handler&amp;lt;/code&amp;gt;&lt;br /&gt;
**&amp;lt;code&amp;gt;request_handler&amp;lt;/code&amp;gt; is a function which is intended to handler incoming requests&lt;br /&gt;
**By providing this function as the argument, we are telling the server to call the function every time a request comes in&lt;br /&gt;
&lt;br /&gt;
=====server.listen()=====&lt;br /&gt;
*Here we are calling the &amp;lt;code&amp;gt;listen()&amp;lt;/code&amp;gt; method of the &amp;lt;code&amp;gt;server&amp;lt;/code&amp;gt; object that was just created&lt;br /&gt;
*This tells the server to start listening for incoming requests&lt;br /&gt;
*The first two arguments are telling the server where to listen (on port 8080 of localhost)&lt;br /&gt;
**This information came from the &amp;lt;code&amp;gt;options&amp;lt;/code&amp;gt; object which was defined at the top of the file&lt;br /&gt;
*The third argument is a callback function&lt;br /&gt;
**This will run once the server has started listening&lt;br /&gt;
**It simply prints a message in the console to indicate that the server is ready&lt;br /&gt;
&lt;br /&gt;
=====request_handler()=====&lt;br /&gt;
*This function gets called every time a request comes in (because we provided it as an argument to the &amp;lt;code&amp;gt;http.createServer()&amp;lt;/code&amp;gt; method)&lt;br /&gt;
*There are two arguments &lt;br /&gt;
**&amp;lt;code&amp;gt;request&amp;lt;/code&amp;gt; is an object which represents the request that was received&lt;br /&gt;
**&amp;lt;code&amp;gt;response&amp;lt;/code&amp;gt; is an object which represents the response that will be sent&lt;br /&gt;
*At a high level, this function checks for the existence of a requested file and responds with either the file or an error code&lt;br /&gt;
*There are many nested callback functions here&lt;br /&gt;
**Although this makes the code more difficult to read, why is this preferable to doing things synchronously?&lt;br /&gt;
***This ensures that the &amp;lt;code&amp;gt;request_handler()&amp;lt;/code&amp;gt; function will return very quickly&lt;br /&gt;
***All the bulk of the work is done asynchronously&lt;br /&gt;
***This allows the server to be available to handle another request again very quickly&lt;br /&gt;
***This is important in order to keep the server responsive for numerous requests &lt;br /&gt;
&lt;br /&gt;
====MIME Types====&lt;br /&gt;
*MIME types are used to specify the type of data being sent via HTTP&lt;br /&gt;
*In Tiny Web Server, there is an object &amp;lt;code&amp;gt;MIME_TYPES&amp;lt;/code&amp;gt; defined near the top of the file which contains the types of files that the server recognizes&lt;br /&gt;
*The &amp;lt;code&amp;gt;MIME_TYPES&amp;lt;/code&amp;gt; object is used in the &amp;lt;code&amp;gt;get_mime()&amp;lt;/code&amp;gt; function to determine the type of a requested file&lt;br /&gt;
*Similarly, the &amp;lt;code&amp;gt;MIME_TYPES&amp;lt;/code&amp;gt; object is used to set the Content-Type header of the HTTP response that is sent by the server&lt;br /&gt;
*We can break ‘image/jpg’  by changing it to ‘imaage/jpg’&lt;br /&gt;
**The web browser does not know how to handle it because it does not recognize imaage&lt;br /&gt;
*Web browsers can deal with broken content type for some types of files&lt;br /&gt;
**For example, putting a jpg as png in &amp;lt;code&amp;gt;MIME_TYPES&amp;lt;/code&amp;gt;, will not break the content and Firefox is smart enough to render it correctly as jpg  &lt;br /&gt;
&lt;br /&gt;
====Response Headers====&lt;br /&gt;
*Some headers of the HTTP response are set in the &amp;lt;code&amp;gt;respond()&amp;lt;/code&amp;gt; function&lt;br /&gt;
**This is done using the &amp;lt;code&amp;gt;response.writeHead()&amp;lt;/code&amp;gt; method&lt;br /&gt;
**We could add more headers here if we want to&lt;br /&gt;
*Some other headers are set for us by the code in the &amp;lt;code&amp;gt;http&amp;lt;/code&amp;gt; module&lt;br /&gt;
*We can see these headers from within the dev tools of the web browser after a response has been received&lt;br /&gt;
*There are not many headers used here because Tiny Web Server is not very complex&lt;br /&gt;
&lt;br /&gt;
====Frameworks and Modern Servers====&lt;br /&gt;
How have things improved since 1995?&lt;br /&gt;
*We use code to generate responses to requests&lt;br /&gt;
**We often use frameworks to do much of the work for us&lt;br /&gt;
&lt;br /&gt;
Why use frameworks?&lt;br /&gt;
*Because we don’t want to build up HTML from scratch&lt;br /&gt;
*Abstracted away logic&lt;br /&gt;
*We also want routing (dispatcher): have someone else decide which function to call in response to requests &lt;br /&gt;
&lt;br /&gt;
====Debugging====&lt;br /&gt;
*You can use the node debugger by typing &amp;lt;code&amp;gt;node debug script.js&amp;lt;/code&amp;gt; in the terminal&lt;br /&gt;
*This will stop the program on the first line of code that is run&lt;br /&gt;
*You can continue execution of the code by typing &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt;&lt;br /&gt;
*You can learn about other useful commands by typing &amp;lt;code&amp;gt;help&amp;lt;/code&amp;gt;&lt;br /&gt;
*In particular, you can type &amp;lt;code&amp;gt;repl&amp;lt;/code&amp;gt; to enter a read-evaluate-print loop from which you can inspect or modify objects or other data&lt;br /&gt;
*You can also write &amp;lt;code&amp;gt;debugger;&amp;lt;/code&amp;gt; in the script to create a break point where the debugger will pause the execution of the program&lt;br /&gt;
*There is also a debugger available in the web browser&lt;br /&gt;
**This is for client side code and will not work for any server-side code&lt;br /&gt;
&lt;br /&gt;
====HTML====&lt;br /&gt;
*You can use the inspect in the web browser dev tools to see the the tree-structure HTML which makes up a web page&lt;br /&gt;
*&amp;lt;code&amp;gt;&amp;amp;lt;div&amp;amp;gt;&amp;lt;/code&amp;gt; tags are used for sections in HTML&lt;br /&gt;
*&amp;lt;code&amp;gt;&amp;amp;lt;a&amp;amp;gt;&amp;lt;/code&amp;gt; tags are used for links (review on CodeAcademy)&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_3&amp;diff=20537</id>
		<title>WebFund 2016W Lecture 3</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_3&amp;diff=20537"/>
		<updated>2016-01-18T15:24:24Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on January 14, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec03-14Jan2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In-class Notes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 3&lt;br /&gt;
---------&lt;br /&gt;
What is the web?&lt;br /&gt;
&lt;br /&gt;
Web != Internet&lt;br /&gt;
&lt;br /&gt;
So, what is the Internet?!&lt;br /&gt;
&lt;br /&gt;
Network of networks&lt;br /&gt;
&lt;br /&gt;
What is a network?&lt;br /&gt;
&lt;br /&gt;
Networks allow computers to talk to each other&lt;br /&gt;
&lt;br /&gt;
WiFi, Ethernet, LTE - different standards for computer networks&lt;br /&gt;
&lt;br /&gt;
Layers of networking (OSI)&lt;br /&gt;
* Physical layer&lt;br /&gt;
* Data link layer&lt;br /&gt;
* Network layer &amp;lt;---- &lt;br /&gt;
* Application layer (and such)&lt;br /&gt;
&lt;br /&gt;
Internet Protocol (IP)&lt;br /&gt;
 - packet-based protocol (NOT a stream or continuous protocol)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Old POTS&lt;br /&gt;
 - wire from your house to a switching station&lt;br /&gt;
 - wires between switching stations A and B&lt;br /&gt;
 ...&lt;br /&gt;
 ...&lt;br /&gt;
 - wires between switching station Y to Z&lt;br /&gt;
 - wire from switching station to house&lt;br /&gt;
&lt;br /&gt;
Packet switching networks multiplex physical wires over time. (Multiple communication connections over one wire by them taking turns.)&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;turn&amp;quot; is the packet&lt;br /&gt;
&lt;br /&gt;
IP protocol is a &amp;quot;best effort&amp;quot; protocol. NO error correction or retransmission.&lt;br /&gt;
&lt;br /&gt;
TCP means Transmission Control Protocol&lt;br /&gt;
turns packets into a reliable data data stream&lt;br /&gt;
&lt;br /&gt;
Network Firewalls block &amp;quot;unwanted traffic&amp;quot;&lt;br /&gt;
 - really, block all those protocols that aren&#039;t safe on the open Internet&lt;br /&gt;
&lt;br /&gt;
TCP adds a &amp;quot;port&amp;quot; to the IP address&lt;br /&gt;
 - the port identifies which program to talk to&lt;br /&gt;
 - some are ephemeral (temporary)&lt;br /&gt;
 - others are &amp;quot;well known&amp;quot;, meaning protocol on that port is standardized&lt;br /&gt;
&lt;br /&gt;
Port 25 is for SMTP (email)&lt;br /&gt;
80: HTTP&lt;br /&gt;
443: HTTPS&lt;br /&gt;
22: ssh&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Web is transmitted over HTTP&lt;br /&gt;
&lt;br /&gt;
Major HTTP commands&lt;br /&gt;
 * GET: get documents from server&lt;br /&gt;
  - can be CACHED&lt;br /&gt;
 * POST: send form contents to server&lt;br /&gt;
  - are not cached&lt;br /&gt;
&lt;br /&gt;
You can GET almost anything&lt;br /&gt;
 - MS Word .doc&lt;br /&gt;
 - tiff&lt;br /&gt;
 - PDF&lt;br /&gt;
&lt;br /&gt;
But the real web is&lt;br /&gt;
 - JPEG, GIF, PNG for images&lt;br /&gt;
 - CSS for style sheets&lt;br /&gt;
 - HTML for content&lt;br /&gt;
 - JavaScript for code&lt;br /&gt;
&lt;br /&gt;
.swf is flash.  Flash is not a web standard.  AVOID&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Student Notes===&lt;br /&gt;
&lt;br /&gt;
====The Web====&lt;br /&gt;
&lt;br /&gt;
Web != Internet&lt;br /&gt;
&lt;br /&gt;
We have to talk about networking...&lt;br /&gt;
&lt;br /&gt;
*The internet is a network of networks (inter networks)&lt;br /&gt;
*What is a network?&lt;br /&gt;
**Networks allow computers to talk to each other&lt;br /&gt;
*There are many different technologies and standards used to connect computers. They work in different ways. This is partially due to frequency rights and legacy issues. Some well-known ones include:&lt;br /&gt;
**Ethernet&lt;br /&gt;
**WiFi&lt;br /&gt;
**LTE (Long Term Evolution)&lt;br /&gt;
*Layers of networking (OSI model)&lt;br /&gt;
**Physical Layer (e.g. wire, or frequency)&lt;br /&gt;
**Data Link Layer&lt;br /&gt;
**Network Layer&lt;br /&gt;
***Interconnection standard&lt;br /&gt;
****one standard = INTERNET PROTOCOL!!&lt;br /&gt;
****TCP/IP&lt;br /&gt;
****all layers have to transport IP (internet protocol)&lt;br /&gt;
**Transport Layer&lt;br /&gt;
**Session Layer&lt;br /&gt;
**Presentation Layer&lt;br /&gt;
**Application Layer&lt;br /&gt;
&lt;br /&gt;
=====Internet Protocol (IP)=====&lt;br /&gt;
&lt;br /&gt;
*Packet-based protocol 	(how do you interpret bits in and out)&lt;br /&gt;
*NOT a stream or continuous protocol like POTS: plain old telephone system&lt;br /&gt;
*Old POTS:&lt;br /&gt;
**Wire from origin home to switching station&lt;br /&gt;
**Wires between switching stations&lt;br /&gt;
**...&lt;br /&gt;
**Wire from switch station to destination home&lt;br /&gt;
*Problem: there&#039;s a 1:1 ratio of wires to connections. This is inefficient. &lt;br /&gt;
*Goal is to share 1 wire for 10 phone calls!&lt;br /&gt;
*Packet switching networks multiplex physical wires over time (Multiple connections by them taking turns)&lt;br /&gt;
**People have to take turns&lt;br /&gt;
**The &amp;quot;turn&amp;quot; is the packet&lt;br /&gt;
**It&#039;s actually pretty good if not all people are talking at once, then it slows down&lt;br /&gt;
**[https://en.wikipedia.org/wiki/Network_packet https://en.wikipedia.org/wiki/Network_packet]&lt;br /&gt;
&lt;br /&gt;
=====IP Addresses=====&lt;br /&gt;
*Any computer that&#039;s on a network has an IP address&lt;br /&gt;
*We currently use IPv4 addresses but we are transitioning towards IPv6&lt;br /&gt;
*Packets have an IP address source and destination&lt;br /&gt;
*The address for localhost is 127.0.0.1&lt;br /&gt;
**That&#039;s the computer itself&lt;br /&gt;
*Domain name system (DNS) translates between IP addresses and &amp;quot;English&amp;quot; (e.g. www.google.ca)&lt;br /&gt;
&lt;br /&gt;
=====TCIP/IP=====&lt;br /&gt;
*What happens when packets gets lots or corrupt? (electrical interference/microwave/etc)&lt;br /&gt;
*The IP protocol is a &amp;quot;best effort&amp;quot; protocol. NO error correction or re-transmission&lt;br /&gt;
*For this, we use TCP/IP, not just IP&lt;br /&gt;
*TCP means Transmission Control Protocol&lt;br /&gt;
*Turns packets into a RELIABLE data stream&lt;br /&gt;
*TCP is layer that takes care of re-transmitting as necessary. It&#039;ll come through reliably in the same order.&lt;br /&gt;
&lt;br /&gt;
=====Firewalls and Ports=====&lt;br /&gt;
*A Network Firewall blocks &amp;quot;unwanted traffic&amp;quot;&lt;br /&gt;
**Really blocks all those protocols that aren&#039;t safe on the open internet&lt;br /&gt;
*Firewalls blocked everything except &amp;quot;The Web&amp;quot;&lt;br /&gt;
**Not that it&#039;s so great, it&#039;s just the only thing that wasn&#039;t blocked on the open internet.&lt;br /&gt;
&lt;br /&gt;
*TCP adds a port to the IP addresses on both sides&lt;br /&gt;
*The port identifies which program to talk to&lt;br /&gt;
**Some ports are temporary (ephemeral)&lt;br /&gt;
**Others are &amp;quot;well known&amp;quot;, meaning the protocol on that port is standardized&lt;br /&gt;
***Port 25: SMTP (email)&lt;br /&gt;
***Port 80: HTTP&lt;br /&gt;
***Port 443: HTTPS&lt;br /&gt;
***Port 22: SSH (secure shell)&lt;br /&gt;
*Modern web browsers don&#039;t show the HTTP (protocol) or port number (:80 for HTTP or :443 for HTTPS)&lt;br /&gt;
*Try localhost:631 //on Linux&lt;br /&gt;
*&amp;quot;Standard&amp;quot; ports are below 1024&lt;br /&gt;
*[www.iana.org www.iana.org] //these are the guys that decide the protocols for the ports&lt;br /&gt;
*Port 80 is hypertext transfer protocol, the web is transmitted over HTTP (protocol)&lt;br /&gt;
**HTTP over TCP over IP over datalink layers, over physical layers&lt;br /&gt;
&lt;br /&gt;
=====HTTP=====&lt;br /&gt;
*Browsers (clients) communicate to servers through HTTP requests (typically GET requests)&lt;br /&gt;
**GET requests have request headers which consist of key-value pairs&lt;br /&gt;
**Responses also have headers of key-value pairs&lt;br /&gt;
*Over the history of the web, different companies implemented different standards&lt;br /&gt;
**Like IE6, Web pages would be required to send different info, web pages would break&lt;br /&gt;
*Normally, in Web dev, you&#039;re working a level above HTTP (you use the existing standard)&lt;br /&gt;
*What are the major HTTP requests:&lt;br /&gt;
**GET&lt;br /&gt;
***Client GETs resource from the server&lt;br /&gt;
***Can be CACHED (people in the middle can hold copies)&lt;br /&gt;
**POST&lt;br /&gt;
***POST (send) information (such as form contents) to the web server&lt;br /&gt;
***Are NOT CACHED&lt;br /&gt;
*Doing a reload on a GET is safe &lt;br /&gt;
**The resource is simply requested again&lt;br /&gt;
**This usually results in a cached copy being used&lt;br /&gt;
*Doing a reload on a POST isn&#039;t safe (you typically do not want to do this action twice)&lt;br /&gt;
**They permanently alter the server state&lt;br /&gt;
*You can GET almost anything!&lt;br /&gt;
**.doc&lt;br /&gt;
**.tiff&lt;br /&gt;
**.anything!&lt;br /&gt;
*Typical file types that are requested include:&lt;br /&gt;
**HTML for content&lt;br /&gt;
**CSS (style sheets)&lt;br /&gt;
**JavaScript for code&lt;br /&gt;
**JPEG, GIF, PNG for images&lt;br /&gt;
*.swf is flash&lt;br /&gt;
**We don&#039;t like flash (bad security, performance hog, new browsers don&#039;t have support for plugins)&lt;br /&gt;
**NOT STANDARD, AVOID&lt;br /&gt;
*Plugins inject arbitrary code into browsers, but &amp;quot;real web content&amp;quot; (HTML CSS, JS, Media) is ENOUGH.&lt;br /&gt;
**Modern JS has fully-fledged Socket Support&lt;br /&gt;
&lt;br /&gt;
=====HTML Page Example=====&lt;br /&gt;
In a new .html file:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!DOCTYPE html&amp;gt;	&amp;lt;!-- this isn&#039;t there --&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
	&amp;lt;head&amp;gt; &amp;lt;!-- header section --&amp;gt;&lt;br /&gt;
		&amp;lt;title&amp;gt;A simple web page&amp;lt;/title&amp;gt;&lt;br /&gt;
	&amp;lt;/head&amp;gt;&lt;br /&gt;
&lt;br /&gt;
	&amp;lt;body&amp;gt;&lt;br /&gt;
		&amp;lt;h1&amp;gt;A SIMPLE WEB PAGE&amp;lt;/h1&amp;gt;&lt;br /&gt;
		&amp;lt;p&amp;gt;It isn&#039;t so bad.&amp;lt;/p&amp;gt;&lt;br /&gt;
	&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W:_Assignment_1&amp;diff=20515</id>
		<title>WebFund 2016W: Assignment 1</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W:_Assignment_1&amp;diff=20515"/>
		<updated>2016-01-15T02:54:57Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This assignment is due on January 20th on cuLearn.&lt;br /&gt;
&lt;br /&gt;
In this assignment you will writing two programs, &amp;lt;tt&amp;gt;words-sync.js&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;words-async.js&amp;lt;/tt&amp;gt;.  These programs will make use of synchronous and asynchronous I/O is the manner of the programs from [[WebFund 2016W: Tutorial 1|Tutorial 1]].&lt;br /&gt;
&lt;br /&gt;
These programs should both behave as follows:&lt;br /&gt;
&lt;br /&gt;
* They should take two arguments, an input text file and an output file to create.  Thus you run them by typing &amp;quot;node words-sync.js document.txt wordlist.txt&amp;quot;, with document.txt being the file to be read and wordlist.txt is the file to be created.&lt;br /&gt;
* The output file should contain a list of words in the input file, one word per line, with each line terminated by a newline character (&#039;\n&#039;).  These words should be sorted in ascending order, as ordered by the standard JavaScript [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort Array.sort() method] (with no comparison function specified).  All duplicates should be removed and there should be no blank lines.&lt;br /&gt;
* Once the output file has been created, the program should output &amp;quot;Finished!&amp;quot; to the console.&lt;br /&gt;
* For the purposes of this program, words can be defined as anything matching the \w operator.  In other words, to split the document into words you may call [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split split()] using &amp;quot;/\W//&amp;quot; as the split [https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions regular expression].&lt;br /&gt;
* You may assume that the input file exists and is readable and the output file is writable.  In other words you do not need to do any error checking (although it is appreciated).&lt;br /&gt;
&lt;br /&gt;
In creating this program, don&#039;t forget that you can use standard JavaScript methods such as [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push push()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/pop pop()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/shift shift()], and [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift unshift()].  And of course you&#039;ll need the methods from [https://nodejs.org/api/fs.html the Node fs module].&lt;br /&gt;
&lt;br /&gt;
There are 10 points, allocated as follows:&lt;br /&gt;
* 3 points for producing the correct output given well-formed input.&lt;br /&gt;
* 2 points for correctly structuring the code in the synchronous version.&lt;br /&gt;
* 5 points for correctly structuring the code in the asynchronous version.  In particular, &amp;quot;Finished!&amp;quot; should only be output when the program is truly finished!&lt;br /&gt;
&lt;br /&gt;
Please submit your answers as a zip file called &amp;quot;&amp;lt;username&amp;gt;-comp2406-assign1.zip&amp;quot;, where username is your MyCarletonOne username. This zip file should uncompress to a directory called &amp;quot;&amp;lt;username&amp;gt;-comp2406-assign1&amp;quot; and inside this directory should be three files: &amp;quot;words-sync.js&amp;quot;, &amp;quot;words-async.js&amp;quot;,  and a text file &amp;quot;comments.txt&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;quot;comments.txt&amp;quot; should:&lt;br /&gt;
* list any references you used to complete the assignment (documentation web sites, for example),&lt;br /&gt;
* list your collaborators, and&lt;br /&gt;
* optionally, should discuss any issues or concerns you had when completing this assignment.&lt;br /&gt;
Remember that while you are allowed to collaborate with others, plagiarism is not allowed. In other words you should not be copying any code or data directly from anywhere, and any assistance or inspiration should be credited. Any significant code similarity (beyond the code already given to you) will be considered plagiarism and will be reported to the Dean.&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_1&amp;diff=20449</id>
		<title>WebFund 2016W Lecture 1</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_1&amp;diff=20449"/>
		<updated>2016-01-09T02:59:34Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on January 7, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec01-07Jan2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In-Class Notes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 1&lt;br /&gt;
---------&lt;br /&gt;
&lt;br /&gt;
web servers&lt;br /&gt;
  - apache, nginx&lt;br /&gt;
&lt;br /&gt;
web browsers (clients)&lt;br /&gt;
  - firefox, chrome, IE, Safari&lt;br /&gt;
  &lt;br /&gt;
web protocols&lt;br /&gt;
  - HTTP&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When you view a web page:&lt;br /&gt;
 - browser sends a GET request for the URL to a web server&lt;br /&gt;
 - web server sends back a document&lt;br /&gt;
 - browser renders document and makes more GET requests&lt;br /&gt;
&lt;br /&gt;
Web pages are, at base, HTML documents with other stuff&lt;br /&gt;
&lt;br /&gt;
Major web renderers&lt;br /&gt;
 - Gecko (Firefox)&lt;br /&gt;
 - Blink (Chrome, fork of WebKit)&lt;br /&gt;
 - Trident (MSHTML)&lt;br /&gt;
 - WebKit&lt;br /&gt;
 - EdgeHTML (fork of Trident)&lt;br /&gt;
&lt;br /&gt;
Web renderers interpret:&lt;br /&gt;
 - HTML&lt;br /&gt;
 - CSS&lt;br /&gt;
 - JavaScript&lt;br /&gt;
&lt;br /&gt;
(and they incorporate images and videos)&lt;br /&gt;
&lt;br /&gt;
HTML - specifies basic structure and content of &amp;quot;page&amp;quot;&lt;br /&gt;
CSS - makes it pretty (fonts, layout, etc)&lt;br /&gt;
JavaScript - code&lt;br /&gt;
&lt;br /&gt;
Read-Eval-Print loop (REPL)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Student Notes ===&lt;br /&gt;
&lt;br /&gt;
==== Course Logistics ====&lt;br /&gt;
&lt;br /&gt;
* This course is about &#039;&#039;forming a conceptual model of the web&#039;&#039;&lt;br /&gt;
* Notes and other content from previous years can be found on the wiki&lt;br /&gt;
* Anil &amp;amp;amp; TA&#039;s have office hours&lt;br /&gt;
* Tutorials are required&lt;br /&gt;
** Come see a TA promptly if you miss one (do the work beforehand)&lt;br /&gt;
* Mark Breakdown Highlights&lt;br /&gt;
** Assignments (lowest grade dropped)&lt;br /&gt;
** Tutorials (20%!! Very important)&lt;br /&gt;
* Collaboration&lt;br /&gt;
** Don&#039;t transfer bits to each other&lt;br /&gt;
** Don&#039;t submit the same thing&lt;br /&gt;
** Cite!!&lt;br /&gt;
&lt;br /&gt;
==== Strategies for succeeding ====&lt;br /&gt;
&lt;br /&gt;
* Don&#039;t get discouraged! This course is targeted at a wide demographic so there might be stuff you don&#039;t understand.&lt;br /&gt;
* Try to keep up in your understanding of the general course content. You should not be trying to piece things together from many code snippets from web searches. The core material is all provided for you.&lt;br /&gt;
&lt;br /&gt;
==== Basics of web technology ====&lt;br /&gt;
&lt;br /&gt;
* Linux environment provided, but you don&#039;t have to use it&lt;br /&gt;
* Node&lt;br /&gt;
** Chrome&#039;s JavaScript engine + some extra capabilities such as networking and disk I/O&lt;br /&gt;
** Used for server-side development&lt;br /&gt;
* Important concepts:&lt;br /&gt;
** Web servers - Apache, Nginx&lt;br /&gt;
** Web browsers (clients) - Firefox, Chrome, IE, Safari, Edge&lt;br /&gt;
*** Browsers are more complicated than servers&lt;br /&gt;
*** Rendering Engines&lt;br /&gt;
*** Gecko (Firefox), Blink (Chrome, fork of Webkit), Trident (MSHTML), WebKit, EdgeHTML (Opera used to have it&#039;s own)&lt;br /&gt;
*** Web renderers interpret HTML, CSS, and JavaScript&lt;br /&gt;
**** HTML specifies layout&lt;br /&gt;
**** CSS makes it pretty&lt;br /&gt;
**** JavaScript is the code!&lt;br /&gt;
**** We use JS on the server BECAUSE it&#039;s on the client (other languages could be used instead)&lt;br /&gt;
** Web protocols - HTTP&lt;br /&gt;
* What happens when you load the wiki?&lt;br /&gt;
** Homeostasis server is running Ubuntu, with Apache configured to run MediaWiki with PHP&lt;br /&gt;
** Anil&#039;s laptop talked to his server (over HTTP), retrieved the main page&lt;br /&gt;
*** You can see the details of this in the web developer tools under the network tab! (press Ctrl-Shift-I)&lt;br /&gt;
*** Also loaded a bunch of resources&lt;br /&gt;
** In general, for any site:&lt;br /&gt;
*** Browser sends a GET request for the URL to a web server&lt;br /&gt;
*** Web server sends back a document&lt;br /&gt;
*** Browser renders document and makes more GET requests&lt;br /&gt;
&lt;br /&gt;
==== Node REPL/Intro to JavaScript ====&lt;br /&gt;
&lt;br /&gt;
* Most interpreted languages have a REPL (Read-Eval-Print-Loop, interactive mode)&lt;br /&gt;
* Node is JIT compiled&lt;br /&gt;
* Simple arithmetic/equality examples:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&amp;amp;gt;2+2&lt;br /&gt;
4&lt;br /&gt;
&amp;amp;gt;5 === 2&lt;br /&gt;
false&lt;br /&gt;
&amp;amp;gt;5 === 5&lt;br /&gt;
true&lt;br /&gt;
&amp;amp;gt;var x;&lt;br /&gt;
undefined&lt;br /&gt;
&amp;amp;gt;x&lt;br /&gt;
undefined&lt;br /&gt;
&amp;amp;gt;x + 3&lt;br /&gt;
NaN&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* Typos are hard to find in JS (you can use a variable without defining it)&lt;br /&gt;
* Example: Reading a file&lt;br /&gt;
** &amp;lt;code&amp;gt;console.log()&amp;lt;/code&amp;gt; prints to the console (ex. &amp;lt;code&amp;gt;console.log(&amp;amp;quot;Hi there!&amp;amp;quot;)&amp;lt;/code&amp;gt;)&lt;br /&gt;
** You have to &amp;amp;quot;require&amp;amp;quot; the &amp;amp;quot;fs&amp;amp;quot; module: &amp;lt;code&amp;gt;var fs = require(&amp;amp;quot;fs&amp;amp;quot;);&amp;lt;/code&amp;gt;&lt;br /&gt;
** To read a file (synchronously): &amp;lt;code&amp;gt;fs.readFileSync(&amp;amp;quot;testfile.txt&amp;amp;quot;);&amp;lt;/code&amp;gt;, returns bytes.&lt;br /&gt;
*** Specify an encoding, utf-8 is pretty standard. Otherwise it doesn&#039;t know what kind of text (or even binary data!) it is&lt;br /&gt;
*** Specifically in our example: &amp;lt;code&amp;gt;var contents = fs.readFileSync(&amp;amp;quot;testfile.txt&amp;amp;quot;, &amp;amp;quot;utf-8&amp;amp;quot;);&amp;lt;/code&amp;gt;&lt;br /&gt;
** What does sync mean?&lt;br /&gt;
*** Synchronicity: Code is done line by line, waiting for operations to complete&lt;br /&gt;
*** In this example, our code waited for the readFileSync to end&lt;br /&gt;
* Asynchronous programming:&lt;br /&gt;
** You have to specify a callback function - what to do when the operation is done. For our example: &amp;lt;code&amp;gt;var callback = function(err, data) {console.log(&amp;amp;quot;Data read:\n&amp;amp;quot; + data);}&amp;lt;/code&amp;gt;&lt;br /&gt;
** To use this callback: &amp;lt;code&amp;gt;fs.readFile(&amp;amp;quot;testfile.txt&amp;amp;quot;, &amp;amp;quot;utf-8&amp;amp;quot;, callback);&amp;lt;/code&amp;gt;&lt;br /&gt;
** This will call &amp;amp;quot;callback&amp;amp;quot; when the data is done reading&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_1&amp;diff=20448</id>
		<title>WebFund 2016W Lecture 1</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2016W_Lecture_1&amp;diff=20448"/>
		<updated>2016-01-09T02:58:45Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on January 7, 2016 [http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/lectures/comp2406-2016w-lec01-07Jan2016.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
== Topics &amp;amp;amp; Readings ==&lt;br /&gt;
&lt;br /&gt;
* Course Logistics&lt;br /&gt;
* Strategies for succeeding in 2406&lt;br /&gt;
* Basics of web technologies&lt;br /&gt;
* Node REPL/Intro to JavaScript&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
===In-Class Notes===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Lecture 1&lt;br /&gt;
---------&lt;br /&gt;
&lt;br /&gt;
web servers&lt;br /&gt;
  - apache, nginx&lt;br /&gt;
&lt;br /&gt;
web browsers (clients)&lt;br /&gt;
  - firefox, chrome, IE, Safari&lt;br /&gt;
  &lt;br /&gt;
web protocols&lt;br /&gt;
  - HTTP&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When you view a web page:&lt;br /&gt;
 - browser sends a GET request for the URL to a web server&lt;br /&gt;
 - web server sends back a document&lt;br /&gt;
 - browser renders document and makes more GET requests&lt;br /&gt;
&lt;br /&gt;
Web pages are, at base, HTML documents with other stuff&lt;br /&gt;
&lt;br /&gt;
Major web renderers&lt;br /&gt;
 - Gecko (Firefox)&lt;br /&gt;
 - Blink (Chrome, fork of WebKit)&lt;br /&gt;
 - Trident (MSHTML)&lt;br /&gt;
 - WebKit&lt;br /&gt;
 - EdgeHTML (fork of Trident)&lt;br /&gt;
&lt;br /&gt;
Web renderers interpret:&lt;br /&gt;
 - HTML&lt;br /&gt;
 - CSS&lt;br /&gt;
 - JavaScript&lt;br /&gt;
&lt;br /&gt;
(and they incorporate images and videos)&lt;br /&gt;
&lt;br /&gt;
HTML - specifies basic structure and content of &amp;quot;page&amp;quot;&lt;br /&gt;
CSS - makes it pretty (fonts, layout, etc)&lt;br /&gt;
JavaScript - code&lt;br /&gt;
&lt;br /&gt;
Read-Eval-Print loop (REPL)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Student Notes ===&lt;br /&gt;
&lt;br /&gt;
==== Course Logistics ====&lt;br /&gt;
&lt;br /&gt;
* This course is about &#039;&#039;forming a conceptual model of the web&#039;&#039;&lt;br /&gt;
* Notes and other content from previous years can be found on the wiki&lt;br /&gt;
* Anil &amp;amp;amp; TA&#039;s have office hours&lt;br /&gt;
* Tutorials are required&lt;br /&gt;
** Come see a TA promptly if you miss one (do the work beforehand)&lt;br /&gt;
* Mark Breakdown Highlights&lt;br /&gt;
** Assignments (lowest grade dropped)&lt;br /&gt;
** Tutorials (20%!! Very important)&lt;br /&gt;
* Collaboration&lt;br /&gt;
** Don&#039;t transfer bits to each other&lt;br /&gt;
** Don&#039;t submit the same thing&lt;br /&gt;
** Cite!!&lt;br /&gt;
&lt;br /&gt;
==== Strategies for succeeding ====&lt;br /&gt;
&lt;br /&gt;
* Don&#039;t get discouraged! This course is targeted at a wide demographic so there might be stuff you don&#039;t understand.&lt;br /&gt;
* Try to keep up in your understanding of the general course content. You should not be trying to piece things together from many code snippets from web searches. The core material is all provided for you.&lt;br /&gt;
&lt;br /&gt;
==== Basics of web technology ====&lt;br /&gt;
&lt;br /&gt;
* Linux environment provided, but you don&#039;t have to use it&lt;br /&gt;
* Node&lt;br /&gt;
** Chrome&#039;s JavaScript engine + some extra capabilities such as networking and disk I/O&lt;br /&gt;
** Used for server-side development&lt;br /&gt;
* Important concepts:&lt;br /&gt;
** Web servers - Apache, Nginx&lt;br /&gt;
** Web browsers (clients) - Firefox, Chrome, IE, Safari, Edge&lt;br /&gt;
*** Browsers are more complicated than servers&lt;br /&gt;
*** Rendering Engines&lt;br /&gt;
*** Gecko (Firefox), Blink (Chrome, fork of Webkit), Trident (MSHTML), WebKit, EdgeHTML (Opera used to have it&#039;s own)&lt;br /&gt;
*** Web renderers interpret HTML, CSS, and JavaScript&lt;br /&gt;
**** HTML specifies layout&lt;br /&gt;
**** CSS makes it pretty&lt;br /&gt;
**** JavaScript is the code!&lt;br /&gt;
**** We use JS on the server BECAUSE it&#039;s on the client (other languages could be used instead)&lt;br /&gt;
** Web protocols - HTTP&lt;br /&gt;
* What happens when you load the wiki?&lt;br /&gt;
** Homeostasis server is running Ubuntu, with Apache configured to run MediaWiki with PHP&lt;br /&gt;
** Anil&#039;s laptop talked to his server (over HTTP), retrieved the main page&lt;br /&gt;
*** You can see the details of this in the web developer tools under the network tab! (press Ctrl-Shift-I)&lt;br /&gt;
*** Also loaded a bunch of resources&lt;br /&gt;
** In general, for any site:&lt;br /&gt;
*** Browser sends a GET request for the URL to a web server&lt;br /&gt;
*** Web server sends back a document&lt;br /&gt;
*** Browser renders document and makes more GET requests&lt;br /&gt;
&lt;br /&gt;
==== Node REPL/Intro to JavaScript ====&lt;br /&gt;
&lt;br /&gt;
* Most interpreted languages have a REPL (Read-Eval-Print-Loop, interactive mode)&lt;br /&gt;
* Node is JIT compiled&lt;br /&gt;
* Simple arithmetic/equality examples:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&amp;amp;gt;2+2&lt;br /&gt;
4&lt;br /&gt;
&amp;amp;gt;5 === 2&lt;br /&gt;
false&lt;br /&gt;
&amp;amp;gt;5 === 5&lt;br /&gt;
true&lt;br /&gt;
&amp;amp;gt;var x;&lt;br /&gt;
undefined&lt;br /&gt;
&amp;amp;gt;x&lt;br /&gt;
undefined&lt;br /&gt;
&amp;amp;gt;x + 3&lt;br /&gt;
NaN&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
* Typos are hard to find in JS (you can use a variable without defining it)&lt;br /&gt;
* Example: Reading a file&lt;br /&gt;
** &amp;lt;code&amp;gt;console.log()&amp;lt;/code&amp;gt; prints to the console (ex. &amp;lt;code&amp;gt;console.log(&amp;amp;quot;Hi there!&amp;amp;quot;)&amp;lt;/code&amp;gt;)&lt;br /&gt;
** You have to &amp;amp;quot;require&amp;amp;quot; the &amp;amp;quot;fs&amp;amp;quot; module: &amp;lt;code&amp;gt;var fs = require(&amp;amp;quot;fs&amp;amp;quot;);&amp;lt;/code&amp;gt;&lt;br /&gt;
** To read a file (synchronously): &amp;lt;code&amp;gt;fs.readFileSync(&amp;amp;quot;testfile.txt&amp;amp;quot;);&amp;lt;/code&amp;gt;, returns bytes.&lt;br /&gt;
*** Specify an encoding, utf-8 is pretty standard. Otherwise it doesn&#039;t know what kind of text (or even binary data!) it is&lt;br /&gt;
*** Specifically in our example: &amp;lt;code&amp;gt;var contents = fs.readFileSync(&amp;amp;quot;testfile.txt&amp;amp;quot;, &amp;amp;quot;utf-8&amp;amp;quot;);&amp;lt;/code&amp;gt;&lt;br /&gt;
** What does sync mean?&lt;br /&gt;
*** Synchronicity: Code is done line by line, waiting for operations to complete&lt;br /&gt;
*** In this example, our code waited for the readFileSync to end&lt;br /&gt;
* Asynchronous programming:&lt;br /&gt;
** You have to specify a callback function - what to do when the operation is done. For our example: &amp;lt;code&amp;gt;var callback = function(err, data) {console.log(&amp;amp;quot;Data read:\n&amp;amp;quot; + data);}&amp;lt;/code&amp;gt;&lt;br /&gt;
** To use this callback: &amp;lt;code&amp;gt;fs.readFile(&amp;amp;quot;testfile.txt&amp;amp;quot;, &amp;amp;quot;utf-8&amp;amp;quot;, callback);&amp;lt;/code&amp;gt;&lt;br /&gt;
** This will call &amp;amp;quot;callback&amp;amp;quot; when the data is done reading&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Final_Exam_Review&amp;diff=20185</id>
		<title>WebFund 2015W Final Exam Review</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Final_Exam_Review&amp;diff=20185"/>
		<updated>2015-04-11T22:54:05Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: /* Notes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
==Audio==&lt;br /&gt;
&lt;br /&gt;
The audio from the exam review given on April 10, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-examreview-10Apr2015.m4a is now available].  (Note: the file does not play in Firefox unfortunately.)&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
The final exam will be based on [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/code/exam-notes.zip exam-notes], a simplified version of the solutions to tls-notes from Assignment 10.&lt;br /&gt;
&lt;br /&gt;
There will be very little in terms of code that you must write on the exam, no more than a few lines for small implementations. While your answers can be pure code, it is better to provide written answers with high level explanations.&lt;br /&gt;
&lt;br /&gt;
Review of the different contexts for functions (the main difference between these contexts is the use of the keyword &#039;this&#039;):&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Regular function call - keyword &#039;this&#039; refers to the global object&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Method call - keyword &#039;this&#039; refers to the object for which the method was called&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Constructor call - keyword &#039;this&#039; refers to the object being constructed&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Apply call - keyword &#039;this&#039; refers to the parameter supplied to the &amp;lt;code&amp;gt;apply()&amp;lt;/code&amp;gt; call indicating what the function is being applied to&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Differentiation between client side and server side&amp;lt;/h3&amp;gt;&lt;br /&gt;
On the client, HTML is rendered by the browser and used to construct the DOM. JavaScript can also be provided to be run inside the browser and interact with the DOM.&lt;br /&gt;
&lt;br /&gt;
On the server, JavaScript is run as an application to provided all necessary server functionality.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Communications between client and server&amp;lt;/h3&amp;gt;&lt;br /&gt;
The two sides (client and server) talk to each other using HTTP. HTTP is built on top of other technologies but it is essentially just structured data being sent back and forth.&lt;br /&gt;
&lt;br /&gt;
A server cannot initiate communication by sending a response to a client. The client must first send a request to the server, the server may then respond to that request. Requests are sent to a server due to actions taken by a client such as trying to load a page or a user clicking on a button which submits a form. A response typically contains HTML, CSS or JavaScript code. A response may cause subsequent requests to be made in order to obtain linked resources (images, stylesheets, scripts, etc).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Older servers (static servers)&amp;lt;/h3&amp;gt;&lt;br /&gt;
On an older web server, you would have files such as HTML documents, images, stylesheets, etc. These are static resources which could be served &#039;as is&#039; by the server. A new web server such as one implemented in Node can easily serve resources as a static web server but its main benefit is the ability to run additional code when a request comes in. JavaScript can be used in this context to generate HTML or do many other things.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;JavaScript execution environments&amp;lt;/h3&amp;gt;&lt;br /&gt;
Node.js (run on the server) is an execution environment for JavaScript. Similarly, there is an execution environment for JavaScript in web browsers to allow for the client to run JavaScript code. These executions environments have differences between them. The browser environment is sandboxed so that it cannot access the rest of the computer (file I/O, networking).&lt;br /&gt;
&lt;br /&gt;
JavaScript run in the browser is meant for two main purposes:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Manipulation of the DOM - Changing the structure of the page, updating the interface, etc...&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Sending AJAX requests (asynchronous GET and POST requests) - This allows the client to communicate with the server in the background, bypassing the need to use HTML elements to trigger communications as well as the need to reload a page when receiving data.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Exam code&amp;lt;/h3&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Differentiation between client side code and server side code&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;The code in index.js is all run on the server.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Anything in the public folder is sent directly to the client. Scripts and HTML code sent from this folder are run on the client.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Jade templates are rendered in HTML on the server and then sent to the client to have the HTML rendered by the browser.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;What does register.js do?&amp;lt;/h4&amp;gt;&lt;br /&gt;
First, recall that $ refers to jQuery, it represents the jQuery object. By providing a single parameter to jQuery as a function, we are defining an on-load function. This means that the function is run once the page is fully loaded. The first line in this function uses jQuery to find an HTML element with an ID of &#039;register&#039; and calls to &amp;lt;code&amp;gt;on()&amp;lt;/code&amp;gt; method of the element to define a function for a click event. The function finds a form element in the DOM and changes its action from /login to /register and then submits the form.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Other notes and tips&amp;lt;/h3&amp;gt;&lt;br /&gt;
You must fully understand the definition of routes that occurs in index.js. The router methods such a &amp;lt;code&amp;gt;get()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;post()&amp;lt;/code&amp;gt; are run only once when the index.js file is run during the startup of the server. These methods register a callback function for the specified route so that whenever a request comes to the server for that route, the callback function is run. The callback function is not run until such a request comes in and so it could potentially never be run if a request never comes in for that route.&lt;br /&gt;
&lt;br /&gt;
It is important to recognize that functions are treated as first class objects in JavaScript. They can be treated just as other objects can, meaning that they can even be used as parameters for another function.&lt;br /&gt;
&lt;br /&gt;
Notice that the exam code does not use bcrypt. The passwords are stored in plain text. This makes the code much simpler but it is a very bad practice. This makes your database very vulnerable as all passwords are completely visible in plain text if anyone happens to get access to the database. You should never do this.&lt;br /&gt;
&lt;br /&gt;
Recall that hashing and encryption are different. With encryption, you can decrypt the data back to its original format if you have the proper key. With hashing, you are producing a fixed length representation of the data based on a hashing function. You do not expect to be able to retrieve data that has been hashed, only verify that something matches what you expect.&lt;br /&gt;
&lt;br /&gt;
Expect questions on the exam about what causes certain requests and what type of response you would expect the server to produce and why.&lt;br /&gt;
&lt;br /&gt;
==Code==&lt;br /&gt;
&lt;br /&gt;
===routes/index.js===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source line lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var express = require(&#039;express&#039;);&lt;br /&gt;
var router = express.Router();&lt;br /&gt;
var mongodb = require(&#039;mongodb&#039;);&lt;br /&gt;
var mc = mongodb.MongoClient;&lt;br /&gt;
var ObjectID = mongodb.ObjectID;&lt;br /&gt;
&lt;br /&gt;
var notesCollection, usersCollection;&lt;br /&gt;
&lt;br /&gt;
mc.connect(&#039;mongodb://localhost/exam-notes&#039;, function(err, db) {&lt;br /&gt;
    if (err) {&lt;br /&gt;
        throw err;&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    notesCollection = db.collection(&#039;notes&#039;);&lt;br /&gt;
    usersCollection = db.collection(&#039;users&#039;);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
router.post(&#039;/register&#039;, function(req, res) {&lt;br /&gt;
    var username = req.body.username;&lt;br /&gt;
    var password = req.body.password;&lt;br /&gt;
&lt;br /&gt;
    var checkInsert = function(err, newUsers) {&lt;br /&gt;
        if (err) {&lt;br /&gt;
            res.redirect(&amp;quot;/?error=Unable to add user&amp;quot;);&lt;br /&gt;
        } else {&lt;br /&gt;
            res.redirect(&amp;quot;/?error=User &amp;quot; + username +&lt;br /&gt;
                         &amp;quot; successfully registered&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    var checkUsername = function(err, user) {&lt;br /&gt;
        if (err) {&lt;br /&gt;
            res.redirect(&amp;quot;/?error=unable to check username&amp;quot;);&lt;br /&gt;
        } else if (user === null) {&lt;br /&gt;
            var newUser = {&lt;br /&gt;
                username: username,&lt;br /&gt;
                password: password&lt;br /&gt;
            };&lt;br /&gt;
            usersCollection.update({username: username},&lt;br /&gt;
                                   newUser,&lt;br /&gt;
                                   {upsert: true},&lt;br /&gt;
                                   checkInsert);    &lt;br /&gt;
&lt;br /&gt;
        } else {&lt;br /&gt;
            res.redirect(&amp;quot;/?error=user already exists&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    usersCollection.findOne({username: username}, checkUsername);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
router.get(&#039;/&#039;, function(req, res) {&lt;br /&gt;
    if (req.session.username) {&lt;br /&gt;
        res.redirect(&amp;quot;/notes&amp;quot;);&lt;br /&gt;
    } else {&lt;br /&gt;
        res.render(&#039;index&#039;, { title: &#039;COMP 2406 Exam Notes Demo&#039;, &lt;br /&gt;
                              error: req.query.error });&lt;br /&gt;
    }&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
router.get(&#039;/notes&#039;, function(req, res) {&lt;br /&gt;
    var username = req.session.username;&lt;br /&gt;
&lt;br /&gt;
    if (username) {&lt;br /&gt;
        res.render(&amp;quot;notes.jade&amp;quot;, {username: username,&lt;br /&gt;
                                  title: username +&amp;quot;&#039;s Notes&amp;quot;});&lt;br /&gt;
    } else {&lt;br /&gt;
        res.redirect(&amp;quot;/?error=Not Logged In&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
router.post(&#039;/login&#039;, function(req, res) {&lt;br /&gt;
    var username = req.body.username;&lt;br /&gt;
    var password = req.body.password;&lt;br /&gt;
    &lt;br /&gt;
    var authenticateUser = function(err, user){&lt;br /&gt;
        if (err || user === null || password !== user.password) {&lt;br /&gt;
            res.redirect(&amp;quot;/?error=invalid username or password&amp;quot;);       &lt;br /&gt;
        } else {&lt;br /&gt;
            req.session.username = username;&lt;br /&gt;
            res.redirect(&amp;quot;/notes&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    usersCollection.findOne({username: username}, authenticateUser);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
router.post(&#039;/logout&#039;, function(req, res) {&lt;br /&gt;
    req.session.destroy(function(err){&lt;br /&gt;
        if (err) {&lt;br /&gt;
            console.log(&amp;quot;Error: %s&amp;quot;, err);&lt;br /&gt;
        }&lt;br /&gt;
    });&lt;br /&gt;
    res.redirect(&amp;quot;/&amp;quot;);&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
router.get(&#039;/getNotes&#039;, function(req, res) {&lt;br /&gt;
    var username = req.session.username;&lt;br /&gt;
&lt;br /&gt;
    var renderNotes = function(err, notes) {&lt;br /&gt;
        if (err) {&lt;br /&gt;
            notes = [{&amp;quot;title&amp;quot;: &amp;quot;Couldn&#039;t get notes&amp;quot;,&lt;br /&gt;
                      &amp;quot;owner&amp;quot;: username,&lt;br /&gt;
                      &amp;quot;content&amp;quot;: &amp;quot;Error fetching notes!&amp;quot;}];&lt;br /&gt;
        }&lt;br /&gt;
        res.send(notes);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    if (username) {&lt;br /&gt;
        notesCollection.find({owner: username}).toArray(renderNotes);&lt;br /&gt;
    } else {&lt;br /&gt;
        res.send([{&amp;quot;title&amp;quot;: &amp;quot;Not Logged In&amp;quot;,&lt;br /&gt;
                   &amp;quot;owner&amp;quot;: &amp;quot;None&amp;quot;,&lt;br /&gt;
                   &amp;quot;content&amp;quot;: &amp;quot;Nobody seems to be logged in!&amp;quot;}]);&lt;br /&gt;
    }    &lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
router.post(&#039;/updateNote&#039;, function(req, res) {&lt;br /&gt;
    var username = req.session.username;&lt;br /&gt;
    var id = req.body.id;&lt;br /&gt;
    var title = req.body.title;&lt;br /&gt;
    var content = req.body.content;&lt;br /&gt;
    &lt;br /&gt;
    var checkUpdate = function(err, result) {&lt;br /&gt;
        if (err) {&lt;br /&gt;
            res.send(&amp;quot;ERROR: update failed&amp;quot;);&lt;br /&gt;
        } else {&lt;br /&gt;
            res.send(&amp;quot;update succeeded&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    if (username) {&lt;br /&gt;
        if (id &amp;amp;&amp;amp; title &amp;amp;&amp;amp; content) {&lt;br /&gt;
            notesCollection.update({_id: ObjectID(id)},&lt;br /&gt;
                                   {$set: {title: title,&lt;br /&gt;
                                           content: content}},&lt;br /&gt;
                                   checkUpdate);&lt;br /&gt;
        } else {&lt;br /&gt;
            res.send(&amp;quot;ERROR: bad parameters&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
    } else {&lt;br /&gt;
        res.send(&amp;quot;ERROR: not logged in&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
router.post(&#039;/newNote&#039;, function(req, res) {&lt;br /&gt;
    var username = req.session.username;&lt;br /&gt;
    var newNote;&lt;br /&gt;
&lt;br /&gt;
    var reportInserted = function(err, notesInserted) {&lt;br /&gt;
        if (err) {&lt;br /&gt;
            res.send(&amp;quot;ERROR: Could not create a new note&amp;quot;);&lt;br /&gt;
        } else {&lt;br /&gt;
            res.send(notesInserted[0]._id);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    if (username) {&lt;br /&gt;
        newNote = {title: &amp;quot;Untitled&amp;quot;,&lt;br /&gt;
                   owner: username,&lt;br /&gt;
                   content: &amp;quot;No content&amp;quot;};&lt;br /&gt;
&lt;br /&gt;
        notesCollection.insert(newNote, reportInserted);&lt;br /&gt;
    } else {&lt;br /&gt;
        res.send(&amp;quot;ERROR: Not Logged In&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
});&lt;br /&gt;
&lt;br /&gt;
module.exports = router;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===public/javascripts/register.js===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source line lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
$(function(){&lt;br /&gt;
    $(&amp;quot;#register&amp;quot;).on(&amp;quot;click&amp;quot;,function(){&lt;br /&gt;
        var $form = $(&amp;quot;form&amp;quot;);&lt;br /&gt;
        $form.attr(&amp;quot;action&amp;quot;, &amp;quot;/register&amp;quot;);&lt;br /&gt;
        $form.submit();&lt;br /&gt;
    });&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===public/javascripts/notes.js===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source line lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
$(function() {&lt;br /&gt;
    var insertNotesIntoDOM = function(userNotes) {&lt;br /&gt;
        var i, theNote;&lt;br /&gt;
&lt;br /&gt;
        $(&amp;quot;.notes&amp;quot;).remove();&lt;br /&gt;
&lt;br /&gt;
        if (userNotes.length &amp;lt; 1) {&lt;br /&gt;
            $(&amp;quot;#notesList&amp;quot;).append(&#039;&amp;lt;div class=&amp;quot;notes&amp;quot;&amp;gt;No notes!&amp;lt;/div&amp;gt;&#039;);&lt;br /&gt;
            return;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        for (i=0; i &amp;lt; userNotes.length; i++) {&lt;br /&gt;
            console.log(&amp;quot;Adding note &amp;quot; + i);&lt;br /&gt;
            theNote = userNotes[i];&lt;br /&gt;
            $(&amp;quot;#notesList&amp;quot;).append(&#039;&amp;lt;div class=&amp;quot;notes list-group-item &#039; +&lt;br /&gt;
				   &#039;list-group-item-default&amp;quot;&amp;gt; &#039; +&lt;br /&gt;
                                   &#039;&amp;lt;a href=&amp;quot;#&amp;quot; &#039; + &lt;br /&gt;
                                   &#039;class=&amp;quot;notes list-group-item-heading&amp;quot; &#039; +&lt;br /&gt;
                                   &#039;id=&amp;quot;edit&#039; + i + &#039;&amp;quot;&amp;gt;&#039; +&lt;br /&gt;
                                   &#039;&amp;lt;h4&amp;gt;&#039; + theNote.title + &#039;&amp;lt;/h4&amp;gt;&amp;lt;/a&amp;gt; &#039; +&lt;br /&gt;
                                   &#039;&amp;lt;p class=&amp;quot;list-group-item-text&amp;quot;&amp;gt;&#039; + &lt;br /&gt;
                                   theNote.content + &#039;&amp;lt;/p&amp;gt;&#039; +&lt;br /&gt;
                                   &#039;&amp;lt;/div&amp;gt;&#039;);&lt;br /&gt;
            $(&amp;quot;#edit&amp;quot; + i).click(userNotes[i], editNote);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    var getAndListNotes = function() {&lt;br /&gt;
        $.getJSON(&amp;quot;/getNotes&amp;quot;, insertNotesIntoDOM);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    var updateNoteOnServer = function(event) {&lt;br /&gt;
        var theNote = event.data;&lt;br /&gt;
        theNote.title = $(&amp;quot;#noteTitle&amp;quot;).val();&lt;br /&gt;
        theNote.content = $(&amp;quot;#noteContent&amp;quot;).val();&lt;br /&gt;
        noteListing();&lt;br /&gt;
        $.post(&amp;quot;/updateNote&amp;quot;,&lt;br /&gt;
               {&amp;quot;id&amp;quot;: theNote._id,&lt;br /&gt;
                &amp;quot;title&amp;quot;: theNote.title,&lt;br /&gt;
                &amp;quot;content&amp;quot;: theNote.content},&lt;br /&gt;
               getAndListNotes);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    var editNote = function(event) {&lt;br /&gt;
        var theNote = event.data;&lt;br /&gt;
&lt;br /&gt;
        $(&amp;quot;.notesArea&amp;quot;).replaceWith(&#039;&amp;lt;div class=&amp;quot;notesArea&amp;quot; &#039; +&lt;br /&gt;
                                    &#039;id=&amp;quot;editNoteDiv&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&#039;);&lt;br /&gt;
        $(&amp;quot;#editNoteDiv&amp;quot;).append(&#039;&amp;lt;h2&amp;gt;Edit Note&amp;lt;/h2&amp;gt;&#039;);&lt;br /&gt;
&lt;br /&gt;
        $(&amp;quot;#editNoteDiv&amp;quot;).append(&#039;&amp;lt;div&amp;gt;&amp;lt;input type=&amp;quot;text&amp;quot; &#039; +&lt;br /&gt;
                                 &#039;id=&amp;quot;noteTitle&amp;quot;&amp;gt;&amp;lt;/input&amp;gt;&amp;lt;/div&amp;gt;&#039;);&lt;br /&gt;
        $(&amp;quot;#noteTitle&amp;quot;).val(theNote.title);&lt;br /&gt;
&lt;br /&gt;
        $(&amp;quot;#editNoteDiv&amp;quot;).append(&#039;&amp;lt;div&amp;gt;&#039; +&lt;br /&gt;
                                 &#039;&amp;lt;textarea cols=&amp;quot;40&amp;quot; rows=&amp;quot;5&amp;quot; &#039; +&lt;br /&gt;
                                 &#039;id=&amp;quot;noteContent&amp;quot; wrap=&amp;quot;virtual&amp;quot;&amp;gt;&#039; +&lt;br /&gt;
                                 &#039;&amp;lt;/textarea&amp;gt;&amp;lt;/div&amp;gt;&#039;);&lt;br /&gt;
        $(&amp;quot;#noteContent&amp;quot;).val(theNote.content);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
        $(&amp;quot;#editNoteDiv&amp;quot;).append(&#039;&amp;lt;button type=&amp;quot;button&amp;quot; &#039; + &lt;br /&gt;
                                 &#039;class=&amp;quot;btn btn-success&amp;quot; &#039; +&lt;br /&gt;
                                 &#039;id=&amp;quot;updateNote&amp;quot;&amp;gt;&#039; +&lt;br /&gt;
                                 &#039;Update&amp;lt;/button&amp;gt;&#039;)&lt;br /&gt;
        $(&amp;quot;#updateNote&amp;quot;).click(theNote, updateNoteOnServer);&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    var editNewNote = function(result) {&lt;br /&gt;
        var theNote = {&amp;quot;title&amp;quot;: &amp;quot;&amp;quot;,&lt;br /&gt;
                       &amp;quot;content&amp;quot;: &amp;quot;&amp;quot;};&lt;br /&gt;
&lt;br /&gt;
        if (result.indexOf(&amp;quot;ERROR&amp;quot;) === -1) {&lt;br /&gt;
            theNote._id = result;&lt;br /&gt;
            editNote({data: theNote});&lt;br /&gt;
        } else {&lt;br /&gt;
            noteListing();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    var newNote = function() {&lt;br /&gt;
        $.post(&amp;quot;/newNote&amp;quot;, editNewNote);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    var noteListing = function() {&lt;br /&gt;
        $(&amp;quot;.notesArea&amp;quot;).replaceWith(&#039;&amp;lt;div class=&amp;quot;notesArea&amp;quot; &#039; +&lt;br /&gt;
                                    &#039;id=&amp;quot;listNotesDiv&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&#039;);&lt;br /&gt;
&lt;br /&gt;
        $(&amp;quot;#listNotesDiv&amp;quot;).append(&#039;&amp;lt;h2&amp;gt;Notes&amp;lt;/h2&amp;gt;&#039;);&lt;br /&gt;
        $(&amp;quot;#listNotesDiv&amp;quot;).append(&#039;&amp;lt;div id=&amp;quot;notesList&amp;quot; class=&amp;quot;list-group well&amp;quot;&amp;gt;&#039; +&lt;br /&gt;
                                  &#039;&amp;lt;a class=&amp;quot;notes list-group-item&amp;quot;&amp;gt;Loading Notes...&amp;lt;/a&amp;gt;&#039; +&lt;br /&gt;
                                  &#039;&amp;lt;/div&amp;gt;&#039;);&lt;br /&gt;
&lt;br /&gt;
        $(&amp;quot;#listNotesDiv&amp;quot;).append(&#039;&amp;lt;button id=&amp;quot;newNote&amp;quot;&#039; +&lt;br /&gt;
                                  &#039; class=&amp;quot;btn btn-primary&amp;quot;&#039; +&lt;br /&gt;
                                  &#039; type=&amp;quot;button&amp;quot;&amp;gt;&#039; +&lt;br /&gt;
                                  &#039;New Note&amp;lt;/button&amp;gt;&#039;);&lt;br /&gt;
        $(&amp;quot;#newNote&amp;quot;).click(newNote);&lt;br /&gt;
&lt;br /&gt;
        getAndListNotes();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    noteListing();&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===views/layout.jade===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source line lang=&amp;quot;html5&amp;quot;&amp;gt;&lt;br /&gt;
doctype html&lt;br /&gt;
html&lt;br /&gt;
  head&lt;br /&gt;
    title= title&lt;br /&gt;
    link(rel=&#039;stylesheet&#039;, href=&#039;/libs/bootstrap/css/bootstrap.css&#039;)&lt;br /&gt;
    link(rel=&#039;stylesheet&#039;, href=&#039;/libs/bootstrap/css/bootstrap-theme.css&#039;)&lt;br /&gt;
    link(rel=&#039;stylesheet&#039;, href=&#039;/stylesheets/style.css&#039;)&lt;br /&gt;
    script(src=&#039;/libs/jquery.js&#039;)&lt;br /&gt;
    script(src=&#039;/libs/bootstrap/js/bootstrap.js&#039;)&lt;br /&gt;
    block header&lt;br /&gt;
  body(role=&amp;quot;document&amp;quot;)&lt;br /&gt;
    block content&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===views/index.jade===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source line lang=&amp;quot;html5&amp;quot;&amp;gt;&lt;br /&gt;
extends layout&lt;br /&gt;
&lt;br /&gt;
block header&lt;br /&gt;
  script(src=&amp;quot;javascripts/register.js&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
block content&lt;br /&gt;
  h1= title&lt;br /&gt;
  p Welcome to #{title}&lt;br /&gt;
  - if(error)&lt;br /&gt;
    div.error #{error}&lt;br /&gt;
  div&lt;br /&gt;
    form(action=&amp;quot;/login&amp;quot;, method=&amp;quot;post&amp;quot;)&lt;br /&gt;
        div.form-group&lt;br /&gt;
            label(for=&amp;quot;username&amp;quot;) Username&lt;br /&gt;
            input.form-control.limitwidth(type=&amp;quot;text&amp;quot;, name=&amp;quot;username&amp;quot;)&lt;br /&gt;
        div.form-group&lt;br /&gt;
            label(for=&amp;quot;password&amp;quot;) Password&lt;br /&gt;
            input.form-control.limitwidth(type=&amp;quot;password&amp;quot;, name=&amp;quot;password&amp;quot;)&lt;br /&gt;
        button.btn.btn-default(type=&amp;quot;submit&amp;quot;) Login&lt;br /&gt;
        button#register.btn.btn-warning(type=&amp;quot;button&amp;quot;) Register&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===views/notes.jade===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source line lang=&amp;quot;html5&amp;quot;&amp;gt;&lt;br /&gt;
extends layout&lt;br /&gt;
&lt;br /&gt;
block header&lt;br /&gt;
  script(src=&amp;quot;javascripts/notes.js&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
block content&lt;br /&gt;
  div.page-header&lt;br /&gt;
    h1#pageTitle #{username}&amp;amp;#39;s Account&lt;br /&gt;
    p#pageWelcome Welcome #{username}&lt;br /&gt;
&lt;br /&gt;
  form(action=&amp;quot;/logout&amp;quot;, method=&amp;quot;post&amp;quot;)&lt;br /&gt;
        button.btn.btn-info(type=&amp;quot;submit&amp;quot;) Logout&lt;br /&gt;
  &lt;br /&gt;
  div.notesArea&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===views/error.jade===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source line lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
extends layout&lt;br /&gt;
&lt;br /&gt;
block content&lt;br /&gt;
  h1= message&lt;br /&gt;
  h2= error.status&lt;br /&gt;
  pre #{error.stack}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_20&amp;diff=20169</id>
		<title>WebFund 2015W Lecture 20</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_20&amp;diff=20169"/>
		<updated>2015-04-10T00:43:30Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on Monday, March 23, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-lec20-23Mar2015.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;h3&amp;gt;Socat&amp;lt;/h3&amp;gt;&lt;br /&gt;
You may be familiar with netcat which is somewhat of a swiss-army knife for dealing with network traffic. Socat is a better netcat. Socat allows you to view all of the traffic going back and forth over a TCP/IP connection.&lt;br /&gt;
&lt;br /&gt;
To use socat, you must:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Download and install it.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Run your web server.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Run socat as a mirror of the web server on a different port.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Use a web browser to connect to the socat mirrored server.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Using socat, we can see all of the contents of requests and responses going back and forth between the client and the server.&lt;br /&gt;
&lt;br /&gt;
&#039;&amp;gt;&#039; indicates the beginning of a request going to the server and shows the timestamp and how many bytes are going in. Similarly, &#039;&amp;lt;&#039; indicates the beginning of a response and has the same information.&lt;br /&gt;
&lt;br /&gt;
HTTP traffic uses a text format that comes from Windows machines (which uses text output with a carriage return and a line feed) by default. With the text output in a Unix box we see &#039;\r&#039; for the carriage return and then an actual line break for the line feed.&lt;br /&gt;
&lt;br /&gt;
If a request or response has a body then then will be an extra line break in between the head and the body.&lt;br /&gt;
&lt;br /&gt;
Requests show their method at the start. This will usually be GET or POST. The responses will start with &#039;HTTP/1.1 followed&#039; by the response code (eg 200 OK).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Assignment 5&amp;lt;/h3&amp;gt;&lt;br /&gt;
We want to make queryNotes.js from storeNotes.js and fileStats.js.&lt;br /&gt;
&lt;br /&gt;
fileStats.js takes in a command line argument. We can copy the code for fileStats.js into queryNotes.js. We would then create variables in which to store our expected command line arguments. The arguments can be parsed using familiar functions such as &amp;lt;code&amp;gt;length()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;slice()&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;indexOf()&amp;lt;/code&amp;gt;. By checking if a string such as &amp;quot;-output=&amp;quot; exists in an argument, we can determine whether or not it corresponds to the output argument. Because this process must be done for each argument, we would want to make it into a function.&lt;br /&gt;
&lt;br /&gt;
As you write code, you may find that some parts are redundant and can be factored out into functions. The important part of working on these assignments is not the solution but how to reason your way towards the solution.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Assignment 6&amp;lt;/h3&amp;gt;&lt;br /&gt;
To complete Question 1, you could go to views/notes.jade and change &amp;lt;code&amp;gt;p!= userNotes[i].content&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;p = userNotes[i].content&amp;lt;/code&amp;gt;. However, by doing this, it will no longer be possible to include links in the notes. Instead you must write your own function to sanitize the user input.&lt;br /&gt;
&lt;br /&gt;
The parsing of the links can be handled by once again using the string functions that you are familiar with. Using &amp;lt;code&amp;gt;split(&amp;quot;[&amp;quot;)&amp;lt;/code&amp;gt; separates each segment containing a link. You can then split these segments at the &amp;quot;]&amp;quot; and use &amp;lt;code&amp;gt;indexOf(&amp;quot; &amp;quot;)&amp;lt;/code&amp;gt; to find the division between the URL and the label. Once you have broken up all of the pieces, you can reassemble them into one string with HTML links included.&lt;br /&gt;
&lt;br /&gt;
To update the username, you will need to create a new route to handle the changeusername request. Inside this route, you must verify that the new username is not already in use and then make the update. You must both update the owner of the notes in the MongoDB collection using the collection&#039;s &amp;lt;code&amp;gt;update()&amp;lt;/code&amp;gt; method and you must update the session using &amp;lt;code&amp;gt;req.session.username = ...&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Assignment 9&amp;lt;/h3&amp;gt;&lt;br /&gt;
For Assignment 9, you need to capture a transcript of the HTTP request going out and the response returned by the ajax-notes server. You need to explain what was sent and why for both the requests and the responses. You must do this for the following routes:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;GET /&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;GET /javascripts/notes.js&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;GET /getNotes&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;POST /updateNote&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;POST /newNote&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_24&amp;diff=20163</id>
		<title>WebFund 2015W Lecture 24</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_24&amp;diff=20163"/>
		<updated>2015-04-07T17:14:15Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Audio==&lt;br /&gt;
&lt;br /&gt;
The audio from the lecture given on Monday, April 6, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-lec24-06Apr2015.m4a is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;On-screen notes&amp;lt;/h3&amp;gt;&lt;br /&gt;
====Administrative====&lt;br /&gt;
&lt;br /&gt;
* Optional review session in SC 103 (Steacie Bldg), Friday, April 10, 2-3:30 PM&lt;br /&gt;
* Assignment 10 solutions will be discussed in the review session (and posted then)&lt;br /&gt;
* Assignment 8 and 9 solutions are posted&lt;br /&gt;
* Final exam is on Tuesday, April 14th in Alumni Hall, rows 1-13&lt;br /&gt;
&lt;br /&gt;
====The Future====&lt;br /&gt;
&lt;br /&gt;
Before the web&lt;br /&gt;
* The Internet started late 1960s-early 1970s&lt;br /&gt;
* Web is 1993&lt;br /&gt;
* LAN protocols: file sharing (NFS), directory services (yp), remote login (rsh), remote &amp;quot;desktop&amp;quot; (X windows)&lt;br /&gt;
* WAN protocols: file transfer (ftp), email (smtp), remote login (telnet), net news (UUCP)&lt;br /&gt;
&lt;br /&gt;
Why the web?&lt;br /&gt;
* hypertext - linked information&lt;br /&gt;
* many older systems: MEMEX?, Xanadu, gopher, WAIS&lt;br /&gt;
* what was different about the web?&lt;br /&gt;
** presentation-oriented markup (&amp;quot;pretty&amp;quot; documents) - HTML&lt;br /&gt;
** IMAGES&lt;br /&gt;
** simple to implement renderer (browser) and network protocol (HTTP)&lt;br /&gt;
** stateless: scalable servers&lt;br /&gt;
** URLs&lt;br /&gt;
&lt;br /&gt;
What&#039;s changed since the beginning of the web?&lt;br /&gt;
* CSS: prettier pages&lt;br /&gt;
* JavaScript: dynamic pages&lt;br /&gt;
* plugins came and went: Java, Flash, Silverlight...&lt;br /&gt;
* kitchen sink, aka HTML5 - video, video chat, sockets, location, touch, WebGL&lt;br /&gt;
** richer input and output&lt;br /&gt;
** in the framework of HTML, CSS, JavaScript&lt;br /&gt;
&lt;br /&gt;
Why not just build separately?&lt;br /&gt;
* universal platforms tend to suck the oxygen out of the room&lt;br /&gt;
&lt;br /&gt;
Web is the future&lt;br /&gt;
&lt;br /&gt;
But won&#039;t anything else take over?&lt;br /&gt;
* evolution works, not revolution&lt;br /&gt;
* Java applets&lt;br /&gt;
* Flash&lt;br /&gt;
* Dart (now just compiled to JavaScript)&lt;br /&gt;
&lt;br /&gt;
What about other technologies?&lt;br /&gt;
* run on the server&lt;br /&gt;
* translate for the browser&lt;br /&gt;
&lt;br /&gt;
Other classes&lt;br /&gt;
* COMP 3000 (operating systems): what runs a browser or a web server&lt;br /&gt;
* COMP 3007 (programming paradigms): functional programming, which you kind of do in JavaScript&lt;br /&gt;
* COMP 3005 (databases): learn about SQL, data modelling&lt;br /&gt;
* COMP 3004: bigger programs, read code&lt;br /&gt;
* COMP 3804: see patterns underlying code&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Student notes&amp;lt;/h3&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Before the Web&amp;lt;/h4&amp;gt;&lt;br /&gt;
Before the Web, there was the Internet. The Internet started around the late 60&#039;s / early 70&#039;s. The Web itself began around 1993.&lt;br /&gt;
&lt;br /&gt;
Internet protocols before the Web were LAN (local area network) and WAN (wide area network).&lt;br /&gt;
LAN protocols:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;File sharing (NFS)&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Directory services (yp)&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Remote login (rsh)&lt;br /&gt;
  &amp;lt;li&amp;gt;Remote “desktop” (X windows)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
WAN protocols:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;File transfer (ftp)&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Email (point to point) (smtp)&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Remote login (telnet)&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;net news (UUCP)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each of the services using these protocols required a client which knows the protocol in order to be used. Just as a web browser is a client, email requires a client, remote login requires a client, etc...&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Beginnings of the Web&amp;lt;/h4&amp;gt;&lt;br /&gt;
What was it that the Web added to all of this? Hypertext - linked information. There were many other previous systems which attempted to do this as well such as MEMEX, Xanadu, gopher, WAIS.&lt;br /&gt;
&lt;br /&gt;
So what did the Web do differently from these?&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;The Web had more presentation-oriented markup (it made &#039;prettier&#039; documents) and handled things like images.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;It had a simpler implementation of its renderer (the borwser) and protocol (HTTP).&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;HTTP was also particularly beneficial because of the fact that it is stateless. This greatly helps the scalability of web servers since the protocol needs to do no extra work with states, it is all managed (or ignored) by the code on the server.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;The standardization of URLs brought about uniformity and universal access of resources.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Side note:&lt;br /&gt;
The NeXTSTEP operating system is the OS on which modern Apple operating systems are based. The previous Apple OS was not able to browse the web without crashing and thus required NeXTSTEP system to fix this problem.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;The modern Web and the future&amp;lt;/h4&amp;gt;&lt;br /&gt;
What has changed now since the beginnings of the Web?&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;CSS - Providing the ability to better manage layout and make pages look nicer&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;JavaScript - Providing the ability to make dynamic pages&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Plugins - Providing the ability to extend functionality (Things like Java, Flash, Silverlight... although these have somewhat come and gone)&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;The kitchen sink, aka HTML5 - Provides video, video chat, sockets, location, touch, WebGL... (Allows for richer input and output)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Things are now generally implemented within this framework (HTML, CSS, JavaScript) to extend functionality. This is done in the form of new tags, new functions, etc...&lt;br /&gt;
&lt;br /&gt;
Things could be implemented separately and may have a better implementation if done in that way but we do not usually want to do this. Universal platforms tend to suck the oxygen out of the room. Windows was the universal platform before the Web, and before Windows, Unix sort of held this title. Having a universal platform provides simplicity and easy adaptability.&lt;br /&gt;
&lt;br /&gt;
While the platform may continue to evolve and look different, the same basic pieces are likely to be used for quite some time. For example, while the communication protocol is continuing to change, it is still the same base HTTP concept.&lt;br /&gt;
&lt;br /&gt;
Won&#039;t other newer technologies take over?&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Evolution works, not revolution.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;There have been technologies like Java applets and Flash but these have had issues and are proprietary software. They are mostly on the way out now.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Google created Dart to try to improve on JavaScript... now Dart is just compiled to JavaScript. Nobody wants to support multiple runtimes if it is not necessary.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What about other languages?&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Other languages can be run on the server.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Code written in other languages to be run in the browser is cross-compiled. This can be done using asm.js: an extraordinarily optimizable, low-level subset of JavaScript.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If you look on Wikipedia, you can see that there are many server frameworks available. You can choose whatever matches your programming style and needs (although you may be stuck without a choice when working for a company that has already made their decision).&lt;br /&gt;
&lt;br /&gt;
How does all of this relate to other courses that you will be taking (or have already taken)?&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Comp 3000 (Operating systems): what runs a browser or a web server&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Comp 3007 (Programming paradigms): functional programming, which you kind of do in&lt;br /&gt;
JavaScript)&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Comp 3005 (Databases): SQL, data modelling, more sophisticated databases&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Comp 3004: Bigger programs, reading code&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Comp 3804: See underlying patterns in code and algorithms&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_23&amp;diff=20162</id>
		<title>WebFund 2015W Lecture 23</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_23&amp;diff=20162"/>
		<updated>2015-04-07T16:09:06Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Audio==&lt;br /&gt;
&lt;br /&gt;
The audio from the lecture given on April 1, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-lec23-01Apr2015.m4a is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;On-screen notes&amp;lt;/h3&amp;gt;&lt;br /&gt;
Two topics for last two lectures:&lt;br /&gt;
&lt;br /&gt;
* What are the fundamentals of web applications?&lt;br /&gt;
* Web applications everywhere&lt;br /&gt;
&lt;br /&gt;
====Distributed Applications====&lt;br /&gt;
&lt;br /&gt;
Applications that run on multiple computers&lt;br /&gt;
* separate memory, CPU, storage, communication (I/O)&lt;br /&gt;
* can apply this processes, virtual hosts, or physical hosts&lt;br /&gt;
&lt;br /&gt;
Localhost is useful when you want a distributed app on one computer&lt;br /&gt;
&lt;br /&gt;
* GET and POST can go essentially anywhere on the Internet&lt;br /&gt;
* allows for flexible combination of code and data&lt;br /&gt;
&lt;br /&gt;
* web applications are the premier form of distributed applications today&lt;br /&gt;
&lt;br /&gt;
* with distributed applications, you don&#039;t control all the computers &amp;quot;your&amp;quot; code runs on&lt;br /&gt;
&lt;br /&gt;
====Structured Persistence====&lt;br /&gt;
&lt;br /&gt;
Before this class, you worked with files&lt;br /&gt;
&lt;br /&gt;
Files are unstructured (on modern systems)&lt;br /&gt;
&lt;br /&gt;
On the web we use &amp;quot;databases&amp;quot;&lt;br /&gt;
* really, structured storage&lt;br /&gt;
&lt;br /&gt;
Why structured?&lt;br /&gt;
* convenience&lt;br /&gt;
* speed for &amp;quot;complex&amp;quot; queries&lt;br /&gt;
* concurrency &amp;amp; distribution&lt;br /&gt;
&lt;br /&gt;
====Front vs Back end====&lt;br /&gt;
&lt;br /&gt;
* separate parts of app for...&lt;br /&gt;
** doing computation/storage/real work (back end)&lt;br /&gt;
** providing the interface (front end)&lt;br /&gt;
&lt;br /&gt;
Can you do &amp;quot;real work&amp;quot; in the front end?&lt;br /&gt;
* Yes, but...&lt;br /&gt;
* Don&#039;t trust the answers&lt;br /&gt;
&lt;br /&gt;
Front end is untrusted, back end is trusted&lt;br /&gt;
&lt;br /&gt;
Modern web apps have many backends and can have multiple front ends (mobile, desktop)&lt;br /&gt;
* but &amp;quot;responsive design&amp;quot; says you should have one front end&lt;br /&gt;
&lt;br /&gt;
So why put stuff in the front end?&lt;br /&gt;
* lower the load on the back end&lt;br /&gt;
* but main reason is latency (of network communication)&lt;br /&gt;
&lt;br /&gt;
====Markup and Templates====&lt;br /&gt;
&lt;br /&gt;
HTML, CSS: markup&lt;br /&gt;
Jade: template language&lt;br /&gt;
&lt;br /&gt;
Markup is constraint or declarative programming&lt;br /&gt;
&lt;br /&gt;
template languages output markup (are meta-markup)&lt;br /&gt;
&lt;br /&gt;
====Trust====&lt;br /&gt;
&lt;br /&gt;
Who do you trust?&lt;br /&gt;
* library, runtime authors&lt;br /&gt;
* content providers&lt;br /&gt;
* ad providers&lt;br /&gt;
* whatever else you add to a page&lt;br /&gt;
&lt;br /&gt;
Trust means &amp;quot;who can screw you over&amp;quot;&lt;br /&gt;
* if they go bad how badly are you hurt&lt;br /&gt;
&lt;br /&gt;
Amazing the web works at all&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Student notes&amp;lt;/h3&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Distributed applications&amp;lt;/h4&amp;gt;&lt;br /&gt;
These are applications that run on multiple computers using separate memory, CPU, storage and communication(I/O). These computers don&#039;t have to be separate physical computers.&lt;br /&gt;
&lt;br /&gt;
Our web applications these days are at least in three pieces:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;One part that runs in the browser (client).&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;One part that runs as the server.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;One part that runs as the database(s).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then it goes up from there e.g: communications on twitter(your server interacting with twitter&#039;s)&lt;br /&gt;
&lt;br /&gt;
Importance of distributed apps:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;GET and POST can go essentially anywhere on the internet.&lt;br /&gt;
    &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;li&amp;gt;Allows for flexible combination of code and data.&amp;lt;/li&amp;gt;&lt;br /&gt;
    &amp;lt;/ul&amp;gt;&lt;br /&gt;
  &amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;With distributed applications, you don’t control all the computer your code runs on. Often, you will use third party libraries to run code that is not even your own. For example, you can use features from Google Analytics in your application but it is their code running on their server; this distributes the burden on your application, making it ore distributed.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Web applications are the main form of distributed applications today.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Localhost is useful when you want a distributed app on your own computer. i.e: when you want to have multiple processes talk on the same system, but you want to use the same method as you would to talk to other computers - you talk to localhost.&lt;br /&gt;
&lt;br /&gt;
Distributed applications typically communicate using an HTTP or HTTPS protocol as seen in assignment 9.&lt;br /&gt;
&lt;br /&gt;
Nota Bene(NB)&lt;br /&gt;
Google makes their money from search advertisement and imbedded advertisement on other pages.&lt;br /&gt;
Look at the script tags of various web pages and see if you understand them.&lt;br /&gt;
&lt;br /&gt;
The web is funded primarily by advertisement. The advertisement is done using JavaScript code. The JavaScript is running on untrusted browsers (machines of people that want to steal money) That&#039;s why the code is obfuscated (hard to read and figure out).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Structured persistence&amp;lt;/h4&amp;gt;&lt;br /&gt;
Previously, you have handled persistent storage by using files. Files are unstructured(on modern systems). On the web, we use databases.&lt;br /&gt;
&lt;br /&gt;
What are Databases? Databases are an organization system which enforce a structure on the stored data.&lt;br /&gt;
&lt;br /&gt;
Why structured?&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Convenience.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Speed for Complex Queries: This is primarily because files in the databases can be indexed on various attributes allowing for quick look-ups.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Concurrency and distribution: Key component of databases - concurrent access to data. This is not a simple task to manage. o not try to implement this yourself as you will probably do it poorly. Use an existing database suited to your needs.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Font vs back end&amp;lt;/h4&amp;gt;&lt;br /&gt;
This is the concept of separating parts of apps into:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;The back end - Where the real work is done&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;The front end - used for the interface&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Can the real work be done in the front end?&lt;br /&gt;
&lt;br /&gt;
Yes, but don&#039;t trust the answers. This is because the front end is running from an unknown source so it shouldn&#039;t be trusted. The integrity of your application cannot depend on it working properly.&lt;br /&gt;
&lt;br /&gt;
Back End - Trusted&lt;br /&gt;
Front End - Untrusted&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
YouTube uses Google&#039;s servers as the back end to manage videos and the front end is a user interface on the web page itself. Modern web apps may have many back ends.&lt;br /&gt;
&lt;br /&gt;
So why do computation/put stuff in the front end?&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;To lower the load on the back end.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;The main reason is the latency (of the network communication). The front end takes less time to perform actions and events compared with back end because back computations require communication with the server(s) to return information (which induces latency).&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A web application may also have multiple front ends (desktop, mobile, etc) however it is currently considered to be preferable to only have a single front end made with a responsive design such that it will adapt to whatever display it is used with.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Markup and templates&amp;lt;/h4&amp;gt;&lt;br /&gt;
What we have worked with:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;HTML&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;CSS&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Jade (template/markup language)&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are many others which exist as well. HTML and CSS are constantly evolving. Others include XML, Latex, etc... Even WYSIWYG programs like Microsoft Word have underlying markup languages.&lt;br /&gt;
&lt;br /&gt;
Markup is constraint or declarative programming.&lt;br /&gt;
&lt;br /&gt;
Template languages output markup (they are meta-markup).&lt;br /&gt;
&lt;br /&gt;
A markup language is a way of programming. You are declaring what something should be. You are not telling it exactly how to do it (it is not procedural), it is not object oriented but you are specifying constraints on it. So implicitly, a template language is a markup of a markup language. Markup languages are used for safety and efficiency reasons. i.e: less code to write, protection of input, etc....&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Trust&amp;lt;/h4&amp;gt;&lt;br /&gt;
When you run a web app, you have to trust various sources like: content providers, ad providers, library and runtime authors. Trust essentially refers to who can screw you over. So basically, if they go bad, how strongly does it affect you.&lt;br /&gt;
&lt;br /&gt;
Embedded JavaScript scripts have access to the entire DOM so if they wanted, they could modify the entire page as they please. Companies that have their scripts embedded in many pages across the web could do some serious damage if they wanted.&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_22&amp;diff=20097</id>
		<title>WebFund 2015W Lecture 22</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_22&amp;diff=20097"/>
		<updated>2015-04-03T21:22:33Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video from the lecture given on Monday, March 30, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-lec22-30Mar2015.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&amp;lt;h3&amp;gt;The tls-notes application&amp;lt;/h3&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Some changes in the application&amp;lt;/h4&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;The application now uses HTTPS as the protocol.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;You can log in with username as in the previous ajax-notes, however now you need a password as well.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Visually, the app is different. The buttons look “prettier”. That is because the app uses Bootstrap.&lt;br /&gt;
    &amp;lt;ul&amp;gt;&lt;br /&gt;
      &amp;lt;li&amp;gt;Bootstrap is a toolkit which can be used to help improve your application.&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;li&amp;gt;In order to use Bootstrap, you will need to include a reference to the script in the head of the layout template.&amp;lt;/li&amp;gt;&lt;br /&gt;
      &amp;lt;li&amp;gt;With Bootstrap linked, you can apply CSS classes to elements in your pages to make them look nicer.&amp;lt;/li&amp;gt;&lt;br /&gt;
    &amp;lt;/ul&amp;gt;&lt;br /&gt;
  &amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;There is no longer an edit button - note names are now links that go to the edit note view.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Change username is not working (fixing this is a requirement of the assignment).&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;There is another collection being stored in the database, the users collections.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Markup is basically the same with mild changes to notes.js adding divs and classes.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;A ConvertNote function is added to notes.js in order to escape HTML tags and parse links when you insert into the notes.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Basically the assignment consists of a lot of debugging the code to fix it the problems in the version of the app provided.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Storing passwords&amp;lt;/h4&amp;gt;&lt;br /&gt;
How do you store password on the server? – In MongoDB, within the users collection, all usernames and  passwords are stored. However; the password is hashed. To check a password, you hash some input provided and compare it to the hashed value that is stored. This is important because if someone hacks into your website, you do not want them to have your password and they cannot get it without figuring out the hash.&lt;br /&gt;
&lt;br /&gt;
You should never do the hashing on the client side, the server always does it. This is to prevent others from seeing what is done as this will reveal information to them about your hashing process.&lt;br /&gt;
&lt;br /&gt;
Inside the /register route of the tls-notes app, we can see a checkUsername function that uses bcrypt.hash. This takes an input string and hashes it. Before you call bcrypt.hash you must call bcrypt.gensalt. The salt is used to customize the hashed password. If you did not use a salt, a hacker can look up tables of hashed passwords but with the salt it changes the resultant hash value of the input string which makes it more secure. – We will not be tested on this, it is for personal interest.&lt;br /&gt;
&lt;br /&gt;
In the saveHash function in index.js you use an update function when adding a new user&lt;br /&gt;
because you first check if the user already exists and if they do it will never overwrite an account, it ‘updates it’.&lt;br /&gt;
&lt;br /&gt;
It is important to have a secure connection when working with passwords. If the connection is not secure, anyone can see your password if they look at the data you are sending to the server. Using password hashing without using a secure connection is insufficient.&lt;br /&gt;
&lt;br /&gt;
If you are interested in hashing and security against hacking – look up “rainbow tables”.&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_21&amp;diff=20096</id>
		<title>WebFund 2015W Lecture 21</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_21&amp;diff=20096"/>
		<updated>2015-04-03T20:42:34Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on Wednesday, March 25, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-lec21-25Mar2015.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
Google uses a ton of cookies when you access their server. They use these for tracking who you are and it is vital to their advertising business. Knowing who you are allows advertisers to show you products that you would buy.&lt;br /&gt;
&lt;br /&gt;
Duckduckgo is another search engine which does not use any cookies!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;TLS/SSL&amp;lt;/h3&amp;gt;&lt;br /&gt;
&amp;lt;h4&amp;gt;Encryption and certificates&amp;lt;/h4&amp;gt;&lt;br /&gt;
HTTPS:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Merely HTTP over a secured connection&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Uses port 443 by default&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With HTTPS, everything is encrypted as it is sent. A server is certified by using an SSL certificate which is digitally signed. This is a security certificate, it identifies that the server you are connecting to is recognized/trusted (not someone who may be intercepting your connection).&lt;br /&gt;
&lt;br /&gt;
These certificates are verified by companies such as DigiCert, and these are called SSL Certificates. You can SAY you are facebook.com, but you can lie about that, so you need someone else to verify your identity. Unfortunately, some certificate authorities can be scams, and issue certificates that are &#039;spoofed&#039;.&lt;br /&gt;
&lt;br /&gt;
When you see warnings in the browser, this means that there is a problem with the certificate, as it cannot be verified. When this happens, you should check the details of the certificate.&lt;br /&gt;
&lt;br /&gt;
When you are using HTTPS, everyone can see your traffic, but it is completely encrypted, and thus it looks like garbage to everyone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;HTTPS demo code&amp;lt;/h4&amp;gt;&lt;br /&gt;
When running the TLS notes demo, the browser says there is a problem with the certificate, and that it isn&#039;t valid. It says it is self-signed and expired (usually this == BAD). In this instance, it is ok because we know that we made the certificate so we trust it.&lt;br /&gt;
&lt;br /&gt;
To adapt a previous application to use HTTPS, you must change the &amp;lt;code&amp;gt;require(http)&amp;lt;/code&amp;gt; to &amp;lt;code&amp;gt;require(https)&amp;lt;/code&amp;gt;. This causes the server to use HTTPS as the protocol instead of HTTP. This is within the bin/www file.&lt;br /&gt;
&lt;br /&gt;
For an HTTPS server, we must specify some options. These options define the public and private keys/certificates. These are loaded using the fs module and store in an options object which is used during the creation of the server.&lt;br /&gt;
&lt;br /&gt;
Keys and certificates can be generated using openSSL. The public key is given out to the clients of the server for them to encrypt data. The private key is used by the server for decryption.&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_19&amp;diff=20018</id>
		<title>WebFund 2015W Lecture 19</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_19&amp;diff=20018"/>
		<updated>2015-03-19T17:55:13Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on Wednesday, March 18, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-lec19-18Mar2015.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Assignment due dates&amp;lt;/h3&amp;gt;&lt;br /&gt;
Assignments 5-7 can be submitted late up till Monday March 23 (with a small grade reduction).&lt;br /&gt;
&lt;br /&gt;
Assignment 8 has been  moved to Wednesday March 25.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Regular expressions&amp;lt;/h3&amp;gt;&lt;br /&gt;
In order to understand what a regular expression really is, we need a bit of background information.&lt;br /&gt;
&lt;br /&gt;
The Chomsky Hierarchy defines the classes of formal grammars you can create. They are broken into different classifications based on how “powerful” the language is or how much variation it has. A formal grammar is a set of production rules for strings; the rules describe what syntax is valid for the language. A formal grammar has the following things:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;A finite set of nonterminal symbols indicating that some production rule can yet be applied.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;A finite set of terminal symbols indicating that no production rule can be applied.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;A start symbol.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;em&amp;gt;(Formal grammar definition referenced from Wikipedia.)&amp;lt;/em&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Hierarchy breaks the grammars down into the following 4 groups:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Type 3 - Regular Expression: Think of it as a a grammar that can be parsed by a little machine with finite states (finite automaton), it can’t remember a large quantity of things.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Type 2 - Context Free: They use a stack. This encompasses most programming languages.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Type 1 - Context Sensitive: What a computer can do with a fixed amount of space.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Type 0 - Recursively Enumerable: Anything a computer can do.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is a finite automaton?&lt;br /&gt;
An example of a simple deterministic finite automaton would be a program with 3 states that can receive a 0 or 1. See the [http://en.wikipedia.org/wiki/Deterministic_finite_automaton#/media/File:DFA_example_multiplies_of_3.svg Wikipedia example].&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 0 and receive a 0, return to state 0.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 0 and receive a 1, go to state 1.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 1 and receive a 0, go to state 2.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 1 and receive a 1, go to state 0.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 2 and receive a 0, go to state 1.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 2 and receive a 1, return to state 2.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Wikipedia says it is a sequence of characters that forms a search pattern. As seen from the Chomsky Hierarchy, we also know that it is a formal grammar.&lt;br /&gt;
&lt;br /&gt;
Formal grammar was initially used in linguistics but adopted by Computer Science. To represent the structure of natural language you parse it using rules - the grammar. Similarly, in Computer Science, a grammar is a set of rules specifying a language, also known as syntax. The simplest languages are ones like LISP (blah(blah blah blah)). Contains lots of parenthesis, symbols, numbers, it is very easy to parse.&lt;br /&gt;
&lt;br /&gt;
The most common programming languages are C-like: Java, C++, JavaScript, Perl(sort of). Not all grammars are the same; some are more powerful or have more variation than others. You will learn this if you take 3803.&lt;br /&gt;
&lt;br /&gt;
While it was more common in the past to parse user input with a formal grammar, we don’t really do this anymore because user input is done with a form so we don’t have to search for the information.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Using regular expressions to parse note contents with links&amp;lt;/h4&amp;gt;&lt;br /&gt;
Do not use regular expressions if you don’t have to, they are hard to make and are very buggy. You will probably not get the expected output as seen in the example below. It works by creating a finite automaton and evaluating the state. Regular expressions have very fast pattern matching.&lt;br /&gt;
&lt;br /&gt;
Incomplete regular expression class example: This example shows us how regular expressions work by trying to parse for links in our notes:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var s = “Example [http://www.google.ca link here]” + “and [http://students.carleton.ca here]&lt;br /&gt;
s.replace(/\[(.+) (.+)\]/g, ‘&amp;lt;a href=”$1”&amp;gt;$2&amp;lt;/a&amp;gt;;&lt;br /&gt;
&lt;br /&gt;
//s will now be“Example &amp;lt;a href=”http://www.google.ca link here] and [http://students.carleton.ca here]&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The string is not exactly what we want, it still has some whitespace and unnecessary text in the link, avoid using regular expressions but understand how Node uses regular expressions to parse URLs in the routing functions. If you use regular expressions expect yours to be wrong, it would be much simpler and less buggy to do it the following way.&lt;br /&gt;
&lt;br /&gt;
The easier way to parse the links would have been to manipulate the string to your desire using:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;s.indexOf(‘[‘)&amp;lt;/code&amp;gt; will return the index of the first open square bracket it sees.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;s.split(‘[‘)&amp;lt;/code&amp;gt; will cut the string into multiple strings at the points where it sees an open square bracket.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Debugging&amp;lt;/h3&amp;gt;&lt;br /&gt;
Code is compiled and checked for syntax before being run. Server side code problems gives error messages to the terminal, client side code problems need to be inspected directly from the browser.&lt;br /&gt;
&lt;br /&gt;
Server Side Code Problems Created in Index.js:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Random characters in your code causes a Reference Error: meaning your random characters don’t exist. It would, however, run if it was defined even if it doesn&#039;t do anything.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Missing a parenthesis gives a Syntax Error: meaning you violated the grammar of JavaScript.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Client Side Code Problems Created in notes.js:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Missing a curly brace caused part of the page to be missing. To investigate the problem we inspected the page and went to the Console to see an error message telling us we are missing a ; before statement on line X. The line that is given is usually the end of the program even though the error occurred somewhere else. A good way to debug missing braces is to use an editor that indents based off of braces and just indent lines until we find a discrepancy.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Assignment 8 hints&amp;lt;/h3&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 2: Hangs, redirection failed. Redirection happens on the server side.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 3: Error status 500 means a server side problem.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 4: Why is Not Logged In appearing? This is not normal input so it is probably encoded somewhere in the program. Find these strings, now look for the error.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 5:  Problem is client side because we made a new note and got a status 200 from the server. It is not rendering the interface for making a new note.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 6: JavaScript console means it is client side code, $ is used with jQuery.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 7: Client side not making the right requests&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 8: Client side&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Deploying web apps&amp;lt;/h3&amp;gt;&lt;br /&gt;
Deploying Web Apps in 3 steps:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Get a domain name by going to the right registrar – is it .ca. com? This is a database that contains databases. For example Carleton’s Computer Science department contains the following hierarchy .ca -&amp;gt; Carleton -&amp;gt; scs-&amp;gt;scs machines.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Get a DNS provider/server to maintain the database for your domain.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Get a hosting provider to have the machine that runs the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can get all these 3 services from one provider; this can be a problem if you don’t like your hosting provider since you’re locked into their services to use your domain. If you purchase the domain from somewhere separate from your hosting provider, it is easy to simply switch hosting providers for your domain. &lt;br /&gt;
&lt;br /&gt;
===In Lecture===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regular Expressions&lt;br /&gt;
&lt;br /&gt;
 - comes out of formal language theory, originally from linguistics, but adopted by computer scientists&lt;br /&gt;
&lt;br /&gt;
 - how do you represent the structure of natural languages?&lt;br /&gt;
   - how do you &amp;quot;parse&amp;quot; languages?&lt;br /&gt;
 - parse using rules&lt;br /&gt;
&lt;br /&gt;
 - in CS, a &amp;quot;grammar&amp;quot; is a set of rules specifying a &amp;quot;language&amp;quot;&lt;br /&gt;
   - all syntax, no meaning&lt;br /&gt;
   - is this a valid X, where X is...English?  more likely C or Java&lt;br /&gt;
 - simplest languages are things like LISP&lt;br /&gt;
   (blah (blah blah blah) )&lt;br /&gt;
 - most common: C-like syntax&lt;br /&gt;
   Java, C++, JavaScript, Perl (sort of)&lt;br /&gt;
&lt;br /&gt;
 - not all grammars are the same&lt;br /&gt;
 - some are more &amp;quot;powerful&amp;quot; than others&lt;br /&gt;
 - this is the stuff of 3803&lt;br /&gt;
&lt;br /&gt;
Deploying Web Apps&lt;br /&gt;
------------------&lt;br /&gt;
&lt;br /&gt;
* get a domain name (DNS)&lt;br /&gt;
  - go to the right registrar - is it .ca, .com, .sucks&lt;br /&gt;
* get a DNS provider/server&lt;br /&gt;
  - maintain DB for your domain&lt;br /&gt;
* get a hosting provider (machine to run web code)&lt;br /&gt;
&lt;br /&gt;
You can get all three from certain providers&lt;br /&gt;
&lt;br /&gt;
Don&#039;t do your own DNS server&lt;br /&gt;
you *can* do your own web server&lt;br /&gt;
How?&lt;br /&gt;
 - just run it on port 80&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_19&amp;diff=20017</id>
		<title>WebFund 2015W Lecture 19</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_19&amp;diff=20017"/>
		<updated>2015-03-19T17:53:34Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Video==&lt;br /&gt;
&lt;br /&gt;
The video for the lecture given on Wednesday, March 18, 2015 [http://homeostasis.scs.carleton.ca/~soma/webfund-2015w/lectures/comp2406-2015w-lec19-18Mar2015.mp4 is now available].&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Assignment due dates&amp;lt;/h3&amp;gt;&lt;br /&gt;
Assignments 5-7 can be submitted late up till Monday March 23 (with a small grade reduction).&lt;br /&gt;
&lt;br /&gt;
Assignment 8 has been  moved to Wednesday March 25.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Regular Expressions&amp;lt;/h3&amp;gt;&lt;br /&gt;
In order to understand what a regular expression really is, we need a bit of background information.&lt;br /&gt;
&lt;br /&gt;
The Chomsky Hierarchy defines the classes of formal grammars you can create. They are broken into different classifications based on how “powerful” the language is or how much variation it has. A formal grammar is a set of production rules for strings; the rules describe what syntax is valid for the language. A formal grammar has the following things:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;A finite set of nonterminal symbols indicating that some production rule can yet be applied.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;A finite set of terminal symbols indicating that no production rule can be applied.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;A start symbol.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&amp;lt;em&amp;gt;(Formal grammar definition referenced from Wikipedia.)&amp;lt;/em&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Hierarchy breaks the grammars down into the following 4 groups:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Type 3 - Regular Expression: Think of it as a a grammar that can be parsed by a little machine with finite states (finite automaton), it can’t remember a large quantity of things.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Type 2 - Context Free: They use a stack. This encompasses most programming languages.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Type 1 - Context Sensitive: What a computer can do with a fixed amount of space.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Type 0 - Recursively Enumerable: Anything a computer can do.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What is a finite automaton?&lt;br /&gt;
An example of a simple deterministic finite automaton would be a program with 3 states that can receive a 0 or 1. See the [http://en.wikipedia.org/wiki/Deterministic_finite_automaton#/media/File:DFA_example_multiplies_of_3.svg Wikipedia example].&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 0 and receive a 0, return to state 0.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 0 and receive a 1, go to state 1.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 1 and receive a 0, go to state 2.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 1 and receive a 1, go to state 0.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 2 and receive a 0, go to state 1.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;If we are on state 2 and receive a 1, return to state 2.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Wikipedia says it is a sequence of characters that forms a search pattern. As seen from the Chomsky Hierarchy, we also know that it is a formal grammar.&lt;br /&gt;
&lt;br /&gt;
Formal grammar was initially used in linguistics but adopted by Computer Science. To represent the structure of natural language you parse it using rules - the grammar. Similarly, in Computer Science, a grammar is a set of rules specifying a language, also known as syntax. The simplest languages are ones like LISP (blah(blah blah blah)). Contains lots of parenthesis, symbols, numbers, it is very easy to parse.&lt;br /&gt;
&lt;br /&gt;
The most common programming languages are C-like: Java, C++, JavaScript, Perl(sort of). Not all grammars are the same; some are more powerful or have more variation than others. You will learn this if you take 3803.&lt;br /&gt;
&lt;br /&gt;
While it was more common in the past to parse user input with a formal grammar, we don’t really do this anymore because user input is done with a form so we don’t have to search for the information.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Using regular expressions to parse note contents with links&amp;lt;/h4&amp;gt;&lt;br /&gt;
Do not use regular expressions if you don’t have to, they are hard to make and are very buggy. You will probably not get the expected output as seen in the example below. It works by creating a finite automaton and evaluating the state. Regular expressions have very fast pattern matching.&lt;br /&gt;
&lt;br /&gt;
Incomplete regular expression class example: This example shows us how regular expressions work by trying to parse for links in our notes:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
var s = “Example [http://www.google.ca link here]” + “and [http://students.carleton.ca here]&lt;br /&gt;
s.replace(/\[(.+) (.+)\]/g, ‘&amp;lt;a href=”$1”&amp;gt;$2&amp;lt;/a&amp;gt;;&lt;br /&gt;
&lt;br /&gt;
//s will now be“Example &amp;lt;a href=”http://www.google.ca link here] and [http://students.carleton.ca here]&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The string is not exactly what we want, it still has some whitespace and unnecessary text in the link, avoid using regular expressions but understand how Node uses regular expressions to parse URLs in the routing functions. If you use regular expressions expect yours to be wrong, it would be much simpler and less buggy to do it the following way.&lt;br /&gt;
&lt;br /&gt;
The easier way to parse the links would have been to manipulate the string to your desire using:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;s.indexOf(‘[‘)&amp;lt;/code&amp;gt; will return the index of the first open square bracket it sees.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;&amp;lt;code&amp;gt;s.split(‘[‘)&amp;lt;/code&amp;gt; will cut the string into multiple strings at the points where it sees an open square bracket.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Debugging&amp;lt;/h3&amp;gt;&lt;br /&gt;
Code is compiled and checked for syntax before being run. Server side code problems gives error messages to the terminal, client side code problems need to be inspected directly from the browser.&lt;br /&gt;
&lt;br /&gt;
Server Side Code Problems Created in Index.js:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Random characters in your code causes a Reference Error: meaning your random characters don’t exist. It would, however, run if it was defined even if it doesn&#039;t do anything.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Missing a parenthesis gives a Syntax Error: meaning you violated the grammar of JavaScript.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Client Side Code Problems Created in notes.js:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Missing a curly brace caused part of the page to be missing. To investigate the problem we inspected the page and went to the Console to see an error message telling us we are missing a ; before statement on line X. The line that is given is usually the end of the program even though the error occurred somewhere else. A good way to debug missing braces is to use an editor that indents based off of braces and just indent lines until we find a discrepancy.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Assignment 8 hints&amp;lt;/h3&amp;gt;&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 2: Hangs, redirection failed. Redirection happens on the server side.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 3: Error status 500 means a server side problem.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 4: Why is Not Logged In appearing? This is not normal input so it is probably encoded somewhere in the program. Find these strings, now look for the error.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 5:  Problem is client side because we made a new note and got a status 200 from the server. It is not rendering the interface for making a new note.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 6: JavaScript console means it is client side code, $ is used with jQuery.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 7: Client side not making the right requests&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Error 8: Client side&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Deploying web apps&amp;lt;/h3&amp;gt;&lt;br /&gt;
Deploying Web Apps in 3 steps:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Get a domain name by going to the right registrar – is it .ca. com? This is a database that contains databases. For example Carleton’s Computer Science department contains the following hierarchy .ca -&amp;gt; Carleton -&amp;gt; scs-&amp;gt;scs machines.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Get a DNS provider/server to maintain the database for your domain.&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Get a hosting provider to have the machine that runs the code.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can get all these 3 services from one provider; this can be a problem if you don’t like your hosting provider since you’re locked into their services to use your domain. If you purchase the domain from somewhere separate from your hosting provider, it is easy to simply switch hosting providers for your domain. &lt;br /&gt;
&lt;br /&gt;
===In Lecture===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Regular Expressions&lt;br /&gt;
&lt;br /&gt;
 - comes out of formal language theory, originally from linguistics, but adopted by computer scientists&lt;br /&gt;
&lt;br /&gt;
 - how do you represent the structure of natural languages?&lt;br /&gt;
   - how do you &amp;quot;parse&amp;quot; languages?&lt;br /&gt;
 - parse using rules&lt;br /&gt;
&lt;br /&gt;
 - in CS, a &amp;quot;grammar&amp;quot; is a set of rules specifying a &amp;quot;language&amp;quot;&lt;br /&gt;
   - all syntax, no meaning&lt;br /&gt;
   - is this a valid X, where X is...English?  more likely C or Java&lt;br /&gt;
 - simplest languages are things like LISP&lt;br /&gt;
   (blah (blah blah blah) )&lt;br /&gt;
 - most common: C-like syntax&lt;br /&gt;
   Java, C++, JavaScript, Perl (sort of)&lt;br /&gt;
&lt;br /&gt;
 - not all grammars are the same&lt;br /&gt;
 - some are more &amp;quot;powerful&amp;quot; than others&lt;br /&gt;
 - this is the stuff of 3803&lt;br /&gt;
&lt;br /&gt;
Deploying Web Apps&lt;br /&gt;
------------------&lt;br /&gt;
&lt;br /&gt;
* get a domain name (DNS)&lt;br /&gt;
  - go to the right registrar - is it .ca, .com, .sucks&lt;br /&gt;
* get a DNS provider/server&lt;br /&gt;
  - maintain DB for your domain&lt;br /&gt;
* get a hosting provider (machine to run web code)&lt;br /&gt;
&lt;br /&gt;
You can get all three from certain providers&lt;br /&gt;
&lt;br /&gt;
Don&#039;t do your own DNS server&lt;br /&gt;
you *can* do your own web server&lt;br /&gt;
How?&lt;br /&gt;
 - just run it on port 80&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_18&amp;diff=20015</id>
		<title>WebFund 2015W Lecture 18</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=WebFund_2015W_Lecture_18&amp;diff=20015"/>
		<updated>2015-03-18T00:17:17Z</updated>

		<summary type="html">&lt;p&gt;LeeCroft: Created page with &amp;quot;==Audio==   ==Notes==  &amp;lt;h3&amp;gt;Assignment 7&amp;lt;/h3&amp;gt;  &amp;lt;h4&amp;gt;Question 1&amp;lt;/h4&amp;gt; The delete button is implemented in the same way as the update button except that it will do something differ...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Audio==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Notes==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h3&amp;gt;Assignment 7&amp;lt;/h3&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Question 1&amp;lt;/h4&amp;gt;&lt;br /&gt;
The delete button is implemented in the same way as the update button except that it will do something different in the callback function provided for its click event. You must create this callback function and set it up to:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Accept a parameter containing the note&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Check with a dialogue box to confirm the user&#039;s choice to delete note&amp;lt;/li&amp;gt;&lt;br /&gt;
  &amp;lt;li&amp;gt;Make a POST request to &#039;/deleteNote&#039;&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You must also set up a route on the serve to handle this request. The body of the callback function for this route will be very similar to the previously used delete note route.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Question 2&amp;lt;/h4&amp;gt;&lt;br /&gt;
To easily format a string containing the contents of a note, you can use [https://regex101.com/#javascript JavaScript regular expressions]. One potential place to do this is on the server in the /updateNotes route.&lt;br /&gt;
&lt;br /&gt;
HTML elements can be escaped in exactly the same way as in the previous assignment.&lt;br /&gt;
&lt;br /&gt;
For a link with a label, use:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function makeLinkswLabel(str){&lt;br /&gt;
   return str.replace(/\[(\S+)\s+([^\]]+)\]/gi,&#039;&amp;lt;a href=\&amp;quot;$1\&amp;quot;&amp;gt;$2&amp;lt;/a&amp;gt;&#039;&lt;br /&gt;
);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For a link without a label, use:&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
function makeLinkswoLabel(str){&lt;br /&gt;
   return str.replace(/\[(\S+)\]/gi,&#039;&amp;lt;a href=\&amp;quot;$l\&amp;quot;&amp;gt;$1&amp;lt;/a&amp;gt;&#039;);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;h4&amp;gt;Question 3&amp;lt;/h4&amp;gt;&lt;br /&gt;
This will involve making a post request to a route /newName. The rest is left up to you to figure out...&lt;/div&gt;</summary>
		<author><name>LeeCroft</name></author>
	</entry>
</feed>