Node.js整合式開發環境Cloud9搭建及CoffeeScript外掛程式使用

來源:互聯網
上載者:User

Node.js是神馬?簡單來說,它是實現JavaScript在本地啟動並執行一個架構,包括一個js語言解譯器(V8)和一些類庫。有了它,就可以使用js語言開發一些本地的應用,使js瀏覽器外用的束縛。
Node.js使用的語言解譯器與google chrome 瀏覽器用的是同一個高效的開源引擎-V8。
關於Node.js的入門教程可以參考這本書:"Node入門» 一本全面的Node.js教程",線上地址 http://www.nodebeginner.org/index-zh-cn.html
對於Node.js的開發,除了使用傳統的文本編輯工具如Notepad++,vim,還有一款開源的IDE可用,這就是Cloud9 IDE
官方網站:http://c9.io/
這個開發環境的獨特之處就是直接使用瀏覽器在雲端完成開發工作,不用在本地安裝node啊之類的東東,
不過使用官方的網站有一些限制,比如只對開源的項目免費,不支援第三方擴充啊等等。
本篇文章要講的是如何在ubuntu 10.10上搭建Cloud9 IDE。
首先說明,Cloud9(版本是0.6),基於node 0.4.2版本構建,雖然目前的node(版本0.6.14)支援windows,但很多類庫對window的支援還不完善,
所以還是在linux上搭建會比較順利。
首先安裝git,直接使用
[plain]
sudo apt-get install git 
然後安裝nodejs,使用
[plain]
sudo apt-get install nodejs 
安裝 npm,使用
[plain]
sudo curl http://npmjs.org/install.sh | sh 
然後在使用者目錄下建立一個檔案夾:
[plain]
mkdir cloud9 
在cloud9內執行
[plain]
git clone git://github.com/ajaxorg/cloud9.git 
更文藝一點的做法:
[plain]
git init 
git remote add origin git://github.com/ajaxorg/cloud9.git 
git fetch master 
git checkout master 
然後執行
[plain]
bin/cloud9.sh 
初次執行,會下載一堆submodule,如果不幸中間網路中斷了,可以使用
[plain]
git submodule update 
下載完後會對這些submodule進行編譯,可能會遇到libxml package prinstall Failed 的問題,
使用命令:
[plain]
sudo apt-get install libxml2-dev 
最終成功後,會顯示如: 

 


並自動啟動瀏覽器,開啟127.0.0.1:3000,進入IDE介面
cloud9.sh是可以帶參數的,具體可使用 cloud9.sh --help查看用法

[sql]
Show this help message 
    --help 
Load the configuration from a config file. Overrides command-line options. Defau 
lt: <null> 
    -c, --config <value> 
Run child processes with a specific group. Default: <false> 
    -g, --group <value> 
Run child processes as a specific user. Default: <false> 
    -u, --user <value> 
Activate debug-mode. Default: <false> 
    -d, --debug 
Disable auto-open of the browser. 
    -s, --silent 
Define an action to execute after the Cloud9 server is started. Default: <null> 
    -a, --action <value> 
IP address where Cloud9 will serve from. Default: <127.0.0.1> 
    -l, --ip <value> 
Port number where Cloud9 will serve from. Default: <3000> 
    -p, --port <value> 
Path to the workspace that will be loaded in Cloud9 (may be relative or absolute 
). Default: <.> 
    -w, --workspace <value> 

我使用的命令列:
bin/cloud9.sh -l 192.168.37.130 -w ../mynode/ -s
具體意思就是指定一個固定ip,指定工作目錄,不自動啟動瀏覽器
啟動成功後,就可以通過其他機器來訪問這個網站了,: 


 


快速鍵:Ctrl+數字可以切換編輯器中的檔案,Ctrl+s儲存檔案,Ctrl+F5是debug,Shift+F5是stop,可以在preferences中設定vim mode使用vim方式編輯源碼(大愛啊)等等,最下面的命令列視窗可以執行一些伺服器端的命令如ls,git等,方便原始程式碼控制

 


使用CoffeeScript:
JS使用的是類C/Java的文法風格,CoffeeScript則採用了類似Ruby/Python的文法,
使指令碼的編寫工作靈活有趣,減少代碼量等等,好處很多,
關於CoffeeScript,這本書值得一看(只有142頁):
《CoffeeScript: Accelerated JavaScript Development》網址:http://pragprog.com/book/tbcoffee/coffeescript


在項目中使用Coffee很方便,
首先使用npm安裝CoffeeScript:npm install -g coffee-script
然後在項目目錄下執行npm link coffee-script
建立一個coffee尾碼的指令檔test.coffee
內容:[javascript] hi = -> 
    arr = [1..3] 
    last = x for x in arr 
    console.log "#{last}" 
module.exports.hi = hi 
hi = ->
    arr = [1..3]
    last = x for x in arr
    console.log "#{last}"
module.exports.hi = hi


再建立個runner.js,內容:
[javascript] require("coffee-script"); 
var test = require("./test"); 
test.hi(); 
require("coffee-script");
var test = require("./test");
test.hi();
直接調試runner.js即可。


在cloud9中整合livecoffee外掛程式:
首先到使用者目錄下執行:
[plain]  nickwar@ubuntu:~$ git clone git://github.com/tanepiper/cloud9-livecoffee-ext.git cloud9/client/ext/livecoffee 
nickwar@ubuntu:~$ git clone git://github.com/tanepiper/cloud9-livecoffee-ext.git cloud9/client/ext/livecoffee

然後在介面中的Windows->Extension Manager->User Extensions中輸入"ext/livecoffee/livecoffee",點Add,即可加入此外掛程式

  


 


開啟coffee檔案後,選擇Edit->LiveCoffee即可顯示對應的js代碼視窗

  


在windows7上執行cloud9的話,需要自行下載node-builds檔案夾到support中,代碼地址:https://github.com/ajaxorg/node-builds
不過socket.io貌似出現問題,暫未成功。。。。。
報錯資訊:
uncaught exception:
TypeError: Object #<a Server> has no method 'once'
    at new Manager (/cygdrive/d/Program Files/Cloude9/cloud9/support/socket.io/l
ib/manager.js:123:10)
    at Object.listen (/cygdrive/d/Program Files/Cloude9/cloud9/support/socket.io
/lib/socket.io.js:71:10)



 摘自  NickWar的專欄 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.