1. 程式人生 > >Linux安裝Node.js(源碼編譯安裝)環境:Ubuntu 12.04.2

Linux安裝Node.js(源碼編譯安裝)環境:Ubuntu 12.04.2

www. require oca list make roo 3.5 如果 環境

Linux安裝Node.js(源碼編譯安裝)

環境:

Ubuntu 12.04.2 LTS (GNU/Linux 3.5.0-23-generic i686)

下載Node.js安裝包,請參考網址:http://nodejs.org/download/

技術分享圖片

這裏選擇源碼包安裝方式,安裝過程如下:

登陸到Linux終端,進入/usr/local/src目錄,如下:

root@ubuntu:~# cd /usr/local/src/

下載nodejs安裝包:

#wget http://nodejs.org/dist/v0.10.17/node-v0.10.17.tar.gz

技術分享圖片

2,解壓文件並安裝

# tar xvf node-v0.10.17.tar.gz # cd node-v0.10.17 # ./configure # make # make install # cp /usr/local/bin/node /usr/sbin/ node 查看當前安裝的Node的版本 # node -v v0.10.17 www.yiibai.com

到此整個安裝已經完成,如果在安裝過程有錯誤問題,請參考以下解決: 可能出現的問題:

  1. The program ‘make‘ is currently not installed. You can install it by typing: apt-get install make

按照它的提示,使用命令

# apt-get install make

  1. g++: Command not found 沒有安裝過g++,現在執行安裝:

#apt-get install g++

測試程序 hello.js:

console.log("Hello World");

# node helloworld.js

技術分享圖片

另外的一個實例:WebServer

這個簡單Node 編寫的 Web服務器,為每個請求響應返回“Hello World”。

var http = require(‘http‘); http.createServer(function (req, res) { res.writeHead(200, {‘Content-Type‘: ‘text/plain‘}); res.end(‘Hello World\n‘); }).listen(1337); console.log(‘Server running at port 1337 ‘);

要運行服務器,將代碼編寫到文件example.js 並執行 node 程序命令行:

# node example.js

Server running at http://127.0.0.1:1337/

技術分享圖片

有興趣的朋友可以嘗試下面一個簡單的TCP服務器監聽端口1337 並回應的一個例子:

var net = require(‘net‘); var server = net.createServer(function (socket) { socket.write(‘Echo server\r\n‘); socket.pipe(socket); }); server.listen(1337, ‘127.0.0.1‘);

Linux安裝Node.js(源碼編譯安裝)環境:Ubuntu 12.04.2