歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux(CentOS 6.0)下安裝Node.js以及使用

Linux(CentOS 6.0)下安裝Node.js以及使用

日期:2017/2/28 14:36:49   编辑:Linux教程

Linux(CentOS 6.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"});

Node.js 的詳細介紹:請點這裡
Node.js 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved