Macでnode.jsを動かしてみた

まずはnode.jsのインストールから。
(インストール対象のMacには既にhomebrewがインストールされている)

 

$ sudo  brew  install  node

 

インストールされたことを確認する。

$ which node

/usr/local/bin/node

 

nodeのバージョンを確認する。

$ node -v

v0.10.32

 

適当なディレクトリで以下のプログラムを打ち込む。(ファイル名はtest1.js)

var http = require('http');

 

http.createServer(function (request, response) {

  response.writeHead(200, {'Content-Type': 'text/plain'});

    response.end('Hello World\n');

}).listen(8124);

 

console.log('Server running at http://127.0.0.1:8124/');



test1.jsを実行する。

$ node  test1.js

Server running at http://127.0.0.1:8124/

 

ブラウザで

http://127.0.0.1:8124/

にアクセスしてみる。

 

ブラウザ上に、

Hello World

が表示された。