MacOSXにnode.js環境を構築してみた
node.jsは、CentOS5.5で試してたけどMacOSにも入れてみた。Macはhomebrewでインストールすると簡単でした。
MacOSX 10.6.7
node.js 0.4.5
まずは、Homebrewが入っていなければ以下のコマンドでインストール。
$ ruby -e "$(curl -fsSL https://gist.github.com/raw/323731/install_homebrew.rb)" ・・・ Password: ==> /usr/bin/sudo /usr/bin/chgrp staff /usr/local/. /usr/local/lib ==> Downloading and Installing Homebrew... ==> Installation successful!
確認してみる
$ brew -v 0.8
nodeインストール
$ brew install node
npmインストール
$ curl http://npmjs.org/install.sh | sh
nvmインストール
$ npm install nvm $ source ~/.bashrc $ nvm install v0.4.5 $ node -v v0.4.5
サンプルを試す
node.js公式サイトのサンプルを動かしてみる。
example.jsファイルを作成して以下の内容を記述する
var http = require('http') http.createServer(function (req,res){ res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World!!'); }).listen(8124, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8124/');
実行する
$ node example.js Server running at http://127.0.0.1:8124/
ブラウザでhttp://127.0.0.1:8124/にアクセスすると
表示されました!