tag:blogger.com,1999:blog-77533767646236676682024-02-19T17:32:19.749-08:00Node.js in Examplek8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comBlogger59125tag:blogger.com,1999:blog-7753376764623667668.post-89402257414410957072016-08-26T12:00:00.001-07:002016-08-26T12:00:53.587-07:00Streaming http response<h1 style="-webkit-user-select: text; background-color: white; box-sizing: border-box; break-after: avoid; color: #333333; cursor: text; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; letter-spacing: 0.2px; line-height: 1.1; margin-bottom: 0.85em; margin-left: 0px; margin-right: 0px; margin-top: 0px !important;">
Streaming http response</h1>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
某些時候,我們會希望在Server端執行比較久的回應結果可以持續得回傳回來。在express中,可以透過res.write()來持續回傳結果,直到回傳完成,再結束這個連線... 下面是express中的片段範例... 其中"/exec"這個route是在本地端執行child_process.exec(),然後將結果回傳...</div>
<h2 style="-webkit-user-select: text; background-color: white; box-sizing: border-box; break-after: avoid; color: #333333; cursor: text; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 1.75em; letter-spacing: 0.2px; line-height: 1.1; margin-bottom: 0.85em; margin-top: 1.275em; orphans: 3; widows: 3;">
範例</h2>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 4px; border: none; box-sizing: border-box; break-inside: avoid; color: #333333; cursor: text; direction: ltr; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; break-inside: avoid; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px;">app.use('/exec', function(req, res){
exec(function(d, t){
res.write(d);
if(t && t == 'end') res.end('\ndone...');
});
});
function exec(fn){
const spawn = require('child_process').spawn;
const ls = spawn('bash', ['/tmp/test.sh']);
ls.stdout.on('data', (data) => {
fn(`stdout: ${data}`, 'stdo-data');
});
ls.stderr.on('data', (data) => {
fn(`stderr: ${data}`, 'stde-data');
});
ls.on('close', (code) => {
fn(`child process exited with code ${code}`, 'end');
});
}
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
下面是express所欲執行的shell...</div>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 4px; border: none; box-sizing: border-box; break-inside: avoid; color: #333333; cursor: text; direction: ltr; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; break-inside: avoid; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px;"># File: /tmp/test.sh
echo 1
sleep 1
echo 2
sleep 2
echo 3
sleep 3
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
啟動Server後,再透過curl測試...</div>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 4px; border: none; box-sizing: border-box; break-inside: avoid; color: #333333; cursor: text; direction: ltr; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; break-inside: avoid; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px;">curl localhost:3000/exec
stdout: 1
stdout: 2
stdout: 3
child process exited with code 0
done...
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
將會發現,上面的執行結果將是一行一行回覆回來,在結束之前connection將不會中斷...</div>
<h2 style="-webkit-user-select: text; background-color: white; box-sizing: border-box; break-after: avoid; color: #333333; cursor: text; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 1.75em; letter-spacing: 0.2px; line-height: 1.1; margin-bottom: 0.85em; margin-top: 1.275em; orphans: 3; widows: 3;">
細看執行重點</h2>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; orphans: 3; widows: 3;">
上面的程式之所以可以block住connection並且一段一段回傳結果,其中重點在express routing中使用res.write()來做data的回傳,另外也因為child_process.spawn()提供event driven的方式,在每筆data回傳時候呼叫對應的event執行方式...</div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-31350285287298669202016-04-10T09:50:00.001-07:002016-04-10T09:50:57.875-07:00找到了個不錯的command line輔助工具~inquirerinquirer是一套讓您可以製作互動指令的工具,透過inquirer可以達到像是node interpreter的功能。一般在做互動式安裝系統時,需要多次設定參數與環境,或選擇不同的安裝項目時,非常有用ㄛ。<br />
<h2 id="github-repository">
Github repository</h2>
<ul>
<li><a href="https://github.com/sboudrias/Inquirer.js" target="_blank">https://github.com/sboudrias/Inquirer.js</a></li>
</ul>
<h2 id="installation">
Installation</h2>
<pre><code>npm install inquirer
</code></pre>
<h2 id="sample-usae">
Sample Usae</h2>
怎麼使用inquirer呢?最簡單就是來個echo的程式,也就是執行後,你打什麼,他就回覆什麼 :D<br />
File: echo.js<br />
<pre><code>#!/usr/bin/env node
var inquirer = require('inquirer');
function it() {
inquirer.prompt([{
type: 'input',
name: 'cmd',
message: ' →',
}]).then(function (a) {
if(a.cmd == 'exit') process.exit(0);
console.log(a.cmd);
it();
});
}
it();
</code></pre>
執行狀況:<br />
<pre><code>$ node examples/inquirer/echo.js
? → hello
hello
? → Hello Simon!
Hello Simon!
? → exit
</code></pre>
上面的程式中,prompt的輸入參數為需要帶入互動模式的指令,可以指定input, confirm, list...等方法,也可以帶入一些判斷讓指令模式更加聰明。<br />
在then的callback中,所帶入的數值會以input裡面的name為名稱附加在input參數"a"中,例如上方input的name為cmd,則下方then callback的"a"所接收到的參數會帶入在a中...<br />
<pre><code>a = { "cmd":"your-input-text" }
</code></pre>
透過callback再呼叫原函式,則可以持續會到interactive模式,只有接收到'exit'時候會直接離開程式。<br />
<h2 id="reference">
Reference</h2>
<ul>
<li>操作說明:<a href="http://enzolutions.com/articles/2014/09/08/how-to-create-an-interactive-command-in-node-js/" target="_blank">http://enzolutions.com/articles/2014/09/08/how-to-create-an-interactive-command-in-node-js/</a></li>
</ul>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-49120320936837863672016-02-01T21:58:00.000-08:002016-02-01T21:58:42.520-08:00用Node.js搭配Google Vision API與Canvas套件,做到人臉辨識功能!<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
Canvas是HTML5上處理2D影像的常用套件,而在node.js下,可以透過node-canvas來針對影像做一些特殊處理,例如:針對給定的座標畫各式幾何圖形、圖片縮圖等等。<br />
Google Vision API目前是Trust Preview的階段,如果有想要試用,可以在Google的官網(<a href="http://cloud.google.com/vision/">http://cloud.google.com/vision/</a>)申請唷!<span style="letter-spacing: 0.2px; line-height: 23.8px;">本篇還是以node-canvas的繪圖為主~ :D</span></div>
<h2 style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; letter-spacing: 0.2px; line-height: 1.1; margin-bottom: 0.85em; margin-top: 1.275em; orphans: 3; page-break-after: avoid; widows: 3;">
Github</h2>
<ul style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin: 0px 0px 0.85em; padding: 0px 0px 0px 2em;">
<li style="-webkit-user-select: text; box-sizing: border-box; cursor: text; font-size: inherit;"><a href="https://www.blogger.com/blogger.g?blogID=7753376764623667668#" style="-webkit-user-select: text; background: 0px 0px; box-sizing: border-box; color: #4183c4; cursor: text; font-size: inherit; text-decoration: none;">https://github.com/Automattic/node-canvas</a></li>
</ul>
<h2 style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; letter-spacing: 0.2px; line-height: 1.1; margin-bottom: 0.85em; margin-top: 1.275em; orphans: 3; page-break-after: avoid; widows: 3;">
Installation</h2>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; page-break-inside: avoid; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; page-break-inside: avoid;">npm install node-canvas
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
node-canvas是需要搭配canvas相關函式庫來啟動,相關的安裝方式可以透過官方的github位置來安裝相關套件,以Mac來說,安裝如下:</div>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; page-break-inside: avoid; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; page-break-inside: avoid;">$ brew install pkg-config cairo libpng jpeg giflib</code></pre>
<h2 style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; letter-spacing: 0.2px; line-height: 1.1; margin-bottom: 0.85em; margin-top: 1.275em; orphans: 3; page-break-after: avoid; widows: 3;">
範例程式</h2>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
這邊搭配google-vision-api-client套件來查詢Vision API做人臉偵測的動作,並在人的臉部做方型框框標示。</div>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; page-break-inside: avoid; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; page-break-inside: avoid;">#!/usr/bin/env node
var vision = require('google-vision-api-client');
var requtil = vision.requtil;
var request = require('request');
var Canvas = require('canvas')
, Image = Canvas.Image
, fs = require('fs')
, sizeOf = require('image-size');
var tmpfile = '/tmp/mytmpfile';
var canvas = new Canvas(200, 200)
, ctx = canvas.getContext('2d')
var jsonfile = '/Users/peihsinsu/.gcpkeys/itri-smart-home/itri-smart-home-33f5a755a360.json';
vision.init(jsonfile);
var outfile = process.argv[3] || '/tmp/test.jpg';
function getColor(i) {
var list = ['red', 'yellow', 'pink', 'green', 'blue'];
return list[i%list.length];
}
exports.draw = draw;
function draw(imgfile, outfile, opts, cb){
fs.readFile(imgfile, function(err, squid){
if (err) throw err;
img = new Image;
img.src = squid;
if (opts && opts.filter) {
opts.filter(ctx);
}
ctx.drawImage(img , 0, 0, img.width , img.height );
ctx.globalAlpha = .5;
var i = 0;
function doit(vec) {
var color = Math.random()*1000%255;
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(vec[0].x, vec[0].y);
ctx.lineTo(vec[1].x, vec[1].y);
ctx.lineTo(vec[2].x, vec[2].y);
ctx.lineTo(vec[3].x, vec[3].y);
ctx.closePath();
ctx.strokeStyle = getColor(i);//'rgb(255,color,0)';
i++;
ctx.stroke();
//ctx.fill();
}
opts.vec.forEach(function(v) {
doit(v)
});
fs.writeFileSync(outfile, canvas.toBuffer());
cb();
});
}
function main(imgfile) {
var d = requtil.createRequests().addRequest(
requtil.createRequest(imgfile)
.withFeature('FACE_DETECTION', 50)
//.withFeature('LABEL_DETECTION', 20)
.build());
var imgSize = sizeOf(imgfile);
console.log('Got the image size: %sx%s', imgSize.width, imgSize.height);
vision.query(d, function(e, r, d){
if(e) return console.log('ERROR:', e);
console.log(JSON.stringify(d));
if(!d.responses[0].faceAnnotations) return;
//var v = d.responses[0].faceAnnotations[0].boundingPoly.vertices;
var v = [];
d.responses[0].faceAnnotations.forEach(function(o){
v.push(o.boundingPoly.vertices);
})
console.log('-->', v);
canvas.width = imgSize.width;
canvas.height = imgSize.height;
draw(imgfile, outfile, {
vec: v,
filter: function(ctx) {
//ctx.strokeStyle = 'rgba(255,0,0,.5)';
}
},
function(err) {
if(err) console.log('ERROR:', err);
});
});
}
//Execute the process
if(process.argv[2].indexOf('http') == 0) { //from http resource
var url = process.argv[2];
var req = request.get(url);
req.pipe(fs.createWriteStream(tmpfile));
req.on('end', function(){
main(tmpfile);
});
} else { //from localhost
main(process.argv[2]);
}
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
上面程式碼儲存成test.js並給予執行權限後,即可測試...</div>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
測試http網站圖片:</div>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; page-break-inside: avoid; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; page-break-inside: avoid;">$ node test http://www.kevinparker.com.au/people/p7IGM_images/fullsize/shutterstock_48737587_fs.jpg
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
測試本地圖片:</div>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; page-break-inside: avoid; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; page-break-inside: avoid;">$ node test /Users/peihsinsu/Pictures/Simon/simon01.jpg
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
執行完後,會在/tmp/test.jpg產生圖片檔案</div>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; page-break-inside: avoid; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; page-break-inside: avoid;">$ open /tmp/test.jpg
</code></pre>
<h2 style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; letter-spacing: 0.2px; line-height: 1.1; margin-bottom: 0.85em; margin-top: 1.275em; orphans: 3; page-break-after: avoid; widows: 3;">
Notes</h2>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; orphans: 3; widows: 3;">
Install canvas: <a href="https://www.blogger.com/blogger.g?blogID=7753376764623667668#" style="-webkit-user-select: text; background: 0px 0px; box-sizing: border-box; color: #4183c4; cursor: text; font-size: inherit; text-decoration: none;">https://github.com/Automattic/node-canvas</a></div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-1182146739485014132016-02-01T21:57:00.003-08:002016-02-01T22:06:41.885-08:00Node.js搭配/dev/watchdog做監控Linux上的Watchdog是一個滿常用來監控系統的服務,可以透過掛載模組後產生硬體的watchdog裝置(/dev/watchdog),掛載方式可以參考本文最下面的鏈結。<br />
<br />
其中需要:<br />
1. 設定watchdog的module掛載<br />
2. 啟用module<br />
<br />
而在Node.js中需要連結/dev/watchdog的方法,是open該file後,持續的在所規定的時間內丟一個特定字串給它(/dev/watchdog),下面是node.js實作的方式:<br />
<br />
var fs = require('fs');<br />
var data = 0;<br />
var WDIOC_KEEPALIVE = 2147768069;<br />
var WDIOC_SETTIMEOUT = 3221509894;<br />
var WDIOC_GETTIMEOUT = 2147768071;<br />
<br />
var buf = new Buffer(4);<br />
<br />
fs.open('/dev/watchdog', 'r+', function(err, fd){<br />
if(err) console.log('error:', err);<br />
setInterval(function() {<br />
console.log('write file...');<br />
fs.write(fd, WDIOC_KEEPALIVE, buf, function(err){<br />
if(err) console.log('error:', err);<br />
})<br />
}, 2000);<br />
});<br />
<br />
當程式執行起來後,將會每2秒鐘到該裝置寫一塊資料,當成是關閉後,則會觸動系統重新開機的動作... 這個用途在當系統發生問題時候,需要重新開機時,非常有用唷 :D<br />
<br />
參考:http://www.switchdoc.com/2014/11/reliable-projects-using-internal-watchdog-timer-raspberry-pi/k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-48720414507608621902015-12-11T22:16:00.002-08:002015-12-11T22:16:40.238-08:00用Node.js玩OpenCV<h1 style="-webkit-user-select: text; box-sizing: border-box; cursor: text; margin-bottom: 0.85em; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; page-break-after: avoid;">
<span style="background-color: white; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-weight: normal; letter-spacing: 0.2px; line-height: 23.8px; orphans: 3; widows: 3;">OpenCV是一套有名的影像處理工具,透過node.js來搭配OpenCV可以延伸在node.js在影像相關範圍的應用,例如人臉辨識之類的...</span></h1>
<h2 style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; letter-spacing: 0.2px; line-height: 1.1; margin-bottom: 0.85em; margin-top: 1.275em; orphans: 3; page-break-after: avoid; widows: 3;">
Installation</h2>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; page-break-inside: avoid; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; page-break-inside: avoid;">npm install node-opencv
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
在安裝node-opencv之前,需要先完成opencv套件的安裝,可以參考node-opencv來做相關安裝。我在Mac上是這樣裝的:</div>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; page-break-inside: avoid; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; page-break-inside: avoid;">brew tap homebrew/science
brew install opencv
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
參考網站:<a href="https://www.blogger.com/blogger.g?blogID=7753376764623667668#" style="-webkit-user-select: text; background: 0px 0px; box-sizing: border-box; color: #4183c4; cursor: text; font-size: inherit; text-decoration: none;">http://www.learnopencv.com/install-opencv-3-on-yosemite-osx-10-10-x/</a></div>
<h2 style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; letter-spacing: 0.2px; line-height: 1.1; margin-bottom: 0.85em; margin-top: 1.275em; orphans: 3; page-break-after: avoid; widows: 3;">
範例程式</h2>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
下面是一個簡單的範例程式,可以抓http資源位置或是本地端位置的圖片來做人臉的註記(在人臉不用橢圓形標記起來)</div>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; page-break-inside: avoid; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; page-break-inside: avoid;">#!/usr/bin/env node
var cv = require('opencv');
var request = require('request');
var fs = require('fs');
var tmpfile = '/tmp/tmpgile';
function detect(imgfile) {
cv.readImage(imgfile, function(err, im){
im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){
for (var i=0;i<faces.length; i++){
var x = faces[i]
console.log('>>', x); //Will see something like: { x: 336, y: 1359, width: 42, height: 42 }
im.ellipse(x.x + x.width/2, x.y + x.height/2, x.width/2, x.height/2);
}
im.save('/tmp/test.jpg');
});
})
}
//Execute the process
if(process.argv[2].indexOf('http') == 0) { //from http resource
var url = process.argv[2];
var req = request.get(url);
req.pipe(fs.createWriteStream(tmpfile));
req.on('end', function(){
detect(tmpfile);
});
} else { //from localhost
detect(process.argv[2]);
}
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
上面程式存成:face-opencv.js,並給予執行的權限:</div>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; page-break-inside: avoid; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; page-break-inside: avoid;">$ ./face-opencv.js http://www.pngdot.com/wp-content/uploads/2015/10/People_Png_Transparent_01.png
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; margin-bottom: 0.85em; orphans: 3; widows: 3;">
回覆的結果:</div>
<pre style="-webkit-user-select: text; background: rgb(247, 247, 247); border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; letter-spacing: 0.2px; line-height: 1.42857; margin-bottom: 1.275em; overflow: auto; padding: 0.85em 1em; page-break-inside: avoid; word-break: break-all; word-wrap: normal;"><code style="-webkit-user-select: text; background: 0px 0px; border-radius: 0px; border: none; box-sizing: border-box; color: inherit; cursor: text; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 0.85em; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; page-break-inside: avoid;">>> { x: 336, y: 1359, width: 42, height: 42 }
>> { x: 322, y: 137, width: 115, height: 115 }
>> { x: 1255, y: 188, width: 109, height: 109 }
>> { x: 1483, y: 93, width: 125, height: 125 }
>> { x: 584, y: 152, width: 111, height: 111 }
>> { x: 933, y: 156, width: 120, height: 120 }
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; letter-spacing: 0.2px; line-height: 23.8px; orphans: 3; widows: 3;">
回覆的部分,還會包含/tmp/test.jpg的輸出檔案,而console輸出的訊息代表每個人臉的中心位置以及範圍,透過這些資訊,可以做更多的應用唷!</div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-65776039303380714172015-08-29T22:41:00.002-07:002015-08-29T22:41:52.840-07:00Sails ORM簡介<h1 id="sails-orm" style="-webkit-user-select: text; background-color: white; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 2.25em; line-height: 1.2; margin-bottom: 16px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; padding-bottom: 0.3em; position: relative;">
Sails ORM</h1>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
Sails除了API的概念外,他也可以快速地提供Object Relational Model(ORM)的對應,只要透過下面幾個步驟:</div>
<h2 id="%" style="-webkit-user-select: text; background-color: white; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
說明</h2>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
Sails的ORM對應是透過幾個部分做設定就可以完成:</div>
<ul style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px; margin-top: 0px; padding: 0px 0px 0px 2em;">
<li style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">config/connection.js:設定所要連線的資料庫位置,裡面會有許多的Adaptor的設定,可供後續選用。</li>
<li style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">config/models.js:設定預設的資料庫模組使用哪個Adaptor。</li>
<li style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">api/models/[Object Name].js:透過"sails generate api [Object Name]"所產生的ORM物件,裡面可以再去客製化所擁有的欄位屬性,以結合未來資料輸入的驗證階段使用。</li>
</ul>
<h2 id="%" style="-webkit-user-select: text; background-color: white; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
以MySQL資料庫為例</h2>
<h3 id="%" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
準備</h3>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
在使用之前,我們先透過下面指令啟動Docker的MySQL服務...(關於Docker部分,可以參考:<a href="https://www.gitbook.com/book/peihsinsu/docker-note-book/details" style="-webkit-user-select: text; background: 0px 0px; box-sizing: border-box; color: #4183c4; cursor: text; text-decoration: none;" target="_blank">https://www.gitbook.com/book/peihsinsu/docker-note-book/details</a>)</div>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs stata" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;">docker <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">run</span> --name mysql -p 3306:3306 -<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">e</span> MYSQL_ROOT_PASSWORD=password -<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">d</span> mysql
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
啟動無誤後,即可在本機使用MySQL服務。(由於一般Mac或Windows是透過boot2docker來啟用docker,所以在連線時候,需要知道Docker Host的位置作為連線使用)。</div>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs php" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;">$ <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">echo</span> <span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">$DOCKER_HOST</span>
tcp:<span class="hljs-comment" style="-webkit-user-select: text; box-sizing: border-box; color: #8e908c; cursor: text;">//192.168.59.103:2376</span>
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
上面是Docker Host的回覆訊息,其中192.168.59.103即是Docker的連線位置。</div>
<h3 id="step1-" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
Step1: 安裝相關的資料庫模組</h3>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
在config/connection.js中設定所要連接的MySQL connection位置,設定大致如下:</div>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs gherkin" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;">module.exports.connections = {
... (skip)
/<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span>
<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span> <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span>
<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span> MySQL is the world's most popular relational database. <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span>
<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span> http://en.wikipedia.org/wiki/MySQL <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span>
<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span> <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span>
<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span> Run: npm install sails-mysql <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span>
<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span> <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span>
<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">*</span>/
myMysqlServer: {
adapter: 'sails-mysql',
host: '192.168.59.103',
user: 'root',
password: 'password',
database: 'mydb'
},
...(skip)
</code></pre>
<h3 id="step2-" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
Step2: 設定預設使用的模組</h3>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs typescript" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;"><span class="hljs-module" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;"><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">module</span>.exports.models = </span>{
connection: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">'myMysqlServer'</span>
};
</code></pre>
<h3 id="step3-" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
Step3: 安裝sails mysql plugin</h3>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs elixir" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;"><span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">$ </span>cd <span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">$PROJECT_FOLDER</span>
<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">$ </span>npm install sails-mysql --save
sails-mysql<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">@0</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">11.0</span> node_modules/sails-mysql
├── waterline-errors<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">@0</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">10.1</span>
├── async<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">@1</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">3.0</span>
├── waterline-sequel<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">@0</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">5.0</span>
├── waterline-cursor<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">@0</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">0</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">5</span> (async<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">@0</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">9.2</span>, lodash<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">@2</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">4.2</span>)
├── lodash<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">@3</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">10.1</span>
└── mysql<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">@2</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">8.0</span> (bignumber.js<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">@2</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">0</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">7</span>, readable-stream<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">@1</span>.<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">1.13</span>)
</code></pre>
<h3 id="step4-" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
Step4: 建立ORM</h3>
<h4 id="%" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.25em; line-height: 1.4; margin-bottom: 16px; margin-top: 1em; position: relative;">
建立User module</h4>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs dockerfile" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;">$ sails generate api <span class="hljs-built_ins" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">user</span>
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
修改user module的欄位</div>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs scala" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;">module.exports = {
adapter: <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'myMysqlServe</span>r',
migrate: <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'saf</span>e',
id: {
primaryKey: <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">true</span>,
<span class="hljs-class" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;"><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">type</span>:</span> <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'strin</span>g'
},
attributes: {
name: {
<span class="hljs-class" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;"><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">type</span>:</span> <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'strin</span>g'
},
age: {
<span class="hljs-class" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;"><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">type</span>:</span> <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'floa</span>t',
required: <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">true</span>
},
password: {
<span class="hljs-class" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;"><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">type</span>:</span> <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'strin</span>g',
required: <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">true</span>
}
}
}
</code></pre>
<h4 id="%" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.25em; line-height: 1.4; margin-bottom: 16px; margin-top: 1em; position: relative;">
建立資料庫與對應欄位</h4>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs sql" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;"><span class="hljs-operator" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;"><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">CREATE</span> <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">TABLE</span> <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">`user`</span> (
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">`id`</span> <span class="hljs-built_in" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">int</span>(<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">11</span>) unsigned <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">NOT</span> <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">NULL</span> AUTO_INCREMENT,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">`name`</span> <span class="hljs-built_in" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">varchar</span>(<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">50</span>) <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">DEFAULT</span> <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">NULL</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">`age`</span> <span class="hljs-built_in" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">int</span>(<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">11</span>) <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">DEFAULT</span> <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">NULL</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">`password`</span> <span class="hljs-built_in" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">varchar</span>(<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">200</span>) <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">DEFAULT</span> <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">NULL</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">`createdAt`</span> datetime <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">DEFAULT</span> <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">NULL</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">`updatedAt`</span> datetime <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">DEFAULT</span> <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">NULL</span>,
<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">PRIMARY</span> <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">KEY</span> (<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">`id`</span>)
) <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">ENGINE</span>=<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">InnoDB</span> AUTO_INCREMENT=<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">3</span> <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">DEFAULT</span> <span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">CHARSET</span>=latin1;</span>
</code></pre>
<h3 id="step5-" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
Step5: 啟動並測試</h3>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
透過Sails的RESTful API定義,要Insert一筆User資料到Sails ORM的對應資料庫中,可以透過下面POST method的方法:</div>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs bash" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;">curl <span class="hljs-operator" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">-s</span>S http://localhost:<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">1337</span>/user/create <span class="hljs-operator" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">-d</span> name=simon <span class="hljs-operator" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">-d</span> age=<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">36</span> <span class="hljs-operator" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">-d</span> password=<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">123</span> <span class="hljs-operator" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">-d</span> id=<span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">123</span> -X POST | json
{
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"name"</span>: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"simon"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"age"</span>: <span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">36</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"password"</span>: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"123"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"id"</span>: <span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">123</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"createdAt"</span>: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"2015-08-30T05:04:02.291Z"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"updatedAt"</span>: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"2015-08-30T05:04:02.291Z"</span>
}
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
當Inser完,需要檢視資料的狀況,可以透過GET method取回該資料集的內容:</div>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs elixir" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;"><span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">$ </span>curl -sS <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">http:</span>/<span class="hljs-regexp" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">/localhost:1337/user</span> | json
[
{
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"name"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"simon"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"age"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">36</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"password"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"123"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"id"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">1</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"createdAt"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"2015-08-30T02:46:54.000Z"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"updatedAt"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"2015-08-30T02:46:54.000Z"</span>
},
{
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"name"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"simon"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"age"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">36</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"password"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"123"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"id"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">2</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"createdAt"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"2015-08-30T02:47:28.000Z"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"updatedAt"</span><span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">:</span> <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"2015-08-30T02:47:28.000Z"</span>
}
]
</code></pre>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
如果您知道該筆資料的ID,則可以透過下面方式給定:</div>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs groovy" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;">curl -sS <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">http:</span><span class="hljs-comment" style="-webkit-user-select: text; box-sizing: border-box; color: #8e908c; cursor: text;">//localhost:1337/user/123 | json</span>
{
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"name"</span>: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"simon"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"age"</span>: <span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">36</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"password"</span>: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"123"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"id"</span>: <span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">123</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"createdAt"</span>: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"2015-08-30T05:04:02.000Z"</span>,
<span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"updatedAt"</span>: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">"2015-08-30T05:04:02.000Z"</span>
}
</code></pre>
<h2 id="%" style="-webkit-user-select: text; background-color: white; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
以CouchDB資料庫為例</h2>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
Sails抽離了大部份的設定,而ORM connection的設定抽離讓未來切換到其他的資料源可以更方便。下面是從MySQL切換到CouchDB的方式:</div>
<h3 id="step1-" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
Step1: 安裝相關的資料庫模組</h3>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
在config/connection.js中設定所要連接的CouchDB connection位置,設定大致如下:</div>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs lasso" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;">module<span class="hljs-built_in" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">.</span>exports<span class="hljs-built_in" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">.</span>connections <span class="hljs-subst" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">=</span> {
<span class="hljs-attribute" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">...</span> (<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">skip</span>)
couch: {
adapter: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">'sails-couchdb-orm'</span>,
host: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">'192.168.59.103'</span>,
port: <span class="hljs-number" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">5984</span>,
username: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">'admin'</span>,
password: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">'adminadmin'</span>
},
<span class="hljs-attribute" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">...</span>(<span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">skip</span>)
</code></pre>
<h3 id="step2-" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
Step2: 設定預設使用的模組</h3>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs typescript" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;"><span class="hljs-module" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;"><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">module</span>.exports.models = </span>{
connection: <span class="hljs-string" style="-webkit-user-select: text; box-sizing: border-box; color: #718c00; cursor: text;">'couch'</span>
};
</code></pre>
<h3 id="step3-" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
Step3: 安裝sails couchdb plugin</h3>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs elixir" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;"><span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">$ </span>cd <span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">$PROJECT_FOLDER</span>
<span class="hljs-variable" style="-webkit-user-select: text; box-sizing: border-box; color: #c82829; cursor: text;">$ </span>npm install sails-couchdb-orm --save
</code></pre>
<h3 id="step4-" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
Step4: 建立ORM</h3>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
在CouchDB中,由於是屬於NoSQL的性質,我們不需要是先建立資料庫以及對應的Schema。而ORM的設定檔案部分,直接依據上面所設定的部分即可:</div>
<pre style="-webkit-user-select: text; background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; cursor: text; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; word-break: break-all; word-wrap: normal;"><code class="hljs scala" style="-webkit-user-select: text; background: 0px 0px transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: #4d4d4c; cursor: text; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; word-wrap: normal;">module.exports = {
adapter: <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'couc</span>h',
migrate: <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'saf</span>e',
id: {
primaryKey: <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">true</span>,
<span class="hljs-class" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;"><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">type</span>:</span> <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'strin</span>g'
},
attributes: {
name: {
<span class="hljs-class" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;"><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">type</span>:</span> <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'strin</span>g'
},
age: {
<span class="hljs-class" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;"><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">type</span>:</span> <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'floa</span>t',
required: <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">true</span>
},
password: {
<span class="hljs-class" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;"><span class="hljs-keyword" style="-webkit-user-select: text; box-sizing: border-box; color: #8959a8; cursor: text;">type</span>:</span> <span class="hljs-symbol" style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">'strin</span>g',
required: <span class="hljs-literal" style="-webkit-user-select: text; box-sizing: border-box; color: #f5871f; cursor: text;">true</span>
}
}
}
</code></pre>
<h3 id="step5-" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
Step5: 啟動並測試</h3>
<div style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 16px;">
整體設定完成後,直接依照MySQL部分的啟動測試程序執行,應該就可以在CouchDB中看到對應的資料庫物件與輸入的內容。</div>
<h3 id="%" style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
其他參考資訊</h3>
<ul style="-webkit-user-select: text; background-color: white; box-sizing: border-box; color: #333333; cursor: text; font-family: Raleway, HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 22.4px; margin-bottom: 0px !important; margin-top: 0px; padding: 0px 0px 0px 2em;">
<li style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">Sails模組設定:<a href="http://sailsjs.org/documentation/concepts/models-and-orm/models" style="-webkit-user-select: text; background: 0px 0px; box-sizing: border-box; color: #4183c4; cursor: text; text-decoration: none;" target="_blank">http://sailsjs.org/documentation/concepts/models-and-orm/models</a></li>
<li style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">Sails MySQL外掛模組:<a href="https://github.com/balderdashy/sails-mysql" style="-webkit-user-select: text; background: 0px 0px; box-sizing: border-box; color: #4183c4; cursor: text; text-decoration: none;" target="_blank">https://github.com/balderdashy/sails-mysql</a></li>
<li style="-webkit-user-select: text; box-sizing: border-box; cursor: text;">CouchDB Sails外掛模組:<a href="https://github.com/codeswarm/sails-couchdb-orm" style="-webkit-user-select: text; background: 0px 0px; box-sizing: border-box; color: #4183c4; cursor: text; text-decoration: none;" target="_blank">https://github.com/codeswarm/sails-couchdb-orm</a></li>
</ul>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-18729744451591157172015-08-16T05:41:00.001-07:002015-08-16T05:41:14.132-07:00Node.js的強大Web Framework - SailsJS<h1 id="sails" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 2.25em; line-height: 1.2; margin-bottom: 16px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; padding-bottom: 0.3em; position: relative;">
sails</h1>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
sails是一套功能強大的web framework,他有express的template generate功能,也具備restful api的產生功能,更可以跟後端的資料庫進行restful api與orm對應整合...</div>
<h2 id="github-repository" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
Github repository</h2>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
Github: <a href="https://github.com/balderdashy/sails/" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; box-sizing: border-box; color: #4183c4; text-decoration: none;" target="_blank">https://github.com/balderdashy/sails/</a> 官網:<a href="http://sailsjs.org/" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; box-sizing: border-box; color: #4183c4; text-decoration: none;" target="_blank">http://sailsjs.org/</a> 文件:<a href="http://sailsjs.org/documentation/concepts/" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; box-sizing: border-box; color: #4183c4; text-decoration: none;" target="_blank">http://sailsjs.org/documentation/concepts/</a></div>
<h2 id="installation" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
Installation</h2>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">sudo npm -g install sails
</code></pre>
<h2 id="sample-usage" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
Sample Usage</h2>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
Sails的指令語法與express相仿,下面介紹他的基本使用:</div>
<h3 id="%E5%BB%BA%E7%AB%8B%E6%96%B0%E5%B0%88%E6%A1%88" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
建立新專案</h3>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">sails new [project name]
</code></pre>
<h3 id="%E5%95%9F%E5%8B%95%E5%B0%88%E6%A1%88" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
啟動專案</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
一般express的專案會需要在建立完之後做"npm install"的套件安裝動作,但在sails中,他會直接用symbolic link將套件鏈結到sails已經有的套件位置,讓整個安裝與啟動過程可以更加快速。</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">cd $project
sails lift
</code></pre>
<h2 id="%E4%BD%BF%E7%94%A8auto-generate%E7%9A%84restful-api" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
使用auto generate的restful api</h2>
<h3 id="%E5%BB%BA%E7%AB%8B%E4%B8%80%E5%80%8Buser%E7%89%A9%E4%BB%B6" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
建立一個user物件</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
在該application建立user物件api:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">sails generate api user
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
執行呼叫建立user物件:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">curl http://localhost:1337/user/create
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
如果一切無誤,會看到下面的回覆:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">{"createdAt":"2015-08-15T17:28:53.366Z","updatedAt":"2015-08-15T17:28:53.366Z","id":1}
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
查詢所有的user列表:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">curl http://localhost:1337/user/find
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
回覆如下:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">[{"createdAt":"2015-08-15T17:28:53.366Z","updatedAt":"2015-08-15T17:28:53.366Z","id":1}]
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
查詢id=1的user</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">curl http://localhost:1337/user/1
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
如果該id可以查得到對應的物件,則會就該物件資訊回覆</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">{"createdAt":"2015-08-15T17:28:53.366Z","updatedAt":"2015-08-15T17:28:53.366Z","id":1}
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
而針對查詢不到的id,則會有以下訊息:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">$ curl http://localhost:1337/user/3
No record found with the specified `id`.
</code></pre>
<h2 id="%E9%96%8B%E7%99%BC%E4%B8%80%E5%80%8B%E9%A0%81%E9%9D%A2" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
開發一個頁面</h2>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
對照express的使用方式,express是直接在app.js中建立routing(ex: app.get('/api/:id', ...))或是express4中,在routing資料夾中建立對應的routing然後再link回app.js,並且對應頁面樣板到views資料夾下面。 而sails中,則是在config/routes.js中定義routing,而所指定的頁面樣板,一樣指定到views資料夾下。</div>
<h3 id="%E5%9C%A8express%E4%B8%AD" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
在Express中</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
Express - app.js</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">app.get('/test/:data', function(req, res, next){
res.render('result', {data:'api data:' + req.params.data});
})
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
Express - views/result.ejs</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"><%- data %>
</code></pre>
<h3 id="%E5%9C%A8sails%E4%B8%AD" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em; position: relative;">
在Sails中</h3>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
Sails - config/routes.js</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">module.exports.routes = {
'/test/:data':
{ controller: 'Result', action: 'hello' }
}
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
這邊需要注意,在我們指定controller的部分,Result是一個short name,實際建立檔案時候需要用ResultController.js,Sails才找得到這個檔案...,除了這樣寫,也可以用下面的方式:</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
直接使用字串方式,用short name呈現:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">'/test/:data': 'Result.hello'
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
直接使用字串方式,用full name呈現:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">'/test/:data': 'ResultController.hello'
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
或是透過json方式並以玩逞名稱定義:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">'/test/:data':
{ controller: 'ResultController', action: 'hello' }
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
另外,可以在routing前面指定request method:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">'POST /test/:data': 'Result.hello'
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
或是用萬用字元處理:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">'POST /test/*': 'Result.hello'
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
Sails - api/controllers/ResultController.js</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">module.exports = {
hello: function(req, res) {
return res.send(JSON.stringify({data: '123'}));
}
}
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
除果希望抓到routing所傳入的參數,可以透過req.params['param_name']的方式來抓:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">module.exports = {
hello: function(req, res) {
return res.send(JSON.stringify({data: req.params.data}));
}
}
</code></pre>
<h2 id="%E8%B3%87%E6%96%99%E8%88%87%E9%A0%81%E9%9D%A2%E6%A8%A3%E6%9D%BF%E6%95%B4%E5%90%88" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
資料與頁面樣板整合</h2>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
一般我們會在Controller中去存取資料庫資料,然後再帶到頁面去做rendering,在Sails可以這樣做:</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
Sails - api/controllers/ResultController.js</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">module.exports = {
hello: function(req, res) {
var data = req.params.data;
res.view('result', {data: data});
}
}
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
其中res.view()的第一個參數是頁面樣板的名稱,通常我們放在views資料夾中;第二個參數就是我們帶入的資料...</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
Sails - views/result.js</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px;">
頁面的資料存取,則以ejs的語法即可,例如上面傳入的json中有個data,這邊就可以直接用data來接值...</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">The data is: <%- data %>
</code></pre>
<h2 id="%E5%8F%83%E8%80%83" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
參考</h2>
<ul style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 0px !important; margin-top: 0px; padding: 0px 0px 0px 2em;">
<li style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box;">關於Routing客製化設定: <a href="http://sailsjs.org/documentation/concepts/routes/custom-routes" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; box-sizing: border-box; color: #4183c4; text-decoration: none;" target="_blank">http://sailsjs.org/documentation/concepts/routes/custom-routes</a></li>
<li style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box;">關於Controller: <a href="http://sailsjs.org/documentation/concepts/controllers" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; box-sizing: border-box; color: #4183c4; text-decoration: none;" target="_blank">http://sailsjs.org/documentation/concepts/controllers</a></li>
</ul>
<div>
<span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"><br /></span></span></div>
<div>
<span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;">文章出處:<a href="https://www.gitbook.com/content/book/peihsinsu/node-js-500-samples/mdfiles/sails.html">https://www.gitbook.com/content/book/peihsinsu/node-js-500-samples/mdfiles/sails.html</a></span></span></div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-41417609284568668262015-08-15T10:07:00.002-07:002015-08-15T10:08:34.210-07:00Node.js的RDBMS ORM利器 - sequelize<h1 id="sequelize" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 2.25em; line-height: 1.2; margin-bottom: 16px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; padding-bottom: 0.3em; position: relative;">
sequelize</h1>
<h1 id="sequelize" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 2.25em; line-height: 1.2; margin-bottom: 16px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; padding-bottom: 0.3em; position: relative;">
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
sequelize不是DB連線的工具,但它可以幫助你跟所有資料庫做好orm的溝通,可以說是屬於node.js的hibernate套件,透過很完整的物件規範,讓每次的資料輸入可以直接與資料庫對應。</div>
</h1>
<h2 id="github-repository" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
Github repository</h2>
<h1 style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 2.25em; line-height: 1.2; margin-bottom: 16px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; padding-bottom: 0.3em; position: relative;">
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
<a href="https://github.com/sequelize/sequelize.git" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; box-sizing: border-box; color: #4183c4; text-decoration: none;" target="_blank">https://github.com/sequelize/sequelize.git</a></div>
</h1>
<h2 id="%E5%AE%98%E6%96%B9%E6%96%87%E4%BB%B6" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
官方文件</h2>
<h1 style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 2.25em; line-height: 1.2; margin-bottom: 16px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; padding-bottom: 0.3em; position: relative;">
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
<a href="http://docs.sequelizejs.com/en/latest/docs/getting-started/" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; box-sizing: border-box; color: #4183c4; text-decoration: none;" target="_blank">http://docs.sequelizejs.com/en/latest/docs/getting-started/</a></div>
</h1>
<h2 id="installation" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
Installation</h2>
<h1 style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 2.25em; line-height: 1.2; margin-bottom: 16px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; padding-bottom: 0.3em; position: relative;">
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; font-weight: normal; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">npm install sequlize --save
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
在與資料庫連線使用時,需要搭配各個資料庫的模組,官方建議有下面幾種資料庫的搭配套件:</div>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
postgresql的套件:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; font-weight: normal; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">$ npm install --save pg pg-hstore
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
mysql的套件:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; font-weight: normal; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">$ npm install --save mysql // For both mysql and mariadb dialects
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
sqlite3的套件:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; font-weight: normal; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">$ npm install --save sqlite3
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
mssql的套件:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; font-weight: normal; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">$ npm install --save tedious // MSSQL
</code></pre>
</h1>
<h2 id="sample" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em; position: relative;">
Sample</h2>
<h1 style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; border-bottom-color: rgb(238, 238, 238); border-bottom-style: solid; border-bottom-width: 1px; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 2.25em; line-height: 1.2; margin-bottom: 16px; margin-left: 0px; margin-right: 0px; margin-top: 0px !important; padding-bottom: 0.3em; position: relative;">
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
在測試前,我們需要準備一個資料庫,下面以mysql資料庫維利,我們透過docker來啟動mysql</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; font-weight: normal; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
接下來建立所有連線的資料庫,我們這邊建立一個test資料庫</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; font-weight: normal; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">create database test;
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
建立DB之後,下面的程式可以用來連線,並且建立:</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; font-weight: normal; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">var Sequelize = require('sequelize')
//初始化sequelize,這邊指定docker mysql的位置
//(我是用boot2docker,所以在本地需要指定給boot2docker的位置)
var sequelize = new Sequelize('test', 'root', 'password', {
host: '192.168.59.103',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}});
//建立物件模型
var User = sequelize.define('user', {
firstName: {
type: Sequelize.STRING,
field: 'first_name'
},
lastName: {
type: Sequelize.STRING
}
}, {
freezeTableName: true
});
//實作資料新增
User.sync({force: true}).then(function () {
return User.create({
firstName: 'John',
lastName: 'Hancock'
});
});
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px; margin-bottom: 16px;">
其中,User.sync()中使用{force: true}會讓城市每次執行的時候強制刪除資料庫,並且重新建立資料庫。這比較適合在測試環境中執行,而正式環境,則需要把force拿掉...</div>
<pre style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-color: #f7f7f7; border-radius: 3px; border: 0px; box-sizing: border-box; color: #333333; direction: ltr; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; font-weight: normal; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; direction: ltr; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">User.sync().then(function () {
return User.create({
firstName: 'John',
lastName: 'Hancock'
});
});
</code></pre>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px;">
這部分可以參考:<a href="http://docs.sequelizejs.com/en/latest/docs/models-definition/#database-synchronization" style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; box-sizing: border-box; color: #4183c4; text-decoration: none;" target="_blank">http://docs.sequelizejs.com/en/latest/docs/models-definition/#database-synchronization</a></div>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px;">
<br /></div>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px;">
文章出處:<a href="https://www.gitbook.com/content/book/peihsinsu/node-js-500-samples/mdfiles/sequelize.html">https://www.gitbook.com/content/book/peihsinsu/node-js-500-samples/mdfiles/sequelize.html</a></div>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 25.6000003814697px;">
<br /></div>
</h1>
<div style="-webkit-font-smoothing: antialiased; -webkit-tap-highlight-color: transparent; box-sizing: border-box; color: #333333; font-family: 'Open Sans', 'Clear Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25.6000003814697px;">
</div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-90329247023521121082015-03-08T07:13:00.001-07:002015-03-10T00:32:27.259-07:00AppEngine Managed VM scale篇<div dir="ltr">
完成建置Managed VM後,接下來應該就開始煩惱在Managed VM上的Scale問題,在Managed VM中提供兩種Scale的方法:<br />
<div>
1. manual scale: 提供使用者自己管理VM scale的方式,系統會自動scale out,但是scale in則需要由管理人員自行處理。</div>
<div>
2. auto scale: 提供一系列threadshold參數,讓使用者管理scale的靈敏度,此時scale in則會自動進行ㄛ!<br />
<div>
<br /></div>
<div>
<div style="color: #333333; font-family: 'Open Sans',sans-serif; font-size: 15px; line-height: 21px; margin: 0px 0px 16px;">
Managed VM提供scale的方法,設定上很簡單,只要在app.yaml中指定所要擴充的最高數量在"manual_scaling"中即可:</div>
<pre style="background-color: #f7f7f7; border-radius: 3px; border: none; color: #333333; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 12.75px; line-height: 1.45; margin-bottom: 16px; margin-top: 0px; overflow: auto; padding: 16px; white-space: pre-wrap; word-break: break-all; word-wrap: normal;"><pre style="background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12.75px; line-height: 1.45; margin-bottom: 16px; overflow: auto; padding: 16px; white-space: pre-wrap; word-break: break-all; word-wrap: normal;"><code style="background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: inherit; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">version: 2
runtime: custom
vm: true
api_version: 1
manual_scaling:
instances: 5
handlers:
- url: /.*
script: app.js</code></pre>
</pre>
<div style="color: #333333; font-family: 'Open Sans',sans-serif; font-size: 15px; line-height: 21px; margin: 0px 0px 16px;">
在使用manual_scaling時候,主機的調降需要由user自己動作,而如果在系統的設計上已經有考慮到交易的安全性,那使用全自動的scale方式也相當方便:</div>
<pre style="background-color: #f7f7f7; border-radius: 3px; border: none; color: #333333; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 12.75px; line-height: 1.45; margin-bottom: 0px!important; margin-top: 0px; overflow: auto; padding: 16px; white-space: pre-wrap; word-break: break-all; word-wrap: normal;"></pre>
<pre style="background-color: #f7f7f7; border-radius: 3px; border: none; color: #333333; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 12.75px; line-height: 1.45; margin-bottom: 0px!important; margin-top: 0px; overflow: auto; padding: 16px; white-space: pre-wrap; word-break: break-all; word-wrap: normal;"><pre style="background-color: #f7f7f7; border-radius: 3px; border: none; box-sizing: border-box; color: #333333; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 12.75px; line-height: 1.45; overflow: auto; padding: 16px; white-space: pre-wrap; word-break: break-all; word-wrap: normal;"><code style="background: transparent; border-radius: 3px; border: 0px; box-sizing: border-box; color: inherit; display: inline; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">version: 2
runtime: custom
vm: true
api_version: 1
automatic_scaling:
min_num_instances: 2
max_num_instances: 20
cool_down_period_sec: 60
cpu_utilization:
target_utilization: 0.5
handlers:
- url: /.*
script: app.js</code></pre>
</pre>
<pre style="background-color: #f7f7f7; border-radius: 3px; border: none; color: #333333; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 12.75px; line-height: 1.45; margin-bottom: 0px!important; margin-top: 0px; overflow: auto; padding: 16px; white-space: pre-wrap; word-break: break-all; word-wrap: normal;">以上文章,已收錄到Gitbook:<a href="http://peihsinsu.gitbooks.io/node-js-500-samples/">http://peihsinsu.gitbooks.io/node-js-500-samples/</a></pre>
</div>
</div>
</div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-52981573343704800352015-03-04T08:12:00.001-08:002015-03-10T00:32:43.468-07:00Google AppEngine - Managed VM<div dir="ltr">
<h1 id="google-appengine---managed-vm" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 2.25em; line-height: 1.2; margin-bottom: 16px; margin-left: 0px; margin-right: 0px; margin-top: 0px!important; padding-bottom: 0.3em;">
Google AppEngine - Managed VM</h1>
<div>
From: <a href="https://www.gitbook.com/content/book/peihsinsu/node-js-500-samples/">https://www.gitbook.com/content/book/peihsinsu/node-js-500-samples/</a></div>
<div>
<br /></div>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
Managed VM是Google App Engine所推出的PaaS - AppEngine服務的延伸,透過Linux Container的技術與AppEngine frontend的搭配,讓整個AppEngine的功能無限延伸。</div>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
本服務仍在Beta階段,但是整體的服務概念已經大致完成,筆者也正在著手測試穩定性與可用性當中,有興趣的朋友可以一起參加Google的測試...</div>
<h2 id="managed-vm" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em;">
準備Managed VM環境</h2>
<h3 id="boot2docker" style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em;">
安裝boot2docker</h3>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
您可以到boot2docker的官方下載點,下載boot2docker以供安裝。安裝後,即可執行boot2docker的指令,並解僵boot2docker環境啟動...</div>
<div class="p1">
<span class="s1"><br /></span></div>
<blockquote class="tr_bq">
<span class="s1">boot2docker init</span><span class="s1">$ boot2docker up</span><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">$ $(boot2docker shellinit) </code></blockquote>
<h3 id="google-cloud-sdk" style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em;">
安裝google cloud sdk</h3>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
Mac環境下,透過curl即可下載安裝google cloud sdk...,其他環境可以參考Google官方說明</div>
<pre style="background-color: #f7f7f7; border-radius: 3px; border: 0px; color: #333333; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; margin-top: 0px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">curl <a href="https://sdk.cloud.google.com/">https://sdk.cloud.google.com</a> | bash </code></pre>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
安裝完cloud sdk之後,需要下載開發人員預覽版本的指令庫...</div>
<blockquote class="tr_bq">
<code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">gcloud components update app </code></blockquote>
<h2 id="---express" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em;">
準備開發 - 以express為範例</h2>
<h3 id="" style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em;">
安裝相關套件</h3>
<blockquote class="tr_bq">
<code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">$ sudo npm install express-generator -g </code></blockquote>
<h3 id="express" style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.5em; line-height: 1.43; margin-bottom: 16px; margin-top: 1em;">
建置express專案</h3>
<blockquote class="tr_bq">
<code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">$ express -e myapp</code> </blockquote>
<blockquote class="tr_bq">
$ cd myapp</blockquote>
<h2 id="appyaml" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em;">
增加app.yaml設定檔</h2>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
app.yaml是AppEngine環境的設定檔,目的是提供frontend instance (appengine)與backend instance (managed vm)之間的呼叫介面,以下面設定為例,即是讓所有的路由均由app.js提供服務...</div>
<blockquote class="tr_bq">
<div class="p1">
<span class="s1">version: 2</span></div>
<div class="p1">
<span class="s1">runtime: custom</span></div>
<div class="p1">
<span class="s1">vm: true</span></div>
<div class="p1">
<span class="s1">api_version: 1</span></div>
<div class="p2">
<span class="s1"></span><br /></div>
<div class="p1">
<span class="s1">manual_scaling:</span></div>
<div class="p1">
<span class="s1"> instances: 1</span></div>
<div class="p2">
<span class="s1"></span><br /></div>
<div class="p1">
<span class="s1">handlers:</span></div>
<div class="p1">
<span class="s1">- url: /.*</span></div>
<br />
<div class="p1">
<span class="s1"> script: app.js</span></div>
</blockquote>
<div class="p2">
<span class="s1"></span></div>
<div class="p2">
<span class="s1"></span></div>
<h2 id="dockerfile" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em;">
建立"Dockerfile"檔案</h2>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
Dockerfile是作為未來部署Managed VM環境的依據,您可以依照希望執行的Backend狀態來庭整這個設定檔。在此,我們只有單純把docker賦予node.js的環境,因此僅僅使用Google所提供的base image即可...</div>
<blockquote class="tr_bq">
<code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">FROM google/nodejs-runtime</code></blockquote>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
如果對Docker有興趣,您可以到docker hub檢視Google node.js runtime的docker build file (<a href="https://registry.hub.docker.com/u/google/nodejs-runtime/dockerfile/" style="background: transparent; color: #4183c4; text-decoration: none;" target="_blank">參考</a>),可以了解實際Managed VM執行的環境與狀態...</div>
<h2 id="packagejsonappengine" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em;">
編輯package.json,引入appengine模組</h2>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
在package.json中加入appengine的dependency,該套件提供了與AppEngine整合的一些操作方法,讓應用程式得以與Google其他服務互動。</div>
<div style="margin-bottom: 16px; margin-top: 0px;">
<blockquote class="tr_bq">
<span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;">{</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "name": "test",</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "version": "0.0.0",</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "private": true,</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "scripts": {</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "start": "node ./bin/www"</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> },</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "dependencies": {</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "body-parser": "~1.12.0",</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "cookie-parser": "~1.3.4",</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "debug": "~2.1.1",</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "ejs": "~2.3.1",</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "express": "~4.12.0",</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "morgan": "~1.5.1",</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "serve-favicon": "~2.2.0",</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> "appengine" : "git://github.com/GoogleCloudPlatform/appengine-nodejs.git"</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;"> }</span></span><span style="color: #333333; font-family: Open Sans, Clear Sans, Helvetica Neue, Helvetica, Arial, sans-serif;"><span style="line-height: 25.6000003814697px;">}</span></span></blockquote>
</div>
<h2 id="express" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em;">
修改express設定檔</h2>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
加入appengine的import</div>
<blockquote class="tr_bq">
<code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">var appengine = require('appengine'); </code></blockquote>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
<br /></div>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
並且於app.use部分加入appengine middleware模組</div>
<blockquote class="tr_bq">
<code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">app.use(appengine.middleware.base); </code></blockquote>
<h2 id="---_ah" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em;">
建立系統預設路由 - /_ah/*</h2>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
加入Managed VM的health check routes</div>
<pre style="background-color: #f7f7f7; border-radius: 3px; border: 0px; color: #333333; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 16px; margin-top: 0px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><blockquote class="tr_bq">
<span class="s1">app.get('/_ah/health', function(req, res) {</span><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">
</code><br />
<div class="p1">
<code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"><span class="s1"> res.set('Content-Type', 'text/plain');</span></code></div>
<code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">
</code><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"></code>
<div class="p1">
<code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"><span class="s1"> res.send(200, 'ok');</span></code></div>
<code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">
</code><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"><div class="p1">
<span class="s1">});</span></div>
</code><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">
<div class="p1">
<span class="s1">app.get('/_ah/start', function(req, res) {</span></div>
</code><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"><div class="p1">
<span class="s1"> res.set('Content-Type', 'text/plain');</span></div>
</code><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"><div class="p1">
<span class="s1"> res.send(200, 'ok');</span></div>
</code><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"><div class="p1">
<span class="s1">});</span></div>
</code><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">
<div class="p1">
<span class="s1">app.get('/_ah/stop', function(req, res) {</span></div>
</code><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"><div class="p1">
<span class="s1"> res.set('Content-Type', 'text/plain');</span></div>
</code><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"><div class="p1">
<span class="s1"> res.send(200, 'ok');</span></div>
</code><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"><div class="p1">
<span class="s1"> process.exit();</span></div>
</code><code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;"><div class="p1">
<span class="s1">});</span></div>
</code> <code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">
</code></blockquote>
</pre>
<h2 id="port8080" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em;">
修改啟動port至8080</h2>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
開啟bin/www檔案,將預設port修改為8080<br />
<br />
<blockquote class="tr_bq">
var port = normalizePort(process.env.PORT || '8080');</blockquote>
</div>
<h2 id="" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em;">
預先測試</h2>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
在未佈署到docker環境錢,該專案可以在npm install安裝完相依套件後,使用npm start測試是否可以正常執行。</div>
<blockquote class="tr_bq">
<code style="background: transparent; border-radius: 3px; border: 0px; display: inline; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: inherit; margin: 0px; max-width: initial; overflow: initial; padding: 0px; white-space: pre; word-wrap: normal;">npm install && npm start</code></blockquote>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
可以如此測試是因為Docker build file中有指定docker instance開立起來後,是以npm start來喚起所要執行的專案... 因此我們可以使用這個方式來測試該專案到container中是否可以正常執行...</div>
<h2 id="managed-vm" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em;">
本地端執行Managed VM</h2>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
本地端執行Managed VM時,需要搭配boot2docker已經開啟的狀態下,切換到專案目錄中,執行下面指令:</div>
<blockquote class="tr_bq">
<div class="p1">
<span class="s1">$ gcloud preview app run .</span></div>
<div class="p1">
<span class="s1">Module [default] found in file [/private/tmp/test/app.yaml]</span></div>
<div class="p1">
<span class="s1">INFO: Looking for the Dockerfile in /private/tmp/test</span></div>
<div class="p1">
<span class="s1">INFO: Using Dockerfile found in /private/tmp/test</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,198 devappserver2.py:726] Skipping SDK update check.</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,293 api_server.py:172] Starting API server at: http://localhost:62335</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,365 containers.py:259] Building docker image managedvm-project.default.2 from /private/tmp/test/Dockerfile:</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,365 containers.py:261] -------------------- DOCKER BUILD --------------------</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,366 dispatcher.py:186] Starting module "default" running at: http://localhost:8080</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,370 admin_server.py:118] Starting admin server at: http://localhost:8000</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,873 containers.py:280] # Executing 3 build triggers</span></div>
<div class="p2">
<span class="s1"></span><br /></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,874 containers.py:280] Trigger 0, ADD package.json /app/</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,874 containers.py:280] Step 0 : ADD package.json /app/</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,879 containers.py:280] ---> Using cache</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,879 containers.py:280] Trigger 1, RUN npm install</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,879 containers.py:280] Step 0 : RUN npm install</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,917 containers.py:280] ---> Using cache</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,918 containers.py:280] Trigger 2, ADD . /app</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,918 containers.py:280] Step 0 : ADD . /app</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,937 containers.py:280] ---> Using cache</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,937 containers.py:280] ---> cd272a90bc21</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,938 containers.py:280] Successfully built cd272a90bc21</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,948 containers.py:292] --------------------------------------------------------</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,948 containers.py:304] Image managedvm-project.default.2 built, id = cd272a90bc21</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:04,948 containers.py:534] Creating container...</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:05,104 containers.py:560] Container ef5775aa5d26ade2d524dd44d0cf6a18fa6c5c7aa100ccb3f925e3af30e5b72a created.</span></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:06,407 module.py:1692] New instance for module "default" serving on:</span></div>
<div class="p1">
<span class="s1">http://localhost:8080</span></div>
<div class="p2">
<span class="s1"></span><br /></div>
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:06,435 module.py:737] default: "GET /_ah/start HTTP/1.1" 200 2</span></div>
<br />
<div class="p1">
<span class="s1">INFO 2015-03-04 15:53:06,435 health_check_service.py:101] Health checks starting for instance 0.</span></div>
</blockquote>
<div class="p2">
<span class="s1"></span></div>
<div class="p2">
<span class="s1"></span></div>
<h2 id="managed-vm" style="border-bottom-color: rgb(238,238,238); border-bottom-style: solid; border-bottom-width: 1px; color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 1.75em; line-height: 1.225; margin-bottom: 16px; margin-top: 1em; padding-bottom: 0.3em;">
佈署Managed VM</h2>
<div style="color: #333333; font-family: 'Open Sans','Clear Sans','Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 16px; line-height: 25.6000003814697px; margin-bottom: 16px; margin-top: 0px;">
如開發上一切都順利,接下來可以執行部屬的動作,透過docker preview app deploy來作佈署...</div>
<pre style="background-color: #f7f7f7; border-radius: 3px; border: 0px; color: #333333; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 0px!important; margin-top: 0px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"><blockquote class="tr_bq">
<span class="s1">$ gcloud preview app deploy .</span>
<br />
<div class="p1">
<span class="s1">11:53 PM Host: appengine.google.com</span></div>
<span class="s1">11:53 PM Host: appengine.google.com</span>
<br />
<div class="p1">
<span class="s1">{bucket: vm-containers.managedvm-project.appspot.com, path: /containers}</span></div>
<span class="s1">{bucket: vm-containers.managedvm-project.appspot.com, path: /containers}</span>
<br />
<div class="p2">
<span class="s1"></span>
</div>
<div class="p1">
<span class="s1">Updating module [default] from file [/private/tmp/test/app.yaml]</span></div>
<span class="s1">Updating module [default] from file [/private/tmp/test/app.yaml]</span>
<br />
<div class="p1">
<span class="s1">Pushing image to Google Cloud Storage...managedvm-project</span></div>
<span class="s1">Pushing image to Google Cloud Storage...managedvm-project</span>
<br />
<div class="p1">
<span class="s1">Sending image list</span></div>
<span class="s1">Sending image list</span>
<br />
<div class="p1">
<span class="s1">Pushing repository gcr.io/_m_managedvm-project/managedvm-project.default.2 (1 tags)</span></div>
<span class="s1">Pushing repository gcr.io/_m_managedvm-project/managedvm-project.default.2 (1 tags)</span>
<br />
<div class="p1">
<span class="s1">Image 541923dd11eb already pushed, skipping</span></div>
<span class="s1">Image 541923dd11eb already pushed, skipping</span>
<br />
<div class="p1">
<span class="s1">Image 11971b6377ef already pushed, skipping</span></div>
<span class="s1">Image 11971b6377ef already pushed, skipping</span>
<br />
<div class="p1">
<span class="s1">...(skip)</span></div>
<span class="s1">...(skip)</span>
<br />
<div class="p1">
<span class="s1">Image b62c4c483651 already pushed, skipping</span></div>
<span class="s1">Image b62c4c483651 already pushed, skipping</span>
<br />
<div class="p1">
<span class="s1">Pushing</span></div>
<span class="s1">Pushing</span>
<br />
<div class="p1">
<span class="s1">Buffering to disk: 2.56 kB</span></div>
<span class="s1">Buffering to disk: 2.56 kB</span>
<br />
<div class="p1">
<span class="s1">Image successfully pushed===================================>] 2.56 kB/2.56 kB 0s</span></div>
<span class="s1">Image successfully pushed===================================>] 2.56 kB/2.56 kB 0s</span>
<br />
<div class="p1">
<span class="s1">Pushing</span></div>
<span class="s1">Pushing</span>
<br />
<div class="p1">
<span class="s1">Buffering to disk: 9.834 MB</span></div>
<span class="s1">Buffering to disk: 9.834 MB</span>
<br />
<div class="p1">
<span class="s1">Image successfully pushed===================================>] 9.834 MB/9.834 MB 0ss</span></div>
<span class="s1">Image successfully pushed===================================>] 9.834 MB/9.834 MB 0ss</span>
<br />
<div class="p1">
<span class="s1">Pushing</span></div>
<span class="s1">Pushing</span>
<br />
<div class="p1">
<span class="s1">Buffering to disk: 2.629 MB</span></div>
<span class="s1">Buffering to disk: 2.629 MB</span>
<br />
<div class="p1">
<span class="s1">Image successfully pushed===================================>] 2.629 MB/2.629 MB 0ss1s</span></div>
<span class="s1">Image successfully pushed===================================>] 2.629 MB/2.629 MB 0ss1s</span>
<br />
<div class="p1">
<span class="s1">Pushing tag for rev [c7685fe2aad2] on {https://gcr.io/v1/repositories/_m_managedvm-project/managedvm-project.default.2/tags/latest}</span></div>
<span class="s1">Pushing tag for rev [c7685fe2aad2] on {https://gcr.io/v1/repositories/_m_managedvm-project/managedvm-project.default.2/tags/latest}</span>
<br />
<div class="p1">
<span class="s1">11:54 PM Host: appengine.google.com</span></div>
<span class="s1">11:54 PM Host: appengine.google.com</span>
<br />
<div class="p1">
<span class="s1">11:54 PM Application: managedvm-project (was: None); version: 2</span></div>
<span class="s1">11:54 PM Application: managedvm-project (was: None); version: 2</span>
<br />
<div class="p1">
<span class="s1">11:54 PM</span></div>
<span class="s1">11:54 PM</span>
<br />
<div class="p1">
<span class="s1">Starting update of app: managedvm-project, version: 2</span></div>
<span class="s1">Starting update of app: managedvm-project, version: 2</span>
<br />
<div class="p1">
<span class="s1">11:54 PM Getting current resource limits.managedvm-project</span></div>
<span class="s1">11:54 PM Getting current resource limits.managedvm-project</span>
<br />
<div class="p1">
<span class="s1">11:54 PM Scanning files on local disk.</span></div>
<span class="s1">11:54 PM Scanning files on local disk.</span>
<br />
<div class="p1">
<span class="s1">^@11:54 PM WARNING: Performance settings included in this update are being ignored because your application is not using the Modules feature. See the Modules documentation for more information. Python: https://developers.google.com/appengine/docs/python/modules/ Java: https://developers.google.com/appengine/docs/java/modules/ Go: https://developers.google.com/appengine/docs/go/modules/ PHP: https://developers.google.com/appengine/docs/php/modules/.</span></div>
<span class="s1">^@11:54 PM WARNING: Performance settings included in this update are being ignored because your application is not using the Modules feature. See the Modules documentation for more information. Python: https://developers.google.com/appengine/docs/python/modules/ Java: https://developers.google.com/appengine/docs/java/modules/ Go: https://developers.google.com/appengine/docs/go/modules/ PHP: https://developers.google.com/appengine/docs/php/modules/.</span>
<br />
<div class="p1">
<span class="s1">11:54 PM Cloning 291 application files.</span></div>
<span class="s1">11:54 PM Cloning 291 application files.</span>
<br />
<div class="p1">
<span class="s1">11:54 PM Uploading 51 files and blobs.</span></div>
<span class="s1">11:54 PM Uploading 51 files and blobs.</span>
<br />
<div class="p1">
<span class="s1">11:55 PM Uploaded 51 files and blobs.</span></div>
<span class="s1">11:55 PM Uploaded 51 files and blobs.</span>
<br />
<div class="p1">
<span class="s1">11:55 PM Starting deployment.</span></div>
<span class="s1">11:55 PM Starting deployment.</span>
<br />
<div class="p1">
<span class="s1">11:55 PM Checking if deployment succeeded.</span></div>
<span class="s1">11:55 PM Checking if deployment succeeded.</span>
<br />
<div class="p1">
<span class="s1">11:55 PM Deployment successful.</span></div>
<span class="s1">11:55 PM Deployment successful.</span>
<br />
<div class="p1">
<span class="s1">11:55 PM Checking if updated app version is serving.</span></div>
<span class="s1">11:55 PM Checking if updated app version is serving.</span>
<br />
<div class="p1">
<span class="s1">11:55 PM Will check again in 5 seconds.</span></div>
<span class="s1">11:55 PM Will check again in 5 seconds.</span>
<br />
<div class="p1">
<span class="s1">...(skip)</span></div>
<span class="s1">...(skip)</span>
<br />
<div class="p1">
<span class="s1">11:57 PM Checking if updated app version is serving.</span></div>
<span class="s1">11:57 PM Checking if updated app version is serving.</span>
<br />
<div class="p1">
<span class="s1">11:57 PM Enough VMs ready (1/1 ready).</span></div>
<span class="s1">11:57 PM Enough VMs ready (1/1 ready).</span>
<br />
<div class="p1">
<span class="s1">11:57 PM Completed update of app: managedvm-project, version: 2</span></div>
<span class="s1">11:57 PM Completed update of app: managedvm-project, version: 2</span>
</blockquote>
<pre style="background-color: #f7f7f7; border-radius: 3px; border: none; color: #333333; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 12.75px; line-height: 1.45; margin-bottom: 0px!important; margin-top: 0px; overflow: auto; padding: 16px; white-space: pre-wrap; word-break: break-all; word-wrap: normal;">以上文章,已收錄到Gitbook:<a href="http://peihsinsu.gitbooks.io/node-js-500-samples/">http://peihsinsu.gitbooks.io/node-js-500-samples/</a></pre>
</pre>
<pre style="background-color: #f7f7f7; border-radius: 3px; border: 0px; color: #333333; font-family: Consolas,'Liberation Mono',Menlo,Courier,monospace; font-size: 13.6000003814697px; line-height: 1.45; margin-bottom: 0px!important; margin-top: 0px; overflow: auto; padding: 16px; white-space: pre-wrap; word-wrap: normal;"></pre>
</div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-84366653660405412322014-11-22T19:30:00.001-08:002014-11-22T19:30:37.315-08:00mysql module support pool query<div dir="ltr">應該是新辦本提供的功能,之前沒看到...<br> <div><br></div><div><pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;word-break:normal;color:rgb(51,51,51);background-color:rgb(247,247,247)"><span class="" style="color:rgb(167,29,93)">var</span> mysql <span class="" style="color:rgb(167,29,93)">=</span> <span class="" style="color:rgb(0,134,179)">require</span>(<span class="" style="color:rgb(223,80,0)"><span class="">'</span>mysql<span class="">'</span></span>); <span class="" style="color:rgb(167,29,93)">var</span> pool <span class="" style="color:rgb(167,29,93)">=</span> mysql.createPool({ connectionLimit <span class="" style="color:rgb(167,29,93)">:</span> <span class="" style="color:rgb(0,134,179)">10</span>, host <span class="" style="color:rgb(167,29,93)">:</span> <span class="" style="color:rgb(223,80,0)"><span class="">'</span><a href="http://example.org">example.org</a><span class="">'</span></span>, user <span class="" style="color:rgb(167,29,93)">:</span> <span class="" style="color:rgb(223,80,0)"><span class="">'</span>bob<span class="">'</span></span>, password <span class="" style="color:rgb(167,29,93)">:</span> <span class="" style="color:rgb(223,80,0)"><span class="">'</span>secret<span class="">'</span></span> }); pool.query(<span class="" style="color:rgb(223,80,0)"><span class="">'</span>SELECT 1 + 1 AS solution<span class="">'</span></span>, <span class="" style="color:rgb(167,29,93)">function</span>(<span class="">err</span>, <span class="">rows</span>, <span class="">fields</span>) { <span class="" style="color:rgb(167,29,93)">if</span> (err) <span class="" style="color:rgb(167,29,93)">throw</span> err; <span class="" style="color:rgb(121,93,163)">console</span><span class="" style="color:rgb(0,134,179)">.log</span>(<span class="" style="color:rgb(223,80,0)"><span class="">'</span>The solution is: <span class="">'</span></span>, rows[<span class="" style="color:rgb(0,134,179)">0</span>].solution); });</pre></div></div> k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-2974672945237331092014-07-16T17:07:00.001-07:002014-07-16T18:08:02.242-07:00同步你的前端資料與後端資料 - 自建object filter<div dir="ltr">
<div>
在Node.js/JavaScript中,因為弱型別加上沒有物件導向的概念</div>
<div>
常常在開發NoSQL儲存的時候會有許多資料不一致的問題</div>
<div>
下面是我常用的做法,給大家參考:</div>
<div>
<br /></div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
var _ = require('underscore'); </blockquote>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
//建立user model</blockquote>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
var user = {<br />
username: {<br />
type: "string", require: true, max: 200<br />
},<br />
password: {<br />
type: "string", require: true, max: 200<br />
},<br />
nickname: {<br />
type: "string", require: false, max: 200<br />
},<br />
phone:{<br />
type: "string", require: true, max: 200<br />
},<br />
lang: {<br />
type: "string", require: true, max: 200, pick: ['cht', 'en']<br />
},<br />
mobile:{<br />
type: "string", require: true, max: 200,<br />
map: function(v) {<br />
return "886" + Number(v); //format number<br />
}<br />
}<br />
}<br />
<br />
//建立通用的filter function<br />
function filter(vo, model) {<br />
var modelKeys = Object.keys(model);<br />
var out = {};<br />
for(var i = 0; i< modelKeys.length; i++) {<br />
var k = modelKeys[i];<br />
var v = model[k];<br />
//check required<br />
if(v.require)<br />
if(!vo[k]) throw "value of key [" + k + "] not found";<br />
<br />
//check vo has value<br />
if(Object.keys(vo).indexOf(k)>=0){<br />
out[k] = vo[k];<br />
if(v.type) {<br />
if(typeof(vo[k]) != v.type) throw "type of [" + k + "] is not correct"<br />
}<br />
//check max<br />
if(v.max)<br />
if(vo[k] && vo[k].length > v.max) throw "value of [" + k + "] over the column max size";<br />
<br />
//check value in the pick range<br />
if(v.pick && vo[k]) {<br />
if(_.indexOf(v.pick, vo[k]) < 0) throw "the value of [" + k + "] is not in the pick range"<br />
}<br />
//check execute map function<br />
if(v.map && vo[k])<br />
out[k] = v.map(vo[k]);<br />
}<br />
}<br />
return out;<br />
}</blockquote>
<div>
<br /></div>
<div>
透過上面設定的model的各項參數,讓filter function可以統一過濾所有的物件</div>
<div>
把該留的欄位留下來,也可以做到過濾內容等等的功能...</div>
<div>
<br /></div>
<div>
使用範例: </div>
<blockquote class="gmail_quote" style="border-left-color: rgb(204,204,204); border-left-style: solid; border-left-width: 1px; margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">
var vo = {<br />
"username": "<a href="mailto:xxx@gmail.com">xxx@gmail.com</a>",<br />
"password": "test111",<br />
"nickname": "xxx",<br />
"phone":"02234567",<br />
"mobile":"0912345678",<br />
"address":"台北市內湖區堤頂大道二段",<br />
"lang": "cht",<br />
"created": 1403063028138,<br />
"user_type": "0",<br />
"uuid": "0e7d6d4d6-e3a0b9ea9ca7"<br />
}<br />
console.log(filter(vo, user));</blockquote>
</div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-43058020015387876162014-05-03T00:44:00.001-07:002014-05-03T04:32:46.636-07:00Node.js / JavaScript處理含逗號的CSV格式字串<div dir="ltr">
這幾天遇到一個棘手的問題<br /> 我需要處理一個CSV-like的字串<br /> 字串是逗號隔開的CSV格式<br /> 數字部分不加單引號、其他格式使用單引號隔開...<br /> 而字串中欄位有可能出現逗號(,)或是跳脫的單引號(\')</div>
<div dir="ltr">
範例:<br /> 1,'aaa,bbb,ccc','asdf','a12\'3\'4'<br /> 2,'aab,bbb,ccc','asdf','b12\'3\'4'</div>
<div dir="ltr">
如果你跟我一樣,一開始就以Parser的角度,split後去處理文字....<br /> 或是想到Regular Expression去...<br /> 那應該頭痛一天也不會有答案...</div>
<div dir="ltr">
我最後的答案是...... (沒有看不懂的Regular Expression, 也沒有逐字去拆解分析...)</div>
<div dir="ltr">
file: data.csv<br /> > 1,'aaa,bbb,ccc','asdf','a12\'3\'4'</div>
<div dir="ltr">
<br /></div>
<div dir="ltr">
file: test.js<br /> > var fs = require('fs');<br /> > var data = fs.readFileSync('/tmp/data.csv', 'utf-8');<br /> > data.split('\n').forEach(function(v) {<br /> > if(v.length >0) {<br /> > console.log(parseArr(v));<br /> > }<br /> > });<br /> > function parseArr(v) {<br /> > <span style="color: red;"> eval('var arr = [' + v + ']');</span><br /> > return arr;<br /> > }</div>
<div dir="ltr">
關鍵是紅色的那行<br /> 透過javascript的eval方式將字串組織回來... </div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-66768585059088137432014-01-10T23:06:00.001-08:002014-01-10T23:06:46.865-08:00Node.js call by reference實驗<div dir="ltr"><div>操作node.js時候,針對變數操作call by value或reference的一些迷思...來證明一下...</div><div><br></div><div>新增加a與b物件,兩者賦予同樣的值,但是實際物件並不相等</div><div><div>$ node<br></div><div>> a={aaa:111,bbb:333}</div><div>{ aaa: 111, bbb: 333 }</div> <div>> b={bbb:333,aaa:111}</div><div>{ bbb: 333, aaa: 111 }</div><div>> a===b</div><div>false</div><div>> a==b</div><div>false</div><div>></div><div><br></div><div>物件內容給予string也是一樣...</div><div><div>> a={aaa:'111',bbb:'222'}</div> <div>{ aaa: '111', bbb: '222' }</div><div>> b={aaa:'111',bbb:'222'}</div><div>{ aaa: '111', bbb: '222' }</div><div>> a</div><div>{ aaa: '111', bbb: '222' }</div> <div>> b</div><div>{ aaa: '111', bbb: '222' }</div><div>> a==b</div><div>false</div><div>> a===b</div><div>false</div><div>></div></div><div><div dir="ltr"><div><span style="color:rgb(0,0,0);font-family:Arial,sans-serif;font-size:13px;line-height:18px"><br> </span></div><div><font color="#000000" face="Arial, sans-serif"><span style="line-height:18px">實際上物件是以call by reference存在...</span></font></div><div><font color="#000000" face="Arial, sans-serif"><span style="line-height:18px">因此如果使用b=a賦予b值,則兩個物件實際上是同一個...</span></font></div> <div><span style="color:rgb(0,0,0);font-family:Arial,sans-serif;font-size:13px;line-height:18px"><div>> a={aaa:111,bbb:222}</div><div>{ aaa: 111, bbb: 222 }</div><div>> b=a</div><div>{ aaa: 111, bbb: 222 }</div><div> > a==b</div><div>true</div><div>> a===b</div><div>true</div><div>></div><div><br></div><div>證明call by reference...</div><div>如果用b賦予a值,則當a改變時候</div><div>b也會跟著改變</div><div><div>> a=b</div><div>{ aaa: 333, bbb: 222 }</div> <div>> a</div><div>{ aaa: 333, bbb: 222 }</div><div>> b</div><div>{ aaa: 333, bbb: 222 }</div><div>> a.ccc=123</div><div>123</div><div>> a</div><div>{ aaa: 333, bbb: 222, ccc: 123 }</div><div>> b</div><div> { aaa: 333, bbb: 222, ccc: 123 }</div><div>> a==b</div><div>true</div><div>> a===b</div><div>true</div><div>></div></div></span></div></div></div></div></div> k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-27765856937051531892013-11-04T02:18:00.001-08:002013-11-04T02:22:41.714-08:00CouchDB session store : connect-couchdb<div dir="ltr">
connect-couchdb是一套Express的CouchDB session store,可以讓Express web server的session persistance在CouchDB中<br />
<h2 id="-" style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; line-height: 40px; margin: 10px 0px;">
<span style="font-size: medium;">套件資訊</span></h2>
<div class="" style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; line-height: 20px;">
<ul style="margin: 0px 0px 10px 25px; padding: 0px;">
<li>name : connect-couchdb</li>
<li style="font-size: 14px;">version : 0.4.1</li>
<li style="font-size: 14px;">author :<ul style="margin: 0px 0px 0px 25px; padding: 0px;">
<li>name : Thomas Debarochez</li>
<li>email : <a href="mailto:thomas.barochez%2Bnpm@gmail.com">thomas.barochez+npm@gmail.com</a></li>
</ul>
</li>
<li style="font-size: 14px;">repository :<ul style="margin: 0px 0px 0px 25px; padding: 0px;">
<li>type : git</li>
<li>url : <a href="http://github.com/tdebarochez/connect-couchdb.git" style="color: #0088cc; text-decoration: none;">http://github.com/tdebarochez/connect-couchdb.git</a></li>
</ul>
</li>
<li style="font-size: 14px;">dependencies :<ul style="margin: 0px 0px 0px 25px; padding: 0px;">
<li>connect : *</li>
<li>yacw : 0.2.x</li>
</ul>
</li>
<li style="font-size: 14px;">info : <a href="http://opennodes.arecord.us/node_modules/connect-couchdb/package.json" style="color: #0088cc; text-decoration: none;">參考自套件package.json</a></li>
<li style="font-size: 14px;">readme : <a href="http://opennodes.arecord.us/node_modules/connect-couchdb/README.md" style="color: #0088cc; text-decoration: none;">README.md</a></li>
</ul>
</div>
<h2 id="installation" style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; line-height: 40px; margin: 10px 0px;">
<span style="font-size: medium;">Installation</span></h2>
<pre style="background-color: whitesmoke; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid rgba(0,0,0,0.14902); color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 13px; line-height: 20px; margin-bottom: 10px; margin-top: 0px; padding: 9.5px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"> <code style="background-color: transparent; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 0px; color: inherit; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 12px; padding: 0px;">npm install connect-couchdb</code></pre>
<h2 id="-" style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; line-height: 40px; margin: 10px 0px;">
<span style="font-size: medium;">設定與使用</span></h2>
<h3 id="-couchdb-session-store" style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; line-height: 40px; margin: 10px 0px;">
設定CouchDB Session Store</h3>
<pre style="background-color: whitesmoke; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid rgba(0,0,0,0.14902); color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 13px; line-height: 20px; margin-bottom: 10px; margin-top: 0px; padding: 9.5px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 0px; color: inherit; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 12px; padding: 0px;">var connect = require('connect')
//step1. enable couchdb store
, ConnectCouchDB = require('connect-couchdb')(connect)
//step2. connection setting to couchdb
, store = new ConnectCouchDB({
name: 'session',
host: "db_server_ip_address",
port: 5984,
username: 'username',
password: 'password',
ssl: false
});
var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
//step3. setup session to specific store
app.use(connect.cookieParser());
app.use(connect.session({secret: 'YourSecretKey', store: store }));
//step4. end of session setting
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));</code></pre>
<h3 id="-session" style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; line-height: 40px; margin: 10px 0px;">
使用session</h3>
<div style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; line-height: 20px; margin: 0px 0px 10px;">
使用session的部份,原則上與其他persistance的使用方式相同,都是針對req.session這個物件做操作..</div>
<pre style="background-color: whitesmoke; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid rgba(0,0,0,0.14902); color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 13px; line-height: 20px; margin-bottom: 10px; margin-top: 0px; padding: 9.5px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 0px; color: inherit; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 12px; padding: 0px;">//Sample of get session
app.get('/session/:key', function(req, res){
console.log(req.session);
res.end(util.format('key:%s --> value:%s',
req.params.key, req.session[req.params.key]));
});
//Sample of set session
app.get('/session/:key/:value', function(req, res){
req.session[req.params.key] = req.params.value;
res.end('OK');
});</code></pre>
<br /></div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-40659614795493364012013-10-24T03:30:00.001-07:002013-10-24T03:30:41.188-07:00Read a BIG file in Node.js<div dir="ltr"><div>寫程式的人常有需要處理到大型的檔案讀取,在Node.js中如果直接使用原生的fs.readFile()來讀檔,則會發生"RangeError: length > kMaxLength"之類的Exception,範例程式如下:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"> //test-fs.js<br>var fs = require('fs');<br>fs.readFile('../1G-File.txt', function(e,d){<br> if(e) console.log(e);<br> console.log(d);<br>});</blockquote><div><br></div><div>執行的結果如下:</div><div><br></div> <div> <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">$ node test-fs.js<br>buffer.js:194<br> this.parent = new SlowBuffer(this.length);<br> ^<br>RangeError: length > kMaxLength<br> at new Buffer (buffer.js:194:21)<br> at fs.js:220:16<br> at Object.oncomplete (fs.js:107:15)</blockquote></div><div><br></div><div><br></div><div>這時候借用第三方套件"lazy"+"fs.createReadStream()"的話,將可以很順利的解決讀取大檔案的問題喔∼</div> <div><br></div><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">var lazy = require("lazy")<br> , fs = require("fs");<br>new lazy(fs.createReadStream('../1G-File.txt'))<br> .lines<br> .forEach(function(line){<br> console.log(line.toString());<br> }<br>);</blockquote></div><div><br></div> <div>執行上將會很順利...這邊不秀了∼給大家參考</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div> k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-21627163729345287342013-10-22T07:51:00.000-07:002013-10-22T08:05:29.691-07:00Node.js銜接Google的第一步....<div dir="ltr">
<h1 style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 38.5px; line-height: 40px; margin: 10px 0px;">
gapitoken</h1>
<div style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; line-height: 20px; margin: 0px 0px 10px;">
gapitoken是使用Node.js實作Oauth2認證的一個模組,透過API console中申請一個service account,並使用api console產生的p12 key所轉換的pem key,就可以快速的與API進行認證。此種(service account)與API認證的方式適用於提供查詢服務的應用程式,不需要與user的Auth做互動,與API的互動完全是透過應用程式owner所申請的service account之全縣。</div>
<h2 style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 31.5px; line-height: 40px; margin: 10px 0px;">
<span style="font-size: 31.5px;">套件資訊</span></h2>
<div class="" style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; line-height: 20px;">
<ul style="margin: 0px 0px 10px 25px; padding: 0px;">
<li>name : gapitoken</li>
<li>version : 0.1.0</li>
<li>author :<ul style="margin: 0px 0px 0px 25px; padding: 0px;">
<li>name : Gal Ben-Haim</li>
</ul>
</li>
<li>repository :<ul style="margin: 0px 0px 0px 25px; padding: 0px;">
<li>type : git</li>
<li>url : <a href="https://github.com/bsphere/node-gapitoken.git" style="color: #0088cc; text-decoration: none;">git://github.com/bsphere/node-gapitoken.git</a></li>
</ul>
</li>
<li>dependencies :<ul style="margin: 0px 0px 0px 25px; padding: 0px;">
<li>jws : 0.0.2</li>
</ul>
</li>
<li>info : <a href="http://localhost:3000/node_modules/gapitoken/package.json" style="color: #0088cc; text-decoration: none;">參考自套件package.json</a></li>
<li>readme : <a href="http://localhost:3000/node_modules/gapitoken/README.md" style="color: #0088cc; text-decoration: none;">README.md</a></li>
</ul>
</div>
<h2 style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 31.5px; line-height: 40px; margin: 10px 0px;">
Installation</h2>
<pre style="background-color: whitesmoke; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid rgba(0,0,0,0.14902); color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 13px; line-height: 20px; margin-bottom: 10px; margin-top: 0px; padding: 9.5px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 0px; color: inherit; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 12px; padding: 0px;">npm install gapitoken</code></pre>
<h2 style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 31.5px; line-height: 40px; margin: 10px 0px;">
<span style="font-size: 31.5px;">使用說明</span></h2>
<div style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; line-height: 20px; margin: 0px 0px 10px;">
準備連線需要的資訊,其中iss為當初申請service account時候的所給的Email address,如下圖:</div>
<div style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; line-height: 20px; margin: 0px 0px 10px;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyIM0hc6xWMLLTXW7cZal4HE9BzUrNF5o35SYykIDi81ui1mNXOm37N7js8IjIMQOdgYdqdgFb7cZP9T2yXea4Q7e0PEtKC5aSgHLID5MuGbMUiaDyKlFvnlkhV9S3bfzjQvfcaSTw9geQ/s1600/google-oauth-service-account-729995.png"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5937592701412526594" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiyIM0hc6xWMLLTXW7cZal4HE9BzUrNF5o35SYykIDi81ui1mNXOm37N7js8IjIMQOdgYdqdgFb7cZP9T2yXea4Q7e0PEtKC5aSgHLID5MuGbMUiaDyKlFvnlkhV9S3bfzjQvfcaSTw9geQ/s320/google-oauth-service-account-729995.png" /></a></div>
<div style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; line-height: 20px; margin: 0px 0px 10px;">
而Scope是Google控制存取API的呼叫權限,不同的API或不同的request有不同的scope要給予,才可以做呼叫動作,最後,key.pem是創立service account時候所給予的p12檔案,透過ssl轉換後的檔案...</div>
<pre style="background-color: whitesmoke; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid rgba(0,0,0,0.14902); color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 13px; line-height: 20px; margin-bottom: 10px; margin-top: 0px; padding: 9.5px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 0px; color: inherit; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 12px; padding: 0px;">var opts = {
iss: '<a href="mailto:860835...6s1@developer.gserviceaccount.com">860835...6s1@developer.gserviceaccount.com</a>',
scope: '<a href="https://www.googleapis.com/auth/bigquery">https://www.googleapis.com/auth/bigquery</a> <a href="https://www.googleapis.com/auth/cloud-platform">https://www.googleapis.com/auth/cloud-platform</a>',
keyFile: __dirname + '/key.pem'
};</code></pre>
<div style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; line-height: 20px; margin: 0px 0px 10px;">
<span style="font-size: 31.5px; line-height: 40px;"><b>準備API pem key</b></span><br /> </div>
<div style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; line-height: 20px; margin: 0px 0px 10px;">
從API console下載的key為副檔名p12的key,需要透過openssl轉換為pem key</div>
<pre style="background-color: whitesmoke; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid rgba(0,0,0,0.14902); color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 13px; line-height: 20px; margin-bottom: 10px; margin-top: 0px; padding: 9.5px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 0px; color: inherit; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 12px; padding: 0px;">$ openssl pkcs12 -in privatekey.p12 -out privatekey.pem -nocerts
$ openssl rsa -in privatekey.pem -out key.pem</code></pre>
<div>
<code style="background-color: transparent; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 0px; color: inherit; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 12px; padding: 0px;"><br /> </code></div>
<div style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; line-height: 20px; margin: 0px 0px 10px;">
透過GAPI物件,進行Auth連線動作,實際token可以gapi.getToken()的callback中取得...<br /> </div>
<pre style="background-color: whitesmoke; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid rgba(0,0,0,0.14902); color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 13px; line-height: 20px; margin-bottom: 10px; margin-top: 0px; padding: 9.5px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 0px; color: inherit; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 12px; padding: 0px;">var gapi = new GAPI(opts, function(err) {
if (err) return console.log(err);
gapi.getToken(function(err, token) {
if (err) return console.log(err);
//這邊已經取到token了
console.log(token);
});
});</code></pre>
<div style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 14px; line-height: 20px; margin: 0px 0px 10px;">
呼叫API,這邊透過request模組做API的呼叫,其中auth token需要放在header的Authorization中...</div>
<pre style="background-color: whitesmoke; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid rgba(0,0,0,0.14902); color: #333333; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 13px; line-height: 20px; margin-bottom: 10px; margin-top: 0px; padding: 9.5px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><code style="background-color: transparent; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px; border-top-left-radius: 3px; border-top-right-radius: 3px; border: 0px; color: inherit; font-family: Monaco,Menlo,Consolas,'Courier New',monospace; font-size: 12px; padding: 0px;">var bqurl = '<a href="https://www.googleapis.com/bigquery/v2/projects/%s/queries">https://www.googleapis.com/bigquery/v2/projects/%s/queries</a>';
request({
url: util.format(bqurl,'your-project-name'),
method: 'POST',
headers: {
"Authorization": "Bearer " + token
},
json: {
query: 'select * from cp300.GetAnimals'
}
}, function(e,r,d){
if(e) console.log(e); //Print the query result
console.log(JSON.stringify(d));
});</code></pre>
<h2 style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 31.5px; line-height: 40px; margin: 10px 0px;">
使用範例 - 連線BigQuery</h2>
<h3 style="color: #333333; font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 24.5px; line-height: 40px; margin: 10px 0px;">
See:<a href="http://localhost:3000/examples/gapitoken/bq-sample.js" style="color: #0088cc; text-decoration: none;">/examples/gapitoken/bq-sample.js</a><div class="" id="zclip-ZeroClipboardMovie_1" style="height: 25px; width: 49px;">
</div>
</h3>
<pre class="" id="code-exp-gapitoken-bq-sample-js" style="background-color: whitesmoke; border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-top-left-radius: 4px; border-top-right-radius: 4px; border: 1px solid rgba(0, 0, 0, 0.14902); color: #333333; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; line-height: 20px; margin-bottom: 10px; margin-top: 0px; padding: 9.5px; white-space: pre-wrap; word-break: break-all; word-wrap: break-word;"><span style="font-size: x-small;">var GAPI = require('gapitoken')
, request = require('request')
, fs = require('fs')
, util = require('util')
//From admin console, create a service account, save the client_secrets.json and it's key
var client_secrets = JSON.parse(fs.readFileSync(__dirname + '/client_secrets.json','utf8'))
//Project setting
var iss = client_secrets.web.client_email;
var bq_scope = '<a href="https://www.googleapis.com/auth/bigquery">https://www.googleapis.com/auth/bigquery</a> <a href="https://www.googleapis.com/auth/cloud-platform">https://www.googleapis.com/auth/cloud-platform</a>';
var project = 'your-project-name';
var opts = {
iss: iss,
scope: bq_scope,
keyFile: __dirname + '/key.pem' };
var _token = '';
var gapi = new GAPI(opts, function(err) {
if (err) return console.log(err);
gapi.getToken(function(err, token) {
if (err) return console.log(err);
_token = token;
var bqurl = '<a href="https://www.googleapis.com/bigquery/v2/projects/%s/queries">https://www.googleapis.com/bigquery/v2/projects/%s/queries</a>'; request({
url: util.format(bqurl,project),
method: 'POST',
headers: {
"Authorization": "Bearer " + _token
},
json: {
query: 'select * from cp300.GetAnimals'
}
}, function(e,r,d){
if(e) console.log(e); //Print the query result
console.log(JSON.stringify(d));
});
});
}); </span></pre>
<br /></div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-39947347196865156592013-08-18T20:50:00.001-07:002013-08-18T20:50:50.672-07:00Npm Repository Info of you package<div dir="ltr"><div>透過下面網址可以從npm取回某個到見的相關資訊</div><div>並且render成一張圖片喔∼</div><div><br></div><div><a href="https://nodei.co/npm/">https://nodei.co/npm/</a><font color="#0000ff">[套件名稱]</font>.png<br></div><div>ex: <a href="https://nodei.co/npm/noder.png">https://nodei.co/npm/noder.png</a><br> </div><div><br></div><div>範例如下:</div><div><br></div><div><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK66dmTDHKpGOJHN26Fr0wSt9KiL6utfp_qOhBw2Y4wZfnY3Mya2fvq2cbPUf4R7wj2jPe__2Zlcgx3BMb2yEWsDCtQtFXEykPPPYrZ0XEvzsb0lErWzHOFnp93eqPqTgT8GevTkiRFewF/s1600/image-750672.png"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK66dmTDHKpGOJHN26Fr0wSt9KiL6utfp_qOhBw2Y4wZfnY3Mya2fvq2cbPUf4R7wj2jPe__2Zlcgx3BMb2yEWsDCtQtFXEykPPPYrZ0XEvzsb0lErWzHOFnp93eqPqTgT8GevTkiRFewF/s320/image-750672.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5913672827313433810" /></a><br></div><div><br></div><div>看起來這張圖片是排成去產生的</div><div>如果可以做成自動產生,那就更炫了 :D</div></div> k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-73309329790514339812013-08-04T04:17:00.001-07:002013-08-04T04:17:59.937-07:00GitLab one click installer<div dir="ltr"><div>GitLab是一套提供類似Private Github服務的軟體,開放且因為使用上親近Github,所以是套非常建議每個開發Team都安裝的服務... 實際上看過GitLab的手動安裝方式,其實相當複雜,但是因為有Bitnami這個熱心的服務者打包GitLab的服務,讓Linux上安裝非常的方便...</div><div><br></div><div>目前我常試過CentOS的安裝方式,原則上是下載下來後,直接執行:</div> <div>下載網址:<a href="http://bitnami.com/redirect/to/22096/bitnami-gitlab-5.4.0-0-linux-x64-installer.run">http://bitnami.com/redirect/to/22096/bitnami-gitlab-5.4.0-0-linux-x64-installer.run</a></div><div>執行安裝:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"> <div><font face="courier new, monospace"># chmod u+x bitnami-gitlab-5.4.0-0-linux-x64-installer.run</font></div><div><div><font face="courier new, monospace"># ./bitnami-gitlab-5.4.0-0-linux-x64-installer.run</font></div> </div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">----------------------------------------------------------------------------</font></div> </div><div><div><font face="courier new, monospace">Welcome to the BitNami GitLab Stack Setup Wizard.</font></div></div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">----------------------------------------------------------------------------</font></div> </div><div><div><font face="courier new, monospace">Select the components you want to install; clear the components you do not want</font></div></div><div><div><font face="courier new, monospace">to install. Click Next when you are ready to continue.</font></div> </div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">GitLab : Y (Cannot be edited)</font></div></div><div><div><font face="courier new, monospace"><br></font></div> </div><div><div><font face="courier new, monospace">GitLab CI [Y/n] :</font></div></div><div><div><font face="courier new, monospace">Is the selection above correct? [Y/n]:</font></div></div><div><div><font face="courier new, monospace">----------------------------------------------------------------------------</font></div> </div><div><div><font face="courier new, monospace">Installation folder</font></div></div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">Please, choose a folder to install BitNami GitLab Stack</font></div> </div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">Select a folder [/opt/gitlab-5.4.0-0]:</font></div></div><div><div><font face="courier new, monospace">----------------------------------------------------------------------------</font></div> </div><div><div><font face="courier new, monospace">Create Admin account</font></div></div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">BitNami GitLab Stack admin user creation</font></div> </div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">Login [user]: root</font></div></div><div><div><font face="courier new, monospace"><br></font></div> </div><div><div><font face="courier new, monospace">Password :</font></div></div><div><div><font face="courier new, monospace">Please confirm your password :</font></div></div><div><div><font face="courier new, monospace">Email Address [<a href="mailto:user@example.com">user@example.com</a>]: <a href="mailto:simonsu@mitac.com.tw">simonsu@mitac.com.tw</a></font></div> </div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">----------------------------------------------------------------------------</font></div></div><div> <div><font face="courier new, monospace">Hostname that will be used to create internal URLs. If this value is incorrect,</font></div></div><div><div><font face="courier new, monospace">you may be unable to access your Gitlab installation from other computers. It is</font></div> </div><div><div><font face="courier new, monospace">advisable to use a Domain instead of an IP address for compatibility with</font></div></div><div><div><font face="courier new, monospace">different browsers.</font></div> </div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">Domain [127.0.0.1]: xxx.xxx.xxx.xxx</font></div></div><div><div><font face="courier new, monospace"><br> </font></div></div><div><div><font face="courier new, monospace">Do you want to configure mail support? [y/N]: y</font></div></div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">----------------------------------------------------------------------------</font></div> </div><div><div><font face="courier new, monospace">Configure SMTP Settings</font></div></div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">This is required so your application can send notifications via email.</font></div> </div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">Default email provider:</font></div></div><div><div><font face="courier new, monospace"><br></font></div> </div><div><div><font face="courier new, monospace">[1] GMail</font></div></div><div><div><font face="courier new, monospace">[2] Custom</font></div></div><div><div><font face="courier new, monospace">Please choose an option [1] : 1</font></div> </div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">----------------------------------------------------------------------------</font></div></div><div> <div><font face="courier new, monospace">Configure SMTP Settings</font></div></div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">Default mail server configuration.</font></div> </div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">GMail address []: <a href="mailto:simonsu@mitac.com.tw">simonsu@mitac.com.tw</a></font></div></div> <div> <div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">GMail password :</font></div></div><div><div><font face="courier new, monospace">Re-enter :</font></div></div> <div> <div><font face="courier new, monospace">----------------------------------------------------------------------------</font></div></div><div><div><font face="courier new, monospace">Setup is now ready to begin installing BitNami GitLab Stack on your computer.</font></div> </div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">Do you want to continue? [Y/n]: Y</font></div></div><div><div><font face="courier new, monospace"><br> </font></div></div><div><div><font face="courier new, monospace">----------------------------------------------------------------------------</font></div></div><div><div><font face="courier new, monospace">Please wait while Setup installs BitNami GitLab Stack on your computer.</font></div> </div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace"> Installing</font></div></div><div><div><font face="courier new, monospace"> 0% ______________ 50% ______________ 100%</font></div> </div><div><div><font face="courier new, monospace"> #########################################</font></div></div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">----------------------------------------------------------------------------</font></div> </div><div><div><font face="courier new, monospace">Setup has finished installing BitNami GitLab Stack on your computer.</font></div></div><div><div><font face="courier new, monospace"><br></font></div></div><div><div><font face="courier new, monospace">Info: To access the BitNami GitLab Stack, go to</font></div> </div><div><div><font face="courier new, monospace"><a href="http://xxx.xxx.xxx.xxx:80">http://xxx.xxx.xxx.xxx:80</a> from your browser.</font></div></div><div><div><font face="courier new, monospace">Press [Enter] to continue :</font></div> </div></blockquote><div><br></div><div>完成安裝之後,就可以透過80 port連線進入該主機,畫面與操作上與Github真的頗為相像,後續有趣的地方就留給有興趣的網友去發現了 :D</div><div><br></div><div><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2doTH9g6z9Fz8KgJD5HDt7YLxG7bh1QX6r5Aj2BofsDQDidOjeNF-vf0spS_sURY8BAORYs-Lg4RtDtJ6WmJqI5VyJYlqJH1qxAuMwh1Twfaxq_GVMXfXugVSBWdNn89jGnPT2AN4n0T0/s1600/image-779938.png"><img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2doTH9g6z9Fz8KgJD5HDt7YLxG7bh1QX6r5Aj2BofsDQDidOjeNF-vf0spS_sURY8BAORYs-Lg4RtDtJ6WmJqI5VyJYlqJH1qxAuMwh1Twfaxq_GVMXfXugVSBWdNn89jGnPT2AN4n0T0/s320/image-779938.png" border="0" alt="" id="BLOGGER_PHOTO_ID_5908221780490558546" /></a><br> </div> <div><br></div></div> k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-87597223536458802272013-07-30T22:17:00.001-07:002013-07-30T22:17:54.286-07:00Ejs page scope直接使用session物件的方式<div dir="ltr"><div>在ExpressJS中,使用Ejs view engine時,發現session不能再ejs page中直接取用,所有參數都必須靠render時候傳遞過去...跟asp, jsp等scriptlet language操作上有明顯的不同...@@</div><div><br></div><div>為了達到可以在ejs中直接取用,這邊可以使用ejs page的locals變數,locals變數在route設定時候可以使用res.locals來取出,這時候只要將它與req.session做串連(res.locals.session = req.session)即可讓前端的ejs page直接透過<%=locals.session%>或是<%=session%>的方式來操作session...</div> <div><br></div><div>而如果要在每一個route中設定res.locals.session,這樣也太麻煩... 我們可以透過app.use的方式來設定:</div><div><br></div><div><div><div>app.use(function(req, res, next){</div><div> res.locals.session = req.session;</div><div> next();</div> <div>});</div></div></div><div><br></div><div>這樣所有的route在執行的時候都會先跑過一次這個動作,所以在使用時候可以這樣:</div><div><br></div><div><b>[app.js]</b></div><div>app.get('/', function(req, res) {</div><div> req.session.user = {name: 'simon'}</div> <div> ...(skip)</div><div>});</div><div><br></div><div><b>[view/index.ejs]</b></div><div>...(skip)</div><div><%=session.user? 'got user: '+<a href="http://session.user.name">session.user.name</a> : 'no user...'%></div> <div>...(skip)</div><div><b><br></b></div><div>這時候從ejs取用session的物件就單純多了 :D</div></div> k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-10326152367819955722013-07-30T22:02:00.001-07:002013-07-30T22:02:40.528-07:00在ExpressJS中進階使用Ejs view engine<div dir="ltr"><div>在操作ExpressJS的Ejs View Engine時候</div><div>發現針對頁面上的操作並不是那麼的順心</div><div>舉個例子:</div><div>app.js中做一個route希望能夠將值往前端(.ejs)帶,但是前端implement了express-partials的模組,希望把頁面用template的方式組織起來,並且在template page中會用到一部分的參數... 假設有時個route用到這個template,則每個route都必須把參數設定進去,否則後端會接收到"not defined"的錯誤訊息...</div> <div><br></div><div>上面例子的片段程式碼如下:</div><div><br></div><div><b>[app.js]</b></div><div><div>app.get('/', function(req, res){</div><div> res.render('index', { title: 'my express page' });</div><div> });</div> </div><div><br></div><div><b>[index.ejs]</b></div><div><% if(user) { %><br></div><div><%= user %></div><div><% } %></div><div><br></div><div><b>執行時候會有Exception:</b></div><div>...(skip)</div><div>user is not defined<br> </div><div>...(skip)</div><div><br></div><div>這部份的錯誤應該是Ejs在render page時候造成的,它直接throw Exception而會造成page終止render,在遍尋不著比較好的方法時,從<a href="http://www.cnblogs.com/owenChen/archive/2013/01/23/2872360.html">某篇文章</a>找到一些蛛絲馬跡...</div> <div><br></div><div>原來Ejs使用"locals"這個物件來包裝頁面上會用到的所有參數,而經過ejs的scriptlet tag包裝起來的部分,可以直接使用locals裡面的參數,也就是說上面的app.js做page render時候:</div><div><br></div><div>res.render('index', { title: 'my express page' })<br> </div><div><br></div><div>相當於把title這個參數與ejs頁面的locals變數做整合</div><div>亦即ejs頁面操作:</div><div><br></div><div><%=title%></div><div><br></div><div>與</div><div><br></div><div><%=locals.title%><br></div><div><br></div> <div>是相等的,但是直接操作title屬性時候,會被ejs compile成runtime exception,這導致如果在ejs中執行下面判斷會出錯;</div><div><br></div><div><% if(user) { .... %></div><div><br></div><div>因為實際上locals.user不存在,且ejs compiler會將不存在的狀況throw exception...</div> <div><br></div><div>解決方式,直接使用locals來判斷裡面是否有user這個變數...,因此改寫上面的判斷後:</div><div><br></div><div><% if(locales['user']) { .... %><br></div><div><br></div><div>應該就可以正常運作...</div><div><br></div></div> k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-63699867204824657472013-07-05T00:04:00.001-07:002013-07-05T00:04:21.891-07:00Private NPM建置教學<div dir="ltr">心血來潮找到的:<div><a href="http://clock.co.uk/tech-blogs/how-to-create-a-private-npmjs-repository">http://clock.co.uk/tech-blogs/how-to-create-a-private-npmjs-repository</a><br></div><div><br></div><div>有興趣的人可以實作看看!</div> </div> k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-49583117802994628852013-06-22T12:14:00.001-07:002013-06-22T12:14:35.046-07:00About HTTP delete method error response<div dir="ltr"><div>記錄一個比較不常用到的操作方式...</div><div>以client使用request module為例...</div><div>http delete method中,某些client遇到error時,解讀回覆部分不是直接取用body</div><div>而是去解析error物件來做回覆</div><div>此時如何從server端製作一個錯誤來讓client操作呢...</div><div> <br></div>下面是client端的模擬code,主要client端想要取出callback function中的error物件...<br><div><br></div><div>Client side:</div><div><font face="courier new, monospace">var opts = {url:'<a href="http://localhost:1337/test/123">http://localhost:1337/test/123</a>', method:'DELETE'};</font></div> <div><font face="courier new, monospace">request(</font></div><div><font face="courier new, monospace"> opts, </font></div><div><font face="courier new, monospace"> function(error,res,body) {</font></div><div><font face="courier new, monospace"> console.log(e);</font></div> <div><font face="courier new, monospace"> }</font></div><div><font face="courier new, monospace">);</font></div><div><br></div><div>此時server side的實作,以express為例,只需要在res.send()中依序帶入:</div><div><ul><li>第一個欄位設定錯誤代碼</li><li> 第二個欄位為錯誤物件描述,建議以JSON為格式(預設格式為{code: xxx, message:xxx},若符合此格式,回覆時候匯自動取message內文做為error的message欄位,並會在error物件中多一個code的欄位}</li> </ul></div><div>下面模擬del協定,於呼叫時候丟錯:</div><div><br></div><div><div>Server side:</div><div><font face="courier new, monospace">app.del('/test/:id', function(req, res) {</font></div><div><font face="courier new, monospace"> res.send(400, {code:"test error", message:"the tested error"});</font></div> <div><font face="courier new, monospace">});</font></div></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">此時client的console pring匯出現下面訊息:</font></div><div><br></div> <div> Client result: </div><div><div><font face="courier new, monospace">{ message: 'the tested error',</font></div><div><font face="courier new, monospace"> body: { code: 'test error', message: 'the tested error' },</font></div> <div><font face="courier new, monospace"> httpCode: 400,</font></div><div><font face="courier new, monospace"> statusCode: 400,</font></div><div><font face="courier new, monospace"> restCode: 'test error',</font></div> <div><font face="courier new, monospace"> code: 'test error' }</font></div></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">如上描述,如果server改寫response回傳物件(不使用預設格式)...</font></div> <div><font face="courier new, monospace"><br></font></div><div><div>Server side:</div><div><font face="courier new, monospace">app.del('/test/:id', function(req, res) {</font></div><div><font face="courier new, monospace"> res.send(400, {coder:"test error", msg:"the tested error"});</font></div> <div><font face="courier new, monospace">});</font></div></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">則,error物件會長這樣子:</font></div><div><font face="courier new, monospace"><br> </font></div><div><font face="courier new, monospace"><div>{ message: '{\n "coder": "test error",\n "msg": "the tested error"\n}',</div><div> body: { coder: 'test error', msg: 'the tested error' },</div> <div> httpCode: 400,</div><div> statusCode: 400 }</div></font></div></div> k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-20584856255669578142013-06-22T02:04:00.000-07:002013-06-25T02:20:51.161-07:00Log4js support print object inline<div dir="ltr">
log4js套件是我常使用的log管理工具<br />
<div>
最近看別人的程式才發現,原來它有支援物件的列印</div>
<div>
透過 <a href="http://log.info/">log.info</a>('......%o....', jsonObject) 的方式</div>
<div>
可以直接把物件解析成String...</div>
<div>
真是方便的功能∼</div>
<div>
<div>
<br /></div>
<div>
範例:</div>
<div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhk80CyEihOA6XYJgIRrXYOLMbmazarOpSbp5QkcJcs5TqLFkeEchI4g6PuVo56j9cNEjeYw8UmNu7Vfc3RtFV97qBQX1Zeud8sigx9Lnnfg_wVT8WUNmJdXb8z60BgcD80fTPnPr8sGdqw/s1600/image-720739.png"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5892230937140385938" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhk80CyEihOA6XYJgIRrXYOLMbmazarOpSbp5QkcJcs5TqLFkeEchI4g6PuVo56j9cNEjeYw8UmNu7Vfc3RtFV97qBQX1Zeud8sigx9Lnnfg_wVT8WUNmJdXb8z60BgcD80fTPnPr8sGdqw/s320/image-720739.png" /></a></div>
<div>
<br /></div>
<div>
執行結果:</div>
<div>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyTfw9KqK1z0dcwjp2IUzB7PZqzIgL6pMRBvMF6cFsga0QmCMvBVPgDbtVMz_TCE1q4FnLFlOIojLkBqhaexqFoTY-OB4qwKeFY52HARufWus9sKXG_-egohVTJ7Pmu37oMCL354RK-bXi/s1600/image-722205.png"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5892230941217636418" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhyTfw9KqK1z0dcwjp2IUzB7PZqzIgL6pMRBvMF6cFsga0QmCMvBVPgDbtVMz_TCE1q4FnLFlOIojLkBqhaexqFoTY-OB4qwKeFY52HARufWus9sKXG_-egohVTJ7Pmu37oMCL354RK-bXi/s320/image-722205.png" /></a><br />
<br />
更正,不是%o造成的<br />
應該是log4js將最後一個參數使用像console.log(object)的方式<br />
解些json物件成為string...</div>
</div>
</div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.comtag:blogger.com,1999:blog-7753376764623667668.post-85544149363671719982013-06-11T01:08:00.001-07:002013-06-11T01:51:47.156-07:00Access-Control-Allow-Origin範例<div dir="ltr">
<h1 style="color: #222222; font-family: Lato, 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 28px; line-height: 1.1; margin: 0px 0px 20px;">
<span style="color: #777777; font-size: 14px; font-weight: 300; line-height: 21px;">在JS的世界裡,Ajax的頭號天敵就是為了避免不安全,Browser所設限的coss site not allow的限制,為了避免cross site javascript的問題,傳統作法是透過script tag來嵌入非同個網站的資源,或是使用jsonp來做site to site的呼叫,但是...</span></h1>
<h1 style="font-family: Lato,'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 28px; line-height: 1.1; margin: 0px 0px 20px;">
<ul style="color: #777777; font-size: 14px; font-weight: 300; line-height: 21px; margin: 0px 0px 20px;">
<li>script tag內文必須要是script,否則無法順利讀取,甚至會弄壞網頁中的js...</li>
<li>jsonp已經被chrome排擠... firefox可以正常使用</li>
</ul>
<div style="color: #777777; font-size: 14px; font-weight: 300; line-height: 21px; margin-bottom: 20px;">
有了Node.js的request套件後,我們大可以一行解決... 讓遠方資源變成我發資源:</div>
<pre style="background-color: #f8f8f8; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; border-top-left-radius: 5px; border-top-right-radius: 5px; border: 1px solid rgb(229, 229, 229); color: #333333; font-family: Monaco, 'Bitstream Vera Sans Mono', 'Lucida Console', Terminal; font-size: 12px; font-weight: 300; margin-bottom: 20px; overflow-x: auto; padding: 8px 15px;"><code style="font-family: Monaco, 'Bitstream Vera Sans Mono', 'Lucida Console', Terminal;">request.get('http://mysite.com/doodle.png').pipe(resp)</code></pre>
<div style="color: #777777; font-size: 14px; font-weight: 300; line-height: 21px; margin-bottom: 20px;">
上面可以讓原本需要透過<a href="http://mysite.com/doodle.png" style="color: #3399cc; text-decoration: none;">http://mysite.com/doodle.png<img src="http://opennodes.github.io/wiki/images/link.png" style="max-width: 100%;" width="15px" /></a> 來呼叫的資源,透過你制定的鏈結呼叫該資源...</div>
<div style="color: #777777; font-size: 14px; font-weight: 300; line-height: 21px; margin-bottom: 20px;">
而,根本的解決方式可以使用Header中加入Access-Control-Allow-Origin參數來設定你的js,允許讓其他網站來呼叫...</div>
<div style="color: #777777; font-size: 14px; font-weight: 300; line-height: 21px; margin-bottom: 20px;">
Server端實作:</div>
</h1>
<h3 style="color: #494949; font-family: Lato, 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 1.1; margin: 0px 0px 20px;">
See:<a href="http://opennodes.github.io/examples/basic/http/cross-site-server.js" style="color: #3399cc; font-weight: 400; text-decoration: none;">/examples/basic/http/cross-site-server.js</a></h3>
<h1 style="font-family: Lato,'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 28px; line-height: 1.1; margin: 0px 0px 20px;">
<pre class="code" data-js="basic/http/cross-site-server.js" style="background-color: #f8f8f8; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; border-top-left-radius: 5px; border-top-right-radius: 5px; border: 1px solid rgb(229, 229, 229); color: #333333; font-family: Monaco, 'Bitstream Vera Sans Mono', 'Lucida Console', Terminal; font-size: 12px; font-weight: 300; margin-bottom: 20px; overflow-x: auto; padding: 8px 15px;">var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200,{"Access-Control-Allow-Origin": "http://10-20-0-31.my.micloud.tw"});
res.end(JSON.stringify({result:'yes'}));
}).listen(8080);
console.log('Server running at port 8080');</pre>
<div style="color: #777777; font-size: 14px; font-weight: 300; line-height: 21px; margin-bottom: 20px;">
</div>
<div style="color: #777777; font-size: 14px; font-weight: 300; line-height: 21px; margin-bottom: 20px;">
上面設定資源可以讓site:<a href="http://10-20-0-31.my.micloud.tw/" style="color: #3399cc; text-decoration: none;">http://10-20-0-31.my.micloud.tw<img src="http://opennodes.github.io/wiki/images/link.png" style="max-width: 100%;" width="15px" /></a> 下面的網頁來呼叫,如果想要讓所有網站都可以呼叫,可以使用"*"來讓所有網站可以呼叫...</div>
<div style="color: #777777; font-size: 14px; font-weight: 300; line-height: 21px; margin-bottom: 20px;">
Client端實作:</div>
</h1>
<h3 style="color: #494949; font-family: Lato, 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 1.1; margin: 0px 0px 20px;">
See:<a href="http://opennodes.github.io/examples/basic/http/cross-site.html" style="color: #3399cc; font-weight: 400; text-decoration: none;">/examples/basic/http/cross-site.html</a></h3>
<h1 style="font-family: Lato,'Helvetica Neue',Helvetica,Arial,sans-serif; font-size: 28px; line-height: 1.1; margin: 0px 0px 20px;">
<pre class="code" data-html="basic/http/cross-site.html" style="background-color: #f8f8f8; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; border-top-left-radius: 5px; border-top-right-radius: 5px; border: 1px solid rgb(229, 229, 229); color: #333333; font-family: Monaco, 'Bitstream Vera Sans Mono', 'Lucida Console', Terminal; font-size: 12px; font-weight: 300; margin-bottom: 20px; overflow-x: auto; padding: 8px 15px;"><script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function(){
$.getJSON('http://211.78.254.38:8080/', function(data){
$('#r').html(data.result);
});
});
</script>
<h1>Server response: <span id="r"></span></h1></pre>
</h1>
<div>
<div>
<div>
</div>
</div>
</div>
</div>
k8s-studyhttp://www.blogger.com/profile/15291073043196262423noreply@blogger.com