Homework 0

NETS 212: Scalable & Cloud Computing
Fall 2014
Assignment 0: Software Setup and Initial Node.js
Due September 9, 2014, at 10:00pm EDT
Goals
This assignment will help you set up your working environment for NETS 212, and to “get your feet wet”
with JavaScript and node.js.
We offer a standardized development system for NETS 212 that is based on a virtual machine image. This
image contains a small Linux installation, the Eclipse IDE, and all the tools you will need for the
homework assignments. Having such a standardized system has several advantages:
1. You do not have to worry about incompatibilities between the software we will be using and
other software that may be installed on your machine;
2. If something goes wrong, you have a 'safe' configuration to go back to - just reinstall the virtual
machine image;
3. The virtual machine image will be the 'gold standard' for grading; thus, if your solution works in
the VM image, you can be sure that it will also work on the graders' machines.
The virtual machine image works with VirtualBox. We suggest you install it on your own machine. It is
also available on the Linux machines in M100A, M204 (Ketterer Lab), M207, and Towne-M70.
Of course, you are free to use a different development system (e.g., a different operating system, your
favorite editor or IDE, ...). However, we will only grade solutions based on their performance in the
virtual machine image, and we can only offer limited support if you decide to use your own setup.
Installing the VM Image
Your first task is to install the VM image for this assignment. The following step-by-step guide assumes
that you are using a Mac OS X, Windows (7+), or Linux (recent Ubuntu) machine.
1. Download VirtualBox 4.3.12 from https://www.virtualbox.org/wiki/Download_Old_Builds_4_3
(avoid the newest version, 4.3.14, which we have found to NOT work on certain
configurations).
2. Download the VM image from the link provided on the Web at
www.cis.upenn.edu/~nets212/assignments.html. Please follow the instructions there carefully.
3. Double-click on the nets212.ova file you downloaded. This should trigger VirtualBox.
Leave the settings as they are and choose Import.
4. Choose ubuntu-14.04-desktop-i386.vbox and click on the
button in the toolbar.
5. The Linux in the VM image should boot now and automatically log in as the user “nets212.”
6. Whenever a password is requested, “nets212” should work.
You should now have a running Virtual Machine with Ubuntu 14.04.
Setting up a Shared Folder
The next step is to ensure that you can share files between your “base” operating system and your virtual
machine.
1. Go into the Devices menu and select Shared Folders Settings…. Click on the little folder with a
plus icon:
and select your home or Documents directory as the Folder Path. Then put “shared” as the
Folder Name and check Auto-mount and Make Permanent.
2. Your folder will now be found at the path /media/sf_shared.
You can create additional shared folders as you like. If you make them Auto-mount they will show up
under /media/sf_{whatever}.
Care and Feeding of Your Virtual Machine
A few tips regarding the virtual machine:
• Always shut down the VM properly before closing VirtualBox (by clicking on the power
switch icon in the upper right corner and selecting 'Shut down...').
If you do not do this, the VM image can become corrupted, just like your operating system
can become corrupted if you switch off your computer before shutting it down properly.
• If the screen size of your VM is tiny, try resizing it or putting it into full-screen mode by hitting
right-Control-key F. This is a toggle; right-Control-key F will make it
windowed again, e.g., if you want to see the VirtualBox menus.
• The data you store inside the VM is persistent (i.e., will survive reboots), and we will be using a
version control system. Nevertheless, we strongly recommend that you make occasional
backups, e.g., by copying your data files to a place in your /media/sf_shared folder.
• You can edit text files with Eclipse, gedit, or nano. To install another editor, you must use apt-get
to install it. For instance, to install vim, run sudo apt-get install vim in a terminal
window.
•
If the VM runs slowly on your machine, try increasing its memory size to 1.5GB (under Virtual
Machine / Virtual Machine Settings..., or under "Edit virtual machine settings").
Getting Your Feet Wet with Subversion & Node.js [10 points]
Once your environment is set up, you’ll need to get familiar with subversion (version control) and Node.js
(which lets you write server-side programs in Javascript). We assume most of you don’t yet have
familiarity with Javascript, but that’s OK, you’ll pick it up quickly!
Create Your Own Subversion Repository on Eniac
For your own code, you’ll want to make sure you have version control set up. A version control system
lets you “check out” code and “commit” different versions. It saves the changes in each successive
revision. You can “revert” to old versions (important if you go down a bad path!), compute diffs across
revisions, and share code. Today, the most popular version control systems are git and subversion (svn),
with Mercurial another fairly popular choice. Subversion is easier to manage globally, so we will use it.

Log into eniac.seas.upenn.edu via your favorite ssh client (Mac OS/Linux, open Terminal and
run ssh; Windows, use Cygwin or putty), then type in:
svnadmin create –-pre-1.4-compatible nets212

Type in pwd and note what it says is your home directory path. It should be “/home1” followed
by the first letter in your userid, followed by your userid. For example, I get
“/home1/z/zives.”
Then you can log out for now.
Sample Code Checkout
Start by clicking on the Eclipse icon on the left side of the screen. Then:
 Right-click in the Package Explorer or Project Explorer (whichever you see) and choose
