node.js教學

  1. 安裝node.js
  2. 基本node指令操作
  3. npm操作
  4. 雲端編輯環境
  5.  ebook - IT eBooks
  6. 套件
  7. 待續...

基本知識(二) "http"

1.建立網路的程式node.js內建有http的module
因此呼叫出來設定即可

2.基本程式碼
hello_http.js
內容為
var http = require('http');
var ip = '127.0.0.1';
var port = 1337;

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(port, ip);

console.log('Server running at http://' + ip + ':' + port);
執行
node hello_http.js
假設如上面程式碼所示只會監聽127.0.0.1的IP的請求
因此外面請求會無視
要讓外面可以請求
請將IP改成0.0.0.0
  

3.開啟網頁
輸入主機的IP和port有看到便是成功

4.停止執行
我們呼叫http的listen
它會一直監聽直到程式結束
因此需要按下Ctrl+C來強制結束

node.js 基本知識(一) "hello world"

1.node.js是使用javascript執行它的語法
因此可以寫程式為副檔名為js的檔案將它執行

2.建立檔案
hello.js
內容為
console.log("hello, world");
3.執行hello.js
node hello.js

4.在node.js裡每個檔案都是一個獨立的module
因此要讀其他人的檔案(module)
需要使用 exports 和 require 互相溝通
exports 和 require 有好幾種寫法
下面是其中幾種
只要是符合Javascript的寫法都可

5-1.
hello_exports.js
內容為
module.exports = {
    do_a : function(r) {
        console.log( 'This is do_a method of hello_export module, r:' + r);
    },

    do_b : function(r) {
        console.log( 'This is do_b method of hello_export module, r:' + r);
    }
};

hello_requir.js
內容為
var dosomething = require('./hello_exports.js');
var doing_b = require('./hello_exports.js').do_b;

dosomething.do_a('Run');
doing_b('Walk');

執行hello_requir.js
node hello_requir.js 

5-2.
hello_exports.js
內容為
exports.do_a = function(r) {
        console.log( 'This is do_a method of hello_export module, r:' + r);
};

exports.do_b = function(r) {
        console.log( 'This is do_b method of hello_export module, r:' + r);
};

hello_requir.js
內容如同5-1的hello_requir.js和執行

5-3:
hello_exports.js
內容為
module.exports = function(){
    console.log( 'This is module only one method.');
};

hello_requir.js
內容為
var dosomething = require('./hello_exports.js');>
dosomething();
如同5-1的執行

Linux node.js 安裝

1.建置環境
虛擬環境:visual box
Linux版本:Ubuntu server 12.04.2 32bit
網路:NAT+主機網路

2-1.安裝
直接利用apt-get安裝
sudo apt-get install nodejs
打入
node -v
顯示版本號
不過我當下在Ubuntu的版本過舊


2-2.下載原始檔編譯安裝
安裝編譯環境
sudo apt-get install g++ python make
到http://nodejs.org/download/下載原始檔
wget http://nodejs.org/dist/v0.10.11/node-v0.10.11.tar.gz
tar xvf node-v0.10.11.tar.gz
cd node-v0.10.11/
./configure
make
sudo make install
cd ..
node -v 



windows node.js 安裝

1.架設環境
OS:windows 7 64bit 企業版

2.安裝node
到http://nodejs.org/download/ 下載
http://nodejs.org/dist/v0.10.11/x64/node-v0.10.11-x64.msi
安裝完後開啟cmd
輸入
node -v 
會顯示版號就成功了


3.有問題打
path
看看C:\Program Files\nodejs\是否有被加入

4.沒有請到
電腦右鍵→內容→進階系統設定→進階→環境變數→系統變數
檢查PATH是否加入,有的話請等一下或是重開機
在開啟cmd就可以執行成功

samba加速設定

在samba的設定檔
vi /etc/smb.conf
[global] 節段加入:
read size = 1024
socket options = TCP_NODELAY SO_KEEPALIVE SO_RCVBUF=2048 SO_SNDBUF=2048 IPTOS_LOWDELAY
getwd cache = yes
存檔後輸入命令
service smb reload
速度立刻提昇。

Putty基本設定

1.設定語言
沒有設定會將中文字或其他語言輸出成亂碼

2.設定字體大小
PuTTY預設字體太小
可以自己選擇字體大小和字型

3.設定Terminal detials
因為libncurses所顯示出來的畫面會因為你的Termial-type string
所顯示出來會不一樣
假設是PuTTY預設的值
顯示出來的會是亂碼
設定成
linux
畫面會比較好看

4.設定X11 forwarding
X11的設定配上Xming
可以遠端使用Linux的圖形化界面
X display location
localhost:0.0


5.Session是儲存設定的地方
所以設定完後一定要Save

6.直接載入有儲存的設定
在PuTTY的捷徑上加上
-load COM3
COM3為設定的名子
就可以直接開啟程式並且載入設定

7.備份
在執行命令列輸入命令
regedit.exe /e PuTTY.reg HKEY_CURRENT_USER\Software\SimonTatham
在C:\Windows\System32
可以取得PuTTY.reg的備份檔
回復時直接點選PuTTY.reg即可