WebFund 2014W Lecture 9

From Soma-notes
Revision as of 21:57, 1 March 2014 by Gkadri (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 table already exists, 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 it runs 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 all static pages that are being served are 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 use adventure-demo then show collections in mongo shell.

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.