天天看點

mac 環境下安裝node.js

最近換了macbook,于是又要裝node.js,記錄一下

系統:OS X Mountain Lion 10.8.3

1/ 下載下傳node.js for mac

    http://nodejs.org/download/       

mac 環境下安裝node.js

    輕按兩下安裝,一路next,很簡單。

2/ 測試是否安裝成功

   control + space 打開spotlight,輸入“終端”,就打開了終端,類似win下的cmd

   輸入 node -v , 回車;

   輸入 npm -v , 回車

   若無錯,則顯示版本号

mac 環境下安裝node.js

3/ 測試運作

    在(Finder > ~username )目錄,建立 helloworld.js

mac 環境下安裝node.js

    若在Finder左側欄看不到你的使用者名,則打開Finder的偏好設定,勾選你的使用者名

mac 環境下安裝node.js

    回到主題,helloworld.js 内容:

*********code start**********

var http = require('http');

http.createServer(function(req, res){

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

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

}).listen(8808, '127.0.0.1');

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

**********code end**********

    運作:  打開“終端”,輸入“node helloworld.js” (若在其他目錄,則在簡介裡複制完整位置)

   such as: node /Users/inman/Sites/node/helloworld.js

mac 環境下安裝node.js

    若無誤,則顯示 Server running at http://127.0.0.1:8808

    打開safari,輸入http://127.0.0.1:8808 , 顯示“Hello World”

mac 環境下安裝node.js

    that's it !

轉載于:https://blog.51cto.com/neikvon/1155998

繼續閱讀