WebFund 2016W: Tutorial 1: Difference between revisions
No edit summary |
|||
(11 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
(See the end of this page for instructions on how to get started in node and tips on what to do if you have extra time.) | |||
In this tutorial you will be studying and extending two implementations of simplegrep, included below. grep is a standard UNIX utility for returning lines from a text file that match a given pattern. | |||
First, with the sync version, try the following: | |||
* Make the search pattern into a [https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions regular expression]. | |||
* Give it an additional argument that causes matched lines to be output to a new file. You'll need to use writeFileSync() from [https://nodejs.org/api/fs.html the Node fs module]. When finished, output "All done!" to the console. | |||
* Make the output lines [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort sorted] in alphabetical order. | |||
Then, do all of the above with the async version. Make sure to use writeFile(), not writeFileSync()! | |||
You may find the following methods useful when working with arrays:[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()], [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift unshift()], and [https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join join()]. | |||
==Code== | ==Code== | ||
===simplegrep_sync.js=== | ===simplegrep_sync.js=== | ||
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/simplegrep_sync.js Downloadable version]. | |||
<source line lang="javascript"> | <source line lang="javascript"> | ||
Line 11: | Line 23: | ||
if (process.argv.length < 4) { | if (process.argv.length < 4) { | ||
console.error('Not enough parameters given. Try this: ' + | console.error('Not enough parameters given. Try this: ' + | ||
'"node | '"node simplegrep_sync term filename.txt"'); | ||
process.exit(1); | process.exit(1); | ||
} | } | ||
Line 29: | Line 41: | ||
===simplegrep_async.js=== | ===simplegrep_async.js=== | ||
[http://homeostasis.scs.carleton.ca/~soma/webfund-2016w/code/simplegrep_async.js Downloadable version]. | |||
<source line lang="javascript"> | <source line lang="javascript"> | ||
Line 35: | Line 49: | ||
if (process.argv.length < 4) { | if (process.argv.length < 4) { | ||
console.error('Not enough parameters given. Try this: ' + | console.error('Not enough parameters given. Try this: ' + | ||
'"node | '"node simplegrep_async term filename.txt"'); | ||
process.exit(1); | process.exit(1); | ||
} | } | ||
Line 55: | Line 69: | ||
fs.readFile(filename, 'utf-8', returnMatches); | fs.readFile(filename, 'utf-8', returnMatches); | ||
</source> | </source> | ||
==Getting Started with Node.js== | |||
For this class you will be using [http://nodejs.org/ node.js] and a web broswer. While these are both cross-platform, we can provide the most support for running these applications inside the class [http://www.lubuntu.net/ Lubuntu] virtual machine appliance. We will be using [https://www.virtualbox.org/ VirtualBox] as our preferred virtualization platform; however, VMware Workstation/Fusion and other virtualization platforms should be able to run the appliance as well. | |||
In the labs node.js is also installed on the lab machines under Programs->Programming->node.js. From the node.js command prompt you can run node programs in a fashion similar to a Linux command prompt; however, expect there to occasionally be bugs that are not present in the Lubuntu environment. Consider yourself warned! | |||
==Running the VM== | |||
In the SCS labs you should be able to run the VM by starting Virtualbox (listed in the Applications menu) and selecting the COMP 2406 virtual machine. After the VM has fully booted up you should be logged in automatically as the user "student". If the screen locks or you need administrative access (via sudo) you'll need the password for the student account, however. This password is "tneduts!" (students backwards followed by an !). There is also an admin account in case your student account gets corrupted for any reason. The password for it is "nimda!". | |||
We highly recommend running your VM in full-screen mode. (Don't maximize the window; instead select full screen from the view menu.) Do all of your work inside of the VM; it should be fast enough and you won't have any issues with sharing files or with host firewalls. | |||
If you want to run the VM on your own machine, see the section on configuring your own VM [http://homeostasis.scs.carleton.ca/wiki/index.php/Fundamentals_of_Web_Applications_%28Winter_2016%29#Running_the_VM_on_your_own_machineson the main course webpage]. | |||
Note as we will explain, you will have the ability to easily save the work you do from any VM to your SCS account and restore it to any other copy of the class VM. Thus feel free to play around with VMs; if you break anything, you can always revert. Remember though that in the labs you '''must''' save and restore your work, as all of your changes to the VM will be lost when you logout! | |||
While you may update the software in the VM, those updates will be lost when you next login to the lab machines; thus, you probably only want to update a VM installed on your own system. | |||
==Hello, World!== | |||
To create your first node application, start [http://www.geany.org/ geany], [http://brackets.io/ brackets], [http://www.vim.org/ vim], or [http://www.gnu.org/software/emacs/ emacs] code editors by clicking on their quick launch icons at the bottom left of the screen (beside the LXDE start menu button). | |||
(If you are a fan of vi but want to try emacs, you should type Alt-X [http://www.gnu.org/software/emacs/manual/html_mono/viper.html viper-mode]. You're welcome.) | |||
In your editor of choice, create a file <tt>hello.js</tt> in your Documents folder with the following contents: | |||
console.log("Hello World!"); | |||
You can now run this file by opening an LXTerminal (under Accessories) and typing: | |||
cd Documents | |||
node hello.js | |||
And you should see <tt>Hello, World!</tt> output to your terminal. | |||
You can also run node interactively by simply running <tt>node</tt> with no arguments. You'll then get a prompt where you can enter any code that you like and see what it does. To exit this environment, type Control-D. | |||
Note that when run interactively, we say that node is running a [http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop read-eval-print loop] (REPL). It reads input, evaluates it, and then prints the results. This structure is very old in computer science, going back to the first LISP interpreters from the early 1960's. | |||
==Saving your work== | |||
You can save your work to your SCS account by running (where it says <SCS username> it should just be your username no <>brackets) | |||
save2406 <SCS username> | |||
This will rsync /home/student to the COMP2406 directory in your SCS account by connecting to access.scs.carleton.ca. | |||
When you wish to restore your student account, run | |||
restore2406 <SCS username> | |||
Note that both of these commands are destructive - they will wipe out all the files in the COMP2406 folder on SCS or /home/student in your VM. If you want to see what the differences are between the two versions, run | |||
compare2406 <SCS username> | |||
=== ASIDE: If you are unsure of your account information (Please read this) === | |||
The SCS account would be your access.scs.carleton.ca account (which is your unix/linux account) if you do not have one or do not remember your password, please go to the main scs page [http://www.scs.carleton.ca here], click on Tech Support (top right corner), then click on accounts, Linux accounts and click the link [http://www.scs.carleton.ca/webacct/ http://www.scs.carleton.ca/webacct/], read the policy, accept, and proceed to retrieve your account information / setup your account. You will then use this password for all future save and restore operations!!) | |||
==CodeAcademy== | |||
Now that you've got your virtual machine running, it is time to start learning about web technologies. If you haven't already, you should either go through or make sure you know the material in all of the following CodeAcademy modules: | |||
* [http://www.codecademy.com/tracks/web Web Fundamentals] | |||
* [http://www.codecademy.com/tracks/javascript/ Javascript] | |||
* [http://www.codecademy.com/tracks/jquery jQuery] | |||
* [http://www.codecademy.com/tracks/projects Web Projects] (just the basic projects) | |||
Feel free to skip around; these should be very simple for you, at least at the beginning. Try to do the last parts of each lesson to see if need to bother going through it. | |||
While you will not be directly tested on this material, this course is MUCH easier if you've had some experience with web technologies before tackling node. |
Latest revision as of 20:18, 13 January 2016
(See the end of this page for instructions on how to get started in node and tips on what to do if you have extra time.)
In this tutorial you will be studying and extending two implementations of simplegrep, included below. grep is a standard UNIX utility for returning lines from a text file that match a given pattern.
First, with the sync version, try the following:
- Make the search pattern into a regular expression.
- Give it an additional argument that causes matched lines to be output to a new file. You'll need to use writeFileSync() from the Node fs module. When finished, output "All done!" to the console.
- Make the output lines sorted in alphabetical order.
Then, do all of the above with the async version. Make sure to use writeFile(), not writeFileSync()!
You may find the following methods useful when working with arrays:push(), pop(), shift(), unshift(), and join().
Code
simplegrep_sync.js
var fs = require('fs');
if (process.argv.length < 4) {
console.error('Not enough parameters given. Try this: ' +
'"node simplegrep_sync term filename.txt"');
process.exit(1);
}
var searchterm = process.argv[2];
var filename = process.argv[3];
var rawContents = fs.readFileSync(filename, 'utf-8');
var lines = rawContents.split('\n');
for (i = 0; i < lines.length; i++) {
if (lines[i].indexOf(searchterm) > -1) {
console.log(lines[i]);
}
}
simplegrep_async.js
var fs = require('fs');
if (process.argv.length < 4) {
console.error('Not enough parameters given. Try this: ' +
'"node simplegrep_async term filename.txt"');
process.exit(1);
}
var searchterm = process.argv[2];
var filename = process.argv[3];
var returnMatches = function(err, rawContents) {
var lines = rawContents.split('\n');
lines.forEach(function(theLine) {
if (theLine.indexOf(searchterm) > -1) {
console.log(theLine);
}
});
}
fs.readFile(filename, 'utf-8', returnMatches);
Getting Started with Node.js
For this class you will be using node.js and a web broswer. While these are both cross-platform, we can provide the most support for running these applications inside the class Lubuntu virtual machine appliance. We will be using VirtualBox as our preferred virtualization platform; however, VMware Workstation/Fusion and other virtualization platforms should be able to run the appliance as well.
In the labs node.js is also installed on the lab machines under Programs->Programming->node.js. From the node.js command prompt you can run node programs in a fashion similar to a Linux command prompt; however, expect there to occasionally be bugs that are not present in the Lubuntu environment. Consider yourself warned!
Running the VM
In the SCS labs you should be able to run the VM by starting Virtualbox (listed in the Applications menu) and selecting the COMP 2406 virtual machine. After the VM has fully booted up you should be logged in automatically as the user "student". If the screen locks or you need administrative access (via sudo) you'll need the password for the student account, however. This password is "tneduts!" (students backwards followed by an !). There is also an admin account in case your student account gets corrupted for any reason. The password for it is "nimda!".
We highly recommend running your VM in full-screen mode. (Don't maximize the window; instead select full screen from the view menu.) Do all of your work inside of the VM; it should be fast enough and you won't have any issues with sharing files or with host firewalls.
If you want to run the VM on your own machine, see the section on configuring your own VM the main course webpage.
Note as we will explain, you will have the ability to easily save the work you do from any VM to your SCS account and restore it to any other copy of the class VM. Thus feel free to play around with VMs; if you break anything, you can always revert. Remember though that in the labs you must save and restore your work, as all of your changes to the VM will be lost when you logout!
While you may update the software in the VM, those updates will be lost when you next login to the lab machines; thus, you probably only want to update a VM installed on your own system.
Hello, World!
To create your first node application, start geany, brackets, vim, or emacs code editors by clicking on their quick launch icons at the bottom left of the screen (beside the LXDE start menu button).
(If you are a fan of vi but want to try emacs, you should type Alt-X viper-mode. You're welcome.)
In your editor of choice, create a file hello.js in your Documents folder with the following contents:
console.log("Hello World!");
You can now run this file by opening an LXTerminal (under Accessories) and typing:
cd Documents node hello.js
And you should see Hello, World! output to your terminal.
You can also run node interactively by simply running node with no arguments. You'll then get a prompt where you can enter any code that you like and see what it does. To exit this environment, type Control-D.
Note that when run interactively, we say that node is running a read-eval-print loop (REPL). It reads input, evaluates it, and then prints the results. This structure is very old in computer science, going back to the first LISP interpreters from the early 1960's.
Saving your work
You can save your work to your SCS account by running (where it says <SCS username> it should just be your username no <>brackets)
save2406 <SCS username>
This will rsync /home/student to the COMP2406 directory in your SCS account by connecting to access.scs.carleton.ca.
When you wish to restore your student account, run
restore2406 <SCS username>
Note that both of these commands are destructive - they will wipe out all the files in the COMP2406 folder on SCS or /home/student in your VM. If you want to see what the differences are between the two versions, run
compare2406 <SCS username>
ASIDE: If you are unsure of your account information (Please read this)
The SCS account would be your access.scs.carleton.ca account (which is your unix/linux account) if you do not have one or do not remember your password, please go to the main scs page here, click on Tech Support (top right corner), then click on accounts, Linux accounts and click the link http://www.scs.carleton.ca/webacct/, read the policy, accept, and proceed to retrieve your account information / setup your account. You will then use this password for all future save and restore operations!!)
CodeAcademy
Now that you've got your virtual machine running, it is time to start learning about web technologies. If you haven't already, you should either go through or make sure you know the material in all of the following CodeAcademy modules:
- Web Fundamentals
- Javascript
- jQuery
- Web Projects (just the basic projects)
Feel free to skip around; these should be very simple for you, at least at the beginning. Try to do the last parts of each lesson to see if need to bother going through it.
While you will not be directly tested on this material, this course is MUCH easier if you've had some experience with web technologies before tackling node.