Import…
 Select SVN | Checkout Projects from SVN (this is probably already highlighted)
 Choose Use existing repository location: and highlight the
svn+ssh://eniac.seas.upenn.edu/home1/n/nets212/nets212 entry. Hit Next.
 Enter your password to unlock the private key (nets212).
 Choose sample, then Next.
 Change the Project Name to HW0. Click Finish.
Migrating to Your Own Repository
Next we want to switch the code to your own Subversion repository:
 Now right-click on HW0. Choose Team | Disconnect… Select Also delete the SVN meta
information and hit Yes.
 Right-click HW0. Choose Team | Share Project… and choose SVN. Click Next.
 Choose Create a new repository location and enter the URL to your own subversion repository
on eniac:
svn+ssh://eniac.seas.upenn.edu/{path from subversion repo step
above}/nets212
 Hit Next, then be sure to choose Use project name as folder name.
 Eventually you’ll be prompted to Confirm Open Perspective with the Synchronize View. You
can say No.
Now as you make changes to your code, remember to periodically (ideally, at stable points) right-click on
the project, and choose Team | Commit. You’ll need to enter a description (for future reference) of what
you are committing. This will commit a “checkpoint” that you can always go back to.
Your First Node.js Program
This first assignment is quite straightforward, but nonetheless introduces some concepts. Javascript has a
fairly Java-like syntax. However, it is a dynamically typed language, so variables don’t have strict
types. A second difference is that most Node.js functions require you to pass them function closures that
are called upon success or failure. You’ll see this below.
First, enable the Node perspective: click on Window | Open Perspective | Other... | Node.
Next, find app.js, open it, and look it over. It’s only a few lines, and you should be able to get the gist of
what’s going on. We are using Express, a library for managing the Web browser (technically, hypertext
transfer) protocol HTTP. You’ll see in the code that a variable app is being defined, and its value
becomes the results of initializing an express object.
Next, we call app.get to associate a particular path (‘/’) with a callback function (closure). That
function is called with two objects – the request information, req, and an object for writing a response,
res. Obviously we are sending a response.
Finally, we create a server that listens on a particular port (3000) for a Web browser request.
Right-click on app.js, choose Run As|Node Application. You should see “Listening on port
3000.” Open Firefox and type in to the address bar: “localhost:3000.” You should see “Hello
World.”
If you prefer using the command line, you can achieve the same result by opening a terminal window,
changing into the HW0 directory (cd ~/workspace/HW0) and then running your application from
there (nodejs app.js).
Click on the red stop button in the Eclipse Console window to stop the server. In general, you’ll need to be
careful to stop the server each time – if you try running it a second time, you’ll get an error about the port
being in use.
Your Task for Submission: Customizing the Node Program
Now you should make the following changes to your program:
1. Replace “Hello World” with “Hello World, from ” followed by your SEAS login ID.
Be very careful to follow this precise pattern! E.g., for me it says:
Hello World, from zives
2. Change the server port from 3000 to 8080.
3. Add another call to app.get() that takes a string after the slash, and prints “Hello ” followed by
that string. Here’s some example code that does something similar for paths beginning with
/mypath/ followed by a string. With just a little thought you should be able to tweak to your
needs.
app.get(’/mypath/:something’), function(req, res) {
var parameter = req.params.something;
// parameter now has the value of the string in the path
}
);
Note that with this very basic knowledge, you can now create an infinite number of dynamic Web pages.
If you have a little extra time (this was an easy assignment, right?), you might try the following to build
up your knowledge of Javascript and Node.js to help with the rest of the semester:




http://www.cs.bham.ac.uk/~pxc/web/infoweb/IW-JavaScript.html
http://www.ibm.com/developerworks/library/j-javadev2-18/
http://eloquentjavascript.net/
http://www.tutorialspoint.com/javascript/javascript_builtin_functions.htm


http://nodeguide.com/beginner.html
http://javascriptissexy.com/learn-node-js-completely-and-with-confidence/
Testing and Committing Your Program
4. Launch your program, then test it using Firefox. Try at least the following locations:
a. localhost:8080
b. localhost:8080/tester
c. localhost:8080/Who’s there?
Finally, check all your changes into your new repository. To do this, right-click on the "HW0" project in
the Package Explorer, select "Team..." and then "Commit". You should be prompted for a comment. Enter
a short description of your changes (Example: "Implemented Hello World") and then hit "OK". Your
changes should now be checked in.
For later assignments, we strongly recommend that you regularly check in your changes, at least once a
day and/or whenevery you've implemented a major new feature. That way, when something suddenly
stops working, you can use svn's diff feature to find the changes you've made since the last checkin.
Also, when you create additional files, please don't forget to add them to the repository.
Submitting Your Solution
Once you are ready to submit, you should first check in all your changes. After that, you should use
Firefox from your Virtual Machine, login into Canvas and submit your app.js program (it should be in
workspace/HW0).