WebFund 2014W Lecture 9: Difference between revisions

From Soma-notes
Created page with "The video from the lecture given on February 5, 2014 is available: * [http://www.screencast.com/t/W9rewtusggz Small from screencast.com] * [http://www.screencast.com/t/cJmYPtR..."
 
Gkadri (talk | contribs)
No edit summary
Line 3: Line 3:
* [http://www.screencast.com/t/cJmYPtR6 Large from screencast.com]
* [http://www.screencast.com/t/cJmYPtR6 Large from screencast.com]
* [http://dl.cuol.ca/capture/Anil.Somayaji/COMP_2406_2014W_Lecture_9_-_20140205_143924_27.mp4 Original from CUOL]
* [http://dl.cuol.ca/capture/Anil.Somayaji/COMP_2406_2014W_Lecture_9_-_20140205_143924_27.mp4 Original from CUOL]
== Lecture Notes ==
In this lecture the adventure-demo app of assignment 2 is explained.
=== storeRooms.js ===
storeRooms.js connects to the database and creates the collection called Rooms. If the collection tables already exist, then it drops it. MongoDb stores the last room the player was at before leaving the game. Even if the server was killed and was started up again, it still remembers the last room of each player.
It is important to note that <code>db.close();</code> at the end of storeRooms.js terminates the node process. Node is designed to wait for the input/output connection to send an event. We need to include this line in App.js otherwise Node is going to be waiting for something to happen after the code gets executed as long as there is an open socket.
=== App.js ===
The variable <code>mongoStore</code> declared at:
> var MongoStore = require('connect-mongo')(express);
was used at:
> app.use(express.session({
> cookie: {maxAge: 60000 * 20} // 20 minutes
> , secret: "Shh... I'm a secret"
> , store: new MongoStore({db: "adventure-demo"})
> }));
The piece of code above provides session support and it is there to have cookies used when handling requests from the browser. Without sessions, there is no other way for the web app to know if the player is logged in or not.
The following line specifies how long the cookies last:
> cookie: {maxAge: 60000 * 20} // 20 minutes
This following line :
> store: new MongoStore({db: "adventure-demo"})
is saying do not store the session in memory otherwise all sessions will be lost if the server was restarted. It is saying store the session in an instance of mongostore and the database to use is “adventure–demo”.
It is important to note that, in app.js, the web server is being created and run first and then the rooms are getting added.
=== Index.js ===
> playersCollection.find({playername: playername}).toArray(addPlayer);
finds the player collection having the player name similar to the content of the variable <code>playername</code>.
The <code>PlayerCollection.find({playername: playername})</code> part returns a cursor object and the <code>toArray(addPlayer)</code> part turns it into an array.
While static pages that are being served are all in the public library, Jade templates are server side dynamic web pages.
=== Home.js ===
The code in this file runs on the browser so it is a client-side code.
=== Other Important Stuff: ===
• All of the available databases can be shown using the <code>show dbs</code> mongo shell command (located in our mongodb installation directory and can be started by typing <code>./bin/mongo</code>).
• Collections of this database can be shown by typing in <code>use adventure-demo</code> then <code>show collections</code>.
• <code>db.sessions.find()</code> shows the active sessions in the database.
• <code>db.players.drop()</code> deletes the players collection.
• Note that when we try to access a collection that does not exist, Mongodb creates it.
• Mongodb is not a part of node. It is a completely separate executable. Mongodb works well with node particularly because it uses JavaScript internally.
• Next () is provided by the “connect” middleware and refers to the next middleware in the express stack.
[http://stackoverflow.com/questions/8710669/having-a-hard-time-trying-to-understand-next-next-in-express-js Having a hard time trying to understand 'next/next()' in express.js?]
• Callbacks (as explained in an article posted on [http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/ javascriptissexy.com]):
In JavaScript, functions are first-class objects, which means that they can be “stored in variables, passed as arguments to functions, created within functions, and returned from functions”
A callback function is a function that is passed to another function (let’s call this other function “otherFunction”) as a parameter, and the callback function is called (executed) inside otherFunction.
When we pass a callback function as an argument to another function, we are only passing the function definition. We are not executing the function in the parameter. We aren’t passing the function with the trailing pair of executing parenthesis () like we do when we are executing a function. And since the containing function has the callback function in its parameter as a function definition, it can execute the callback anytime.
It is important to note that the callback function is not executed immediately. It is “called back” (hence the name) at some specified point inside the containing function’s body.

Revision as of 01:52, 2 March 2014

The video from the lecture given on February 5, 2014 is available:


Lecture Notes

In this lecture the adventure-demo app of assignment 2 is explained.

storeRooms.js

storeRooms.js connects to the database and creates the collection called Rooms. If the collection tables already exist, then it drops it. MongoDb stores the last room the player was at before leaving the game. Even if the server was killed and was started up again, it still remembers the last room of each player.

It is important to note that db.close(); at the end of storeRooms.js terminates the node process. Node is designed to wait for the input/output connection to send an event. We need to include this line in App.js otherwise Node is going to be waiting for something to happen after the code gets executed as long as there is an open socket.

App.js

The variable mongoStore declared at:

> var MongoStore = require('connect-mongo')(express);

was used at:

> app.use(express.session({
> cookie: {maxAge: 60000 * 20} // 20 minutes
> , secret: "Shh... I'm a secret"
> , store: new MongoStore({db: "adventure-demo"})
> }));

The piece of code above provides session support and it is there to have cookies used when handling requests from the browser. Without sessions, there is no other way for the web app to know if the player is logged in or not.

The following line specifies how long the cookies last:

> cookie: {maxAge: 60000 * 20} // 20 minutes

This following line :

> store: new MongoStore({db: "adventure-demo"})

is saying do not store the session in memory otherwise all sessions will be lost if the server was restarted. It is saying store the session in an instance of mongostore and the database to use is “adventure–demo”.

It is important to note that, in app.js, the web server is being created and run first and then the rooms are getting added.

Index.js

> playersCollection.find({playername: playername}).toArray(addPlayer);

finds the player collection having the player name similar to the content of the variable playername.

The PlayerCollection.find({playername: playername}) part returns a cursor object and the toArray(addPlayer) part turns it into an array.

While static pages that are being served are all in the public library, Jade templates are server side dynamic web pages.


Home.js

The code in this file runs on the browser so it is a client-side code.

Other Important Stuff:

• All of the available databases can be shown using the show dbs mongo shell command (located in our mongodb installation directory and can be started by typing ./bin/mongo).

• Collections of this database can be shown by typing in use adventure-demo then show collections.

db.sessions.find() shows the active sessions in the database.

db.players.drop() deletes the players collection.

• Note that when we try to access a collection that does not exist, Mongodb creates it.

• Mongodb is not a part of node. It is a completely separate executable. Mongodb works well with node particularly because it uses JavaScript internally. • Next () is provided by the “connect” middleware and refers to the next middleware in the express stack.

Having a hard time trying to understand 'next/next()' in express.js?

• Callbacks (as explained in an article posted on javascriptissexy.com):

In JavaScript, functions are first-class objects, which means that they can be “stored in variables, passed as arguments to functions, created within functions, and returned from functions”

A callback function is a function that is passed to another function (let’s call this other function “otherFunction”) as a parameter, and the callback function is called (executed) inside otherFunction.

When we pass a callback function as an argument to another function, we are only passing the function definition. We are not executing the function in the parameter. We aren’t passing the function with the trailing pair of executing parenthesis () like we do when we are executing a function. And since the containing function has the callback function in its parameter as a function definition, it can execute the callback anytime.

It is important to note that the callback function is not executed immediately. It is “called back” (hence the name) at some specified point inside the containing function’s body.