Dougal Campbell's geek ramblings

WordPress, web development, and world domination.

Installing node.js on a Mac

For anyone else who might be interested, here are some quick-and-dirty instructions for how to install node.js and npm (node package manager) on Mac OS X, from the terminal. This assumes that you’ve already installed the XCode tools, and git:

# Fetch and build node.js
git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install

# Now install the node package manager, npm:
sudo chgrp -R staff /usr/local/{share/man,bin,lib/node}
sudo chmod -R g+w /usr/local/{share/man,bin,lib/node}
curl http://npmjs.org/install.sh | sh

# Use npm to install some modules:
npm install vows
npm install oauth
npm install oauth-client

If you don’t feel comfortable doing the directory permission and ownership changes used above for npm, see the npm website for alternative installation methods.

If you haven’t already heard of node.js, it is a network server framework which you program in server-side javascript. If you are already familiar with asynchronous networking frameworks like Python’s twisted, it is similar to that. Because of its asynchronous event-based structure, it is able to handle high volumes of connections without resorting to forking or complicated process threads.

A quick Hello World example:

/* helloworld.js */
var sys = require('sys'),
  http = require('http');
http.createServer(function(req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write('
  <h1>Hello World</h1>
  '); 
  res.end(); 
  sys.puts('Served connection from ' + req.connection.remoteAddress); 
}).listen(8080); 
sys.puts('Server running on port 8080');

This will create an HTTP server listening on port 8080 (on all IP addresses available, since we didn’t limit it to a specific one). For every request, it will output a ‘Hello World’ message to the client. Additionally, it will log the IP address of the client on the console.

About Dougal Campbell

Dougal is a web developer, and a "Developer Emeritus" for the WordPress platform. When he's not coding PHP, Perl, CSS, JavaScript, or whatnot, he spends time with his wife, three children, a dog, and a cat in their Atlanta area home.
This entry was posted in Tech and tagged , , , , , , , , , . Bookmark the permalink.

2 Responses to Installing node.js on a Mac

  1. Pingback: Tweets that mention Installing node.js on a Mac -- Topsy.com

Leave a Reply

%d bloggers like this: