Linux下(centos6.0)安裝Node.js
1.wget http://nodejs.org/dist/node-v0.6.9.tar.gz
tar zxvf node-v0.6.9.tar.gz
cd node-v0.6.9
./configure --prefix=/usr/local/node
----------安裝提示-------------
Checking for program g++ or c++ : not found
Checking for program icpc : not found
Checking for program c++ : not found
----------------------------
yum install gcc-c++ 可以解決
-----------------------------
make
make install
2.測試
建立test.js檔案,內容如下:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
執行:node test.js
# /usr/local/node/bin/node /www/test.js
在瀏覽器裡輸入 http://127.0.0.1:1337/,可以看到 "Hello World"字樣,即表示安裝成功!注意後面不能加檔案名稱.
注意事項:
1.用戶端只能通過連接埠訪問,不能指定js檔案名稱.
2.監聽IP地址可以省略,這樣任何地方都可以訪問.如果指定了127.0.0.1,則只能在本機才可以訪問!
如果要區域網路其他機器或者互連網訪問
那麼自然你需要修改你的偵聽ip已經連接埠,次連接埠在防火牆要開啟
例如80連接埠,如果此連接埠被佔用你要啟用其他連接埠
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(80, "192.168.1.100");
console.log('Server running at http://192.168.1.100:80/');
-------------------------------------------
注意,如果你想要要輸出 html 代碼到瀏覽器,請設定 content-type 為 text/html ,如果設定為 text/plain 將只輸出文本
response.writeHead(200, {"Content-Type": "text/html;chartset:utf-8"});
--------------------------------------------
參考:
http://www.cnblogs.com/rubylouvre/archive/2010/07/15/1778403.html
http://www.cnodejs.org/
http://club.cnodejs.org/topic/4f16442ccae1f4aa27001071