The purpose of this repository is to be a practical enviornment for training.
Make sure you have:
- Github account
- Set up git on your box. Follow all the instructions. Make sure to set up a private key with github as well (tip: avoid a password, you will get crazy from all the pulls and pushes).
- Install node.js.
- Alias
npm
for git bash:- Create
~/.bash_profile
file if you don't already have one (from the git bash shell) - Add the line:
alias npm='node /c/Program\ Files\ \(x86\)/nodejs/node_modules/npm/bin/npm-cli.js'
- Create
- Add
C:\Program Files (x86)\nodejs
to your system PATH (first one please). - Watch this lecture about javascript.
- Go over the javascript guide.
- Watch this lecture about nodejs.
- Set up the editor/IDE of your choise with tabs = 2 spaces.
This is a short tutorial to get you started on the essentials of node and github.
Fire up "git bash" (from the Start menu...).
mkdir /c/projects
mkdir /c/projects/nodejs
mkdir /c/projects/nodejs/training
cd /c/projects/nodejs/training
git clone git@github.com:mousaz/training-repo.git .
Please let the name of branch reflect your ownership for it along with its content. For example, in this case it can be named 'lastname-firstapp'
git checkout -b <your_branch>
Please notice that throughtout this tutorial we'll call this directory 'your_dir', so when you start applying don't forget to replace it with your actual name. This is important to keep things clean and not to miss up others work.
mkdir your_dir
cd your_dir
This directory will hold the installation of the modules that you'll need during development.
mkdir node_modules
mkdir firstapp
cd firstapp
var http = require('http');
http.createServer(function(req, res) {
console.log('new request from ' + req.url);
console.warn('this is a warning');
console.error('this is an error');
res.end('This is my first application');
}).listen(process.env.PORT || 5000);
console.log('info: myapp started at', new Date());
node index.js
Browse to http://localhost:5000 and see the result.
Press CTRL+C to kill it.
git add index.js # stage index.js for commit
git commit -m 'my first node app' # commit to local repository
git push origin <your_branch> # push your_branch to origin (github)
In this turotial we will use nodeunit to create our test app.
- Create a test app for your
firstapp
app:
mkdir your_dir/test
cd your_dir/test
- Create
tests.js
for the tests:
var request = require('request');
exports.mytest = function(test) {
var target = 'localhost:5000'; // to run locally
request('http://' + target, function(err, res, body) {
test.equals(res.statusCode, 200, "expecting 200 OK");
test.done();
});
};
Run your tests locally using nodeunit
:
- Install
nodeunit
in the global scope using npm:npm install -g nodeunit
(this addsnodeunit
to the PATH). - Open a git bash window, navigate to
your_dir
and start your app:
$ cd your_dir/firstapp
$ node index.js
info: myapp started at Sun, 15 Jan 2012 11:18:43 GMT
- Open another git bash window, navigate to
your_dir/firstapp/test
and run the tests usingnodeunit
:
$ cd your_dir/firstapp/test
$ nodeunit tests.js
tests.js
new request from /
this is a warning
this is an error
✔ mytest
- Now you are ready to push the code to origin:
git add test/tests.js # assuming you are under firstapp directory
git commit -m 'adding test app for firstapp'
git push origin <your-branch>
- http://nodejs.org - home page of node.js
- http://zguide.zeromq.org/js:_start - node.js samples
- http://nodejs.org/api/ - reference to node.js apis
- http://search.npmjs.org - node package manager registry
- http://help.github.com/git-cheat-sheets - git cheatsheets
- http://git-scm.com/book - Pro Git Book
- http://www-cs-students.stanford.edu/~blynn/gitmagic/index.html - More on Git.