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 */
  1. var sys = require('sys'),
  2.   http = require('http');
  3.  
  4. http.createServer(function(req, res) {
  5.   res.writeHead(200, {'Content-Type': 'text/html'});
  6.   res.write('
  7. <h1>Hello World</h1>
  8. ');
  9.   res.end();
  10.   sys.puts('Served connection from ' + req.connection.remoteAddress);
  11. }).listen(8080);
  12.  
  13. 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

  2. It’s nice to be able to see so many sites all in one place.

Leave a Reply

%d bloggers like this: