2012年7月1日 星期日

啟用 Mongolab 的 240M 免費 MongoDB 並在 node.js 中連線

MongoDB 是一種 NoSQL 之資料庫系統. Mongolab.com 提供了 240M 以下的免費服務. 想要讓在 Nodester.com 中執行的 node app 能夠連上雲端資料庫來練習或實作小規模之Web App, Mongolab.com 是一個不錯的選擇.

 1. 在 mongolab.com 開通新帳號並產生一個 database. Database產生後會有一個 connect string 以供使用

2. 為 node.js 安裝 mongolab 使用之模組: mongoose
$ npm install mongoose

3. 編輯你的 app.js, 例:
(假設database之username: david, password: 123456, 資料庫名: testdb)
var mongoose = require("mongoose");
var db = mongoose.connect(
"mongodb://david:123456@ds029807.mongolab.com:29807/testdb");

這個設定在 Nodester 也是可以使用的

4. 在node.js app 中存取 data
先要建立一個 schema (類似一個 record set structure)並用此 schema 建立一個通道(model), 最後在將此通道和database 連通
//建立一個 schema
var Schema = mongoose.Schema;

// Create a schema for our data
var MemberSchema = new Schema({
  username: String,
  password: String
});

// 用此 schema 建立一個通道
// Use the schema to register a model to the db
mongoose.model('member', MemberSchema);

// 將此通道和database 連通, 連到一個名叫 members 的 collection (類似SQL中的table),db 中mongodb 會自動加 s 在 collection 名後, 但我們依照用 member
var MemberModel = mongoose.model('member');

有了連通到 database 之通道後就可以加入資料:
new MemberModel( {username:'Elvis', password:'11111'} ).save();
new MemberModel( {username:'John', password:'22222'} ).save();
new MemberModel( {username:'Willie', password:'33333'} ).save();

var tmpDataSet = new Member();
tmpDataSet.usrename = "George";
tmpDataSet.password = "44444";
tmpDataSet.save();

也可以查詢資料:
MemberModel.find( {username: "Elvis"} , function(err, results  ) {
// 解析欄位
for(var i in results) {
  console.log("user="+ results[i].username + " pswd=" + results[i].password);
  console.log(results[i]);
}

// 整個 schema 顯示
results.forEach(function(rmember) {
  console.log(rmember);
});
});

在 Ubuntu 12.04 安裝 redis 資料庫及node.js使用範例

在terminal 中執行以下命令即會安裝並啟動完成
$ sudo apt-get install redis-server

使用 npm 安裝 redis 提供 node.js 用的 module
$ npm install redis
$ npm install hiredis redis

測試 redis 是否運行順利:

1. 在node.js之工作目錄增加一檔案 app.js 並輸入以下內容:
var redis = require("redis"),
    client = redis.createClient();

client.on("error", function (err) {
    console.log("Error " + err);
});
// Set a value
client.set("string key", "Hello World", redis.print);
// Get the value back
client.get("string key", function (err, reply) {
    console.log(reply.toString());
});
// Clean quit (waits for client to finish)
client.quit();

2. 用 node.js 執行 app.js
$ node app.js
$ Reply: OK
$ Hello World


3. 在terminal中用命令列執行
$ redis-cli
redis 127.0.0.1:6379>
4. 停止 redis 之運行
$ src/redis-cli shutdown

5. 啟動 redis
$ src/redis-server

2012年6月23日 星期六

在 Ubuntu 12.04 啟動 Nodester 雲端平台 Node.js App service

Nodester 是一個免費的 Node.js 雲端平台. 你所開發出來的 node App 可以放在這裡提供他人使用. 使用前先到 Nodester.com 登記你的 email 後會在兩天左右收到 coupon
收到此一 coupon 之string 後回到 www.nodester.com/help 登錄:
coupon:
username:
password:
email:
RSA key: (cat ~/.ssh/id_rsa.pub 後將內容copy and past)

Note: 產生 rsa key 之方法: (假設home directory name: "ejeng")
~$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ejeng/.ssh/id_rsa):
Created directory '/home/ejeng/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ ejeng /.ssh/id_rsa.
Your public key has been saved in /home/ejeng/.ssh/id_rsa.pub.
The key fingerprint is:
2b:21:c4:e0:8a:dd:5e:1a:cb:5a:08:88:fa:75:73:10 ejeng@ubuntu
The key's randomart image is:
+--[ RSA 2048]----+
|  .              |
| . o             |
|  . o E          |
|+o o   .         |
|* . + + S        |
|.. + * o .       |
|. . B + o        |
| . + . +         |
|  o              |
+-----------------+

開始安裝 Nodester 和 git 版本控管軟體:
~/nodejs$ sudo apt-get install git-core
~/nodejs$ npm install nodester-cli -g
~/nodejs$ nodester user setup myname mypasswd
nodester info verifying credentials
nodester info user verified..
nodester info writing user data to config

~/nodejs$ nodester user setkey
nodester info sending sshkey: /home/ejeng/.ssh/id_rsa.pub
nodester info ssh key successfully sent

開始產生專案 (假設專案名稱為 myAppName):
~/nodejs$ nodester app create myAppName
nodester info creating app: myAppName server.js
nodester info successfully created app  myAppName  to will run on port 18931 from server.js
nodester info run nodester app init  myAppName  to setup this app.

~/nodejs$ nodester app init myAppName 
nodester info initializing git repo for  myAppName  into folder myAppName 
nodester info cloning the repo git clone git@nodester.com:/node/git/myname/10778-d6b8f786e8e3a04d8f82ff143c927136.git myAppName 
The authenticity of host 'nodester.com (50.16.203.53)' can't be established.
RSA key fingerprint is a1:64:54:7c:9b:73:9f:22:a8:95:44:4f:7b:25:15:68.
Are you sure you want to continue connecting (yes/no)? yes
nodester info clone complete
nodester info Writing app data to config in myAppName/.nodester.appconfig
nodester info writing app files
nodester info writing the package.json file
nodester info processing the initial commit
nodester info myAppName started.
nodester info Some helpful app commands:

     cd ./myAppName 
     curl http:// myAppName.nodester.com/
      nodester app info
      nodester app logs
      nodester app stop|start|restart


在一browser中打開 http://myAppName.nodester.com 即可看到server.js 所要呈現之內容

修改內容: 例: server.js 改顯示文字內容後
~/nodejs$ cd myAppName
~/nodejs/myAppName$ vi server.js      (修改檔案內容)
~/nodejs/myAppName$ git commit -am "change string"
      (將改過之檔案更新到git)
~/nodejs/myAppName$ git push
  (將 local repo 更新到myAppName.nodester.com)

專案後續修改完善過程中增加的檔案和目錄
~/nodejs/myAppName$ git add file1.js file2.js file3.js
~/nodejs/myAppName$ git add folder1/\*.png

~/nodejs/myAppName$ git add . (另外: 單獨用這行會將所有新增與修改之檔案自動加入)
~/nodejs/myAppName$ git commit -am "new files added"
~/nodejs/myAppName$ git push (系統會重新起動App)

在Nodester.com 之專案中安裝需要用到的 node.js 其他 modules
~/nodejs/myAppName$ nodester npm install express
~/nodejs/myAppName$ nodester npm install request
~/nodejs/myAppName$ nodester npm install now
~/nodejs/myAppName$ ..... (some error)
 Note: 發現是 now.js 所需的 node-proxy@0.6.0 無法安裝
       嘗試先安裝 node-proxy@0.5.1 成功後再安裝 now.js即可
~/nodejs/myAppName$ nodester npm install node-proxy@0.5.1
~/nodejs/myAppName$ nodester app restart myAppName (重新啟動服務)


在 Ubuntu 12.04 中安裝 Node.js

在安裝完 Ubuntu 12.04 後, 打開 terminal 後輸入以下命令即可:


sudo apt-get install python-software-properties
sudo apt-add-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs npm

完成後安裝 module:
npm install express
npm install now

.... error: g++ command not found // 找不到 g++, 另外安裝如下
sudo apt-get install g++

npm install now
npm install request

2012年5月19日 星期六

在 Ubuntu 11.10 中安裝 Python 的 MySQLdb module


在學習 Python 的過程中需要用到資料庫的功能, 由於之前已經在 Ubuntu 11.10 中安裝了 MySQL, 為了讓 Python 可以存取資料, MySQL 提供給 Python 一個叫作 MySQLdb 的 module, 這個 module 提供 Python 存取 MySQL 之介面.  因此為了可以存取資料, 我們需要安裝此一module. 以下為安裝之步驟:

1. 到 SourceForge ("http://sourceforge.net/projects/mysql-python/")下載取得檔案:
MySQL-python-1.2.3.tar.gz 後建立執行檔

$> tar -xzvf MySQL-python-1.2.3.tar.gz
$> cd MySQL-python-1.2.3
$> python setup.py build


執行後出現以下錯誤:

Traceback (most recent call last):
  File "setup.py", line 5, in
    from setuptools import setup, Extension
ImportError: No module named setuptools

原來是因為還需要一個 "python-setuptools" 的 module, 我們用以下方法快速取得:

$> sudo apt-get install python-setuptools python-dev libmysqlclient15-dev

完成後再執行一次 build, 之後安裝就成功了:

$> sudo python setup.py build
{出現一些訊息}

$> sudo python setup.py install
{出現一些訊息}


3. 在 Python 程式中 import 此 module 即可看到正確執行, 沒有錯誤訊息出現

$>python
>>> import MySQLdb
>>>


2012年4月13日 星期五

在Ubuntu 11.10中安裝 LAMP (Linux, Apache, MySQL, PHP)

終於要開始整理網頁開發的相關資訊了. 這兩天閱讀了PHP和MySQL的教學並且在Ubuntu中試作, 以下為整理出的資訊: 

0. 先登入具有root權限之帳號 (Ubuntu安裝時沒有開放root, 使用者無法直接以root登入, openSUSE則有root)
$sudo su

1. 安裝 MySQL 5, 安裝過程會要求你設定 MySQL 管理者 root 的密碼
$apt-get install mysql-server mysql-client
$New password for the MySQL "root" user: (輸入密碼)
$Repeat password for the MySQL "root" user: (輸入密碼)

2. 安裝 Apache2 (Ubuntu 套件中預存有 Apache2)
$apt-get install apache2 

安裝完成後可在 Ubuntu 中打開網頁流覽器輸入 "http://localhost"會在流覽器中出現以下內容, 表示安裝完成並成功啟動.
It works!
This is the default web page for this server.
The web server software is running but no content has been added yet.

3. 安裝 PHP5
$apt-get install php5 libapache2-mod-php5 

安裝完成後必需重新啟動 Apache 讓此網頁伺服器知道要和 PHP 配合運作, 否則它只會把 .php檔當作一般文件檔打開其內容而不是執行該檔.
$/etc/init.d/apache2 restart 

4. 測試 .php 可否執行 將寫好的 .php 檔覆製到網頁目錄, 預設的網頁目錄為 "/var/www"
$cp testPHP.php /var/www/testPHP.php 

在 Ubuntu 中打開網頁流覽器輸入 "http://testPHP.php" 會在流覽器中出現testPHP.php執行之內容, 表示 PHP 可成功執行.

5. 顯示php 訊息:
5.1 在 /var/www/ 中新增一 info.php, 其內容如下:

<?php phpinfo(); ?>

5.2 在 Ubuntu 中打開網頁流覽器輸入 "http://info.php" 會在流覽器中出現php訊息之內容. 同時也可注意是否PHP有支援MySQL, 目前應該沒有.

6 在 PHP5 中增加對 MySQL 之支援, 我們可以安裝 php5-mysql  和相關之套件:
$ apt-cache search php5
$ apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl


6.1 重新啟動 Apache Server
$ /etc/init.d/apache2 restart


6.2 在 Ubuntu 中打開網頁流覽器輸入 "http://info.php" 檢視是否PHP有支援MySQL, 應該出現支援之訊息

7. PERL, 安裝 mod_perl
$apt-get install libapache2-mod-perl2

7.1 修改 Apache之設定檔以便告知Apache Perl 相關 .pl 檔所會存放的位置, 打開設定檔:
$gedit /etc/apache2/apache2.conf

7.2 假設要存放在 /home/ejeng/Documents/Host/perl 目錄中, 則在設定檔中加入以下段落:
(在此為設定Apache serve 之 perl 存放處)


Alias /perl/ /home/ejeng/Documents/Host/perl/
(註: 此行為增設連結目錄, 轉連到自設目錄可必免許多檔案編輯和執行權限之問題)

PerlModule ModPerl::Registry


   SetHandler perl-script
   PerlHandler ModPerl::Registry
   #PerlHandler ModPerl::PerlRun
   Options +ExecCGI
   #PerlSendHeader On


7.3 將Apache之設定檔回存後重新啟動 Apache Server
$ /etc/init.d/apache2 restart

7.4 在 /home/ejeng/Documents/Host/perl 目錄中編寫一測試 perl 檔: test.pl
$cd /home/ejeng/Documents/Host/perl
$vi test.pl


#!/usr/bin/perl
use CGI;
my $query= new CGI;
print $query->header;
print "This is my first per in ejeng home"\n";


