2015/01/27

Ubuntu Linux下,安裝與運行 Node.js

---- 安裝 GNU C compiler 和 GNU C++ compiler
To install the gcc and g++ compilers, you will need the build-essential package. This will also install GNU make.

build-essential contains a list of packages which are essential for building Ubuntu packages including gcc compiler, make and other required tools.
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential
$ sudo apt-get install gccw
$ sudo apt-get install g++
$ gcc -v
$ make -v


---- Linux從源碼安裝Node,才能安裝你所需要的版本

#Ubuntu安裝代碼
$ sudo wget http://nodejs.org/dist/v0.10.35/node-v0.10.35.tar.gz
$ sudo tar -zxf node-v0.10.35.tar.gz #Download this from nodejs.org
$ cd node-v0.10.35
$ sudo ./configure && sudo make && sudo make install
$ sudo cp /usr/local/bin/node /usr/sbin/


---- 測試一下看裝好了沒有,用命令查看node和npm版本
$ node -v
$ npm -v


---- 編寫一個簡單的伺服器體驗一下
var http = require('http');

http.createServer(function (request, response) {
    response.writeHead(200, {'Content-Type': 'text/plain'});
    response.end('Hello World\n');
}).listen(8000);

console.log('Server running at http://localhost:8000/');

• 1、保存代碼到 test1.js
• 2、命令列下執行命令:node test1.js
• 3、通過流覽器訪問 http://localhost:8000/


---- 接著安裝express
$ sudo apt-get install node-express

---- 建立express工程,啟動第一個專案
$ express -e nodejs-demo

install dependencies:
$ cd nodejs-demo && npm install

run the app:
$ node app

---- 安裝curl
$ sudo apt-get install curl
$ curl localhost:3000

沒有留言: