centos6.0下安裝Node.js以及使用

來源:互聯網
上載者:User

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.