7.5 在 Ubuntu 中打開網頁流覽器輸入 "http://localhost/perl/test.pl"會在流覽器中出現以下內容, 表示 Perl 安裝設定完成並成功執行測試 test.pl
" This is my first per in ejeng home ".


8. 在流覽器中顯示中文: 修改 Apache之設定檔並重新啟動 Apache Server
$gedit /etc/apache2/apache2.conf

加入:
ServerName 127.0.0.1  (註: 此行為必免重啟時出現找不到domain ip之警句)
AddDefaultCharset UTF-8

$/etc/init.d/apache2 restart 

2012年3月12日 星期一

在Ubuntu11.10 中安裝 Skyeye-1.3.3

SkyEye是由中國所開發的一個開源的模擬器,它的擬真環境相當於一個嵌入式計算機系統的模擬器,你可以在SkyEye裡運行一些嵌入式Linux操作系統,如ARMLinux,uClinux,uc/OS-II(ucos-ii)等,並能分析和調試它們的source code。

通過SkyEye可以幫助嵌入式系統的學習,在不需要額外硬件的情況下學習和分析uclinux操作系統和其它嵌入式操作系統,如ucosII等。在學習嵌入式系統時,學者通常都必需準備有學習用的硬體套件「開發板」、「評估版」,英文叫做「Evaluation Board」,或簡稱「EVB」。但是當學者實在是無法備有硬體套件時則可以使用模擬器來練習,但最終還是需要有實際的板子才能學好。 

最近剛安裝了Ubuntu11.10,想安裝 Skyeye-1.3.3來學習embedded system,在網上google了一些教學內容整理於下:

0. 開啟一個terminal視窗

1. 下載 skyeye-1.3.3_rel.tar.gz
$wget http://ncu.dl.sourceforge.net/project/skyeye/skyeye/skyeye-1.3.3_rc1/skyeye-1.3.3_rel.tar.gz

2. 執行以下script:
$sudo apt-get install libgtk2.0-dev pkg-config libatk1.0-dev libpango1.0-dev libfreetype6-dev libglib2.0-dev libx11-dev binutils-dev libncurses5-dev libxpm-dev

3. 下載 llvm-2.8.tgz
$wget http://llvm.org/releases/2.8/llvm-2.8.tgz

4. 解壓 llvm-2.8.tgz 並自動產生 llvm-2.8 目錄
$tar zxvf llvm-2.8.tgz 

5. 進入 llvm-2.8 目錄並分項執行以下 scripts:
$./configure
$make
$sudo make install 

6. 解壓 skyeye-1.3.3_rel.tar.gz 並自動產生 skyeye 目錄,進入 skyeye 目錄並分項執行以下 scripts:
$tar zxvf skyeye-1.3.3_rel.tar.gz
$cd skyeye
$sudo rm -fr /opt/skyeye
$./configure
$make lib
$make
$sudo make install
$sudo make install_lib

7. 修改 ~/.bashrc 在文件尾加入
     export PATH=/opt/skyeye/bin:$PATH
$vi ~/.bashrc

回存後可用 $source ~/.bashrc 讓此連結立即生效

執行 skyeye 1.3.3 可在terminal中輸入:
$/opt/skyeye/bin/skyeye_main.py

或到測試目錄:
$cd /opt/skyeye/testsuite/arm_hello
$/opt/skyeye/bin/skyeye_main.py -e arm_hello

即會叫出   <skyeye>, 此時分別輸入命令
$  <skyeye> start        // 會出現一個小視窗, 此為模擬Embedded System之輸出
$  <skyeye> run          // 會在小視窗中出現 Hello World , 這是 arm_hello 所送出
$  <skyeye> stop        // 停止arm_hello 之執行, Hello World會停止出現
$  <skyeye> quit         // 離開 skyeye


2012年3月1日 星期四

在 MFC 中 使用 OpenGL

想在 MFC 中 使用 OpenGL 可參考以下連結:
參考連結1: MFC_OpenGL/tutorial_01
參考連結2: Setting Up OpenGL in an MFC Control


依此在 Visual Studio .NET 2003 和 Visual Studio 2010 中實作過
遇到的問題是 include header 與 linker
Solution for 2003:
1. 在source code中
    #include  <gl/gl.h>
    #include  <gl/glu.h>
2. 在 Linker/Input/Additional Dependencies中加入
     opengl32.lib glu32.lib


Solution for 2010:
1. 下載及安裝GLUT
2. 在source code中
    #include  <gl/glut.h>
    #include  <gl/glu.h>
3. 在 Linker/Input/Additional Dependencies中加入
     C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A