This is the second post in a small series on Node.js. In this post, I will take a look at how to get started and run the ubiquitous “Hello world” sample.
First, create a directory somewhere and Git clone Node… I did it like this:
1 2 3 4 |
cd ~ mkdir src cd src git clone git://github.com/ry/node.git |
Then, build and install Node like this:
1 2 3 |
./configure make make install |
If you get permission errors during the last step, you might need to do a
1 |
sudo chown -R $USER /usr/local |
to grant yourself ownership of everything beneath /usr/local. Note however, that this should probably only be done if the machine is your own personal machine.
Now, try typing node -v in the terminal… I got this:
1 |
v0.3.2-pre |
Now, let’s finish this post by creating a Node app like so (using TextMate or whatever you prefer):
1 2 3 4 5 |
cd ~ mkdir Projects mkdir Projects/HelloWorld cd Projects/HelloWorld mate app.js |
Punch in the following few lines:
1 2 3 4 5 6 7 8 |
var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/'); |
and go the terminal again and type
1 |
node app.js |
which yields
1 |
Server running at http://127.0.0.1:8124/ |
Now, when I navigate to http://localhost:8124 I get this:
Nifty, huh?
In the next post, I will see if I can get up and running with Express – a simple but powerful Sinatra-like web framework for Node.