<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by George Chen on Medium]]></title>
        <description><![CDATA[Stories by George Chen on Medium]]></description>
        <link>https://medium.com/@george_chen?source=rss-613cf05ecb65------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*efLRO03ki7kXWKrC1MLrLw.jpeg</url>
            <title>Stories by George Chen on Medium</title>
            <link>https://medium.com/@george_chen?source=rss-613cf05ecb65------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 20:03:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@george_chen/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Manage different git remote repo by different key]]></title>
            <link>https://medium.com/@george_chen/manage-different-git-remote-repo-by-different-key-fcf5bdd60da8?source=rss-613cf05ecb65------2</link>
            <guid isPermaLink="false">https://medium.com/p/fcf5bdd60da8</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[George Chen]]></dc:creator>
            <pubDate>Thu, 14 Jan 2016 16:23:39 GMT</pubDate>
            <atom:updated>2016-01-14T16:23:39.287Z</atom:updated>
            <content:encoded><![CDATA[<p>Manage different git remote repo by different key</p><h4><strong>使用情境:</strong></h4><p>電腦本身有多個private-key在<strong><em>.ssh/</em></strong> 下，然後希望使用不同的private-key 來連接不同的remote repo。</p><blockquote>NOTE: 若沒特別設定的話，git預設只會採用 <strong><em>.ssh/id_rsa</em></strong> 來連接remote repo</blockquote><h4><strong>解決方式：</strong></h4><ol><li>新增不同repo 所使用的private-key 設定檔： <strong><em>.ssh/config</em></strong></li></ol><p>在<strong><em>.ssh/config</em></strong> 裡的內容加入以下範例</p><pre>Host github-remote-repo<br>  HostName github.com<br>  User git<br>  IdentityFile ~/.ssh/remoteserver.id_rsa</pre><blockquote>NOTE: 這邊假設我們已經有一組key <strong><em>remoteserver.id_rsa</em></strong>是用來連接特定的remote repo</blockquote><p>2. 在我們的local 端設定remote repo，並套用<strong>step1</strong>的設定</p><pre>git remote add origin github-remote-repo:path/to/repository.git</pre><blockquote>NOTE: 在設定remote repo時，host位置就直接使用config中的Host <strong>(github-remote-repo)</strong> ，則我們要做fetch/push時，就會自動去尋找對應的ssh key <strong>(remote server.id_rsa)</strong></blockquote><p>3. 更新ssh key的設定</p><pre>ssh-add -l</pre><pre>ssh-add -D</pre><h4>Reference:</h4><ul><li><a href="https://matharvard.ca/posts/2011/aug/11/git-push-with-specific-ssh-key/">git-push With Specific SSH Key / Mat Harvard</a></li><li><a href="http://stackoverflow.com/questions/7927750/specify-an-ssh-key-for-git-push-for-a-given-domain">Specify an SSH key for git push for a given domain</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fcf5bdd60da8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Upgrade Node.js Version]]></title>
            <link>https://medium.com/@george_chen/upgrade-node-js-version-4f98103f329f?source=rss-613cf05ecb65------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f98103f329f</guid>
            <dc:creator><![CDATA[George Chen]]></dc:creator>
            <pubDate>Sun, 25 Oct 2015 09:14:18 GMT</pubDate>
            <atom:updated>2016-07-23T08:33:04.541Z</atom:updated>
            <content:encoded><![CDATA[<p>Node.js (node)是 Ryan Dahl 基於 Google 的 V8 引擎於 2009 年釋出的一個 JavaScript 開發平台，目的是為了讓javascript可以運行在伺服器端!</p><p>node經過一系列的分分合合，今年終於在強大社群的支持下，推出了第一個long-term support的版本v4.2.1 !</p><p>目前寫這篇目的在於，簡單記錄我從v0.10.35升到v4.2.1中遇到的一些問題!</p><h4>NVM, node version management</h4><p>目前在node的生態體系下，已經有很方便的版本管理工具 =&gt; nvm<br>所以基本上，升級的過程中已經避掉了很多麻煩的地方了；直接就從nvm安裝開始:</p><pre>git clone git://github.com/creationix/nvm.git ~/.nvm<br>. ~/.nvm/nvm.sh<br>echo &quot;. ~/nvm/nvm.sh&quot; &gt;&gt; ~/.bash_profile<br>基本上，上面這些指令已經可以安裝好nvm了，可以先用簡單的指令：nvm ls來查看目前透過nvm安裝的node版本!</pre><blockquote>.base_profile 是 osx的使者用設定檔，如果是用ubunut類的系統，則要替換成.bashrc</blockquote><h4>Using NVM to install node.js</h4><p>接下來，node的版本管理可以直接由nvm來做了!</p><p>首先要做的是，透過nvm來安裝最新版的node.js</p><pre>nvm ls-remote  // 顯示目前可以安裝的node 版本<br>nvm install v4.2.1  // 我們直接安裝node的最新版本</pre><pre>... 安裝完後</pre><pre>nvm use v4.2.1   // 使用剛安裝好的node 版本<br>                 // 假設有多個版本時，就可以用這個指令來控制目前運行的版本</pre><pre>nvm alias default v4.2.1 // nvm 所影響到的為當下的shell，也就是說，只要每        <br>                         // 開一個shell就要重新設定一次版本；<br>                         // 而這個方便的指令就是用來指定預設使用的node 版本</pre><h4>General dependency issues</h4><p>當使用不同的node版本時，相對應的node_moduels 也要跟著重新安裝，否則通常採到的第一個雷大概就是3rd-party modules的影嚮 !</p><pre>npm rebuild              // 重build npm <br>npm -rf node_modules/    // 清除掉所有用到的3rd-modules<br>npm install              // 用新的npm 來重裝所有的3rd-modules</pre><h4>Special dependency issues</h4><p>運氣好的話，上面這樣就可以解決一些相依性的問題了；但如果還是遇到有問題時，就相對比較麻煩了! 這時候就要根據不同的error log來找問題點!<br>這次遇到的問題有2個3rd-modules，剛好是兩個不一樣的狀況!</p><pre><strong>gulp:</strong><br>先前<em>gulp</em>是採用 <em>npm install -g gulp</em>來安裝的，所以就要找到那個路徑下的<em>gulp</em>，然後刪掉並重新安裝!</pre><pre><strong>hiredis:</strong><br>而hiredis的問題使用的版本太舊了，在<em>package.json</em>中更新<em>hiredis</em>的版本編號後，再重裝也解缺這部分的問題了!</pre><h4>reference</h4><ul><li><a href="http://blog.caesarchi.com/2012/10/nvm-nodejs-nodejs-day-3.html">NVM, 安裝管理你的 node.js</a></li><li><a href="http://icarus4.logdown.com/posts/175092-nodejs-installation-guide">Node.js 安裝與版本切換教學 (for MAC) &quot; icarus4&#39;s blog</a></li><li><a href="http://stackoverflow.com/questions/28486891/uncaught-error-module-did-not-self-register">Uncaught Error: Module did not self-register</a></li><li><a href="https://keymetrics.io/2015/02/03/installing-node-js-and-io-js-with-nvm/">Install Node.js or io.js with nvm</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f98103f329f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[keep user password secured]]></title>
            <link>https://medium.com/@george_chen/keep-user-password-secured-cd89e6467212?source=rss-613cf05ecb65------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd89e6467212</guid>
            <category><![CDATA[passwords]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[George Chen]]></dc:creator>
            <pubDate>Fri, 23 Oct 2015 09:15:25 GMT</pubDate>
            <atom:updated>2015-10-24T06:09:30.458Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jIcfUIW1JrBKLhZ3Fxig4g.jpeg" /><figcaption>source: <a href="https://pixabay.com/en/castle-chain-security-979597/">https://pixabay.com/en/castle-chain-security-979597/</a></figcaption></figure><p>最近要處理密碼相關的儲存，順便把這個過程記錄一下!</p><h4>Wrong Example !</h4><p>我們在開發時使用者的密碼通常都會直接在DB的某個欄位直接儲存，而對資安沒概念或很菜的開發者在這邊就有可能犯下一個錯誤了! 直接將使用者的密碼存成明碼在DB中...</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TfN5ZYxt3VsBqs_P2sOC7g.png" /></figure><blockquote>這樣的做法，只要DB整個被盜取時就會有使用者密碼外流的風險了 !</blockquote><h4>How to enhance it !?</h4><p>為了改善上面的做法，稍微有經驗的開發者則會將<strong><em>password</em></strong>的內容先透過加密或加密或雜湊相關的演算法來對使用者的密碼做加密!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uHxXGxMdIHrIRwVdULMXeA.png" /></figure><blockquote>這樣做法的好處是，密碼不在那麼的直接攤在陽光下了；且即時DB被整個盜取時，不法分子還要花時間在解碼上!</blockquote><h4>Is that enough ?</h4><p>當然，這樣的作法還是不夠的! 對於專門盜取並駭客來說，這樣只能拖延他們破解的速度，很多時候他們已經有一些特定的破解方式來做了! 較有名的破解方式稱作<a href="https://en.wikipedia.org/wiki/Rainbow_table"><strong><em>rainbow attack</em></strong></a><strong><em>，</em></strong>透過<a href="https://en.wikipedia.org/wiki/Rainbow_table"><strong><em>rainbow attack</em></strong></a>，駭客還是可以有效率的破解他們所取得的資料 !</p><blockquote><a href="https://en.wikipedia.org/wiki/Rainbow_table"><strong><em>rainbow attack</em></strong></a>:<br><em>更白話的來講，是一種字典類型的攻擊；<br>透過把常見的密碼直接轉換成對應的hash 編碼值(md5, sha1…)，透過這樣的比對，可以更有效率的去破解資料</em></blockquote><p>從這邊來看，更有經驗的開發者也提供一些方式抵檔這類型的攻擊；最簡單的作法是，讓使用者的密碼在加密前，是由特別且獨立字串組成，再經過加密算法後就會產生出獨一無二的加密值...</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RZMZa8i6xmsEYxrI6LxSVA.png" /></figure><blockquote>在這邊為每個使用者新增一個<strong>salt</strong>亂數，而<strong>salt</strong>的用途是用來加入到每個要加密前的password，以組成一個新的密碼字串；最後再透過加密算法做加密 !<br>ex:<br>dd4b77abcfa7d5e4bb1ea2760fba32d8 = md5_hash(457fa569b8_impassword)</blockquote><p>這樣的作法可以確保新產生的密碼一定也是唯一值，而且可以避免像<a href="https://en.wikipedia.org/wiki/Rainbow_table"><strong><em>rainbow attack</em></strong></a>這類型的攻擊 !</p><h4>Reference:</h4><ul><li><a href="http://stackoverflow.com/questions/1200326/what-is-the-datatype-for-a-password-in-postgresql">What is the datatype for a password in PostgreSQL?</a></li><li><a href="http://blog.codinghorror.com/youre-probably-storing-passwords-incorrectly/">You&#39;re Probably Storing Passwords Incorrectly</a></li><li><a href="http://stackoverflow.com/questions/1012724/what-exactly-is-a-rainbow-attack">What exactly is a rainbow attack?</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd89e6467212" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Do you believe『 The Core Thing 』?]]></title>
            <link>https://medium.com/@george_chen/the-core-thing-56be2aea6579?source=rss-613cf05ecb65------2</link>
            <guid isPermaLink="false">https://medium.com/p/56be2aea6579</guid>
            <category><![CDATA[entrepreneurship]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[George Chen]]></dc:creator>
            <pubDate>Sat, 17 Oct 2015 07:37:45 GMT</pubDate>
            <atom:updated>2015-10-18T07:12:44.954Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*44ESAVdd9MCjlRRYFbKUnw.jpeg" /><figcaption>photo from <a href="https://www.pexels.com/photo/landscape-mountains-nature-man-1005/">https://www.pexels.com/photo/landscape-mountains-nature-man-1005/</a></figcaption></figure><p>『<strong><em>The Core Thing</em></strong>』是一個很抽象的概念，白話的解釋有點類似<strong><em>信仰</em></strong>，<strong><em>核心競爭力</em></strong>，或是<strong><em>自信</em></strong></p><p>很早以前就聽過類似的概念，但沒有一個覺得適合的名詞，而在最近一次的<a href="http://www.inside.com.tw/2015/10/08/mosa-set-the-notch-for-taiwan-startup-activities">MOSA Event</a>中再次聽到成功的創業家在聊這個話題時，又覺得很有感觸!</p><blockquote>當一個人，一個團隊或一個產品相信他們的『<strong>The Core Thing</strong>』，那他們就有機會做出點什麼 !</blockquote><h4>What’s『 The Core Thing 』in our normal life</h4><p>這觀念其實早已出現在我們的生活當中，舉凡各種的不同階段來看；</p><ul><li>有能力的人，總是有自信並知道自己擅長哪些任務。</li><li>強的團隊並非總是如大家所想像的，有著固定的角色組成或專業；<br>相反地 ，它們有著不同的組成方式，且它們更專注在如何達成任務。</li><li>好的組職或公司，它們也有著自己的信仰，相信自己的核心優勢所能創造出來的產品或服務。</li></ul><p>這些都是很常見的事實，但卻呼應了『<strong>The Core Thing</strong>』這個概念...</p><h4>So …… let’s find out and believe 『The Core Thing』we have.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56be2aea6579" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Think more …]]></title>
            <link>https://medium.com/@george_chen/thinking-more-6d6fb5c5201e?source=rss-613cf05ecb65------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d6fb5c5201e</guid>
            <category><![CDATA[ideas]]></category>
            <category><![CDATA[entrepreneurship]]></category>
            <category><![CDATA[startup]]></category>
            <dc:creator><![CDATA[George Chen]]></dc:creator>
            <pubDate>Mon, 12 Oct 2015 09:22:03 GMT</pubDate>
            <atom:updated>2015-10-12T09:27:55.319Z</atom:updated>
            <content:encoded><![CDATA[<p>這件事真的蠻重要的，做事除了效率外，更重要的是，我們的方向對了嗎!?寫程式的時候也是，太急著下手，時常會讓寫出來的東西產出更多的問題！</p><p>The more you think, the less bugs you have</p><p><em>錯的方向即使做在快也是浪費時間!</em></p><h4>Collect your idea</h4><p>在做任何一件事的時候，『<strong>想法』</strong>永遠是最直接的起點~! 所以很重要的一點是，你如何在執行前把你的想法完整的呈現出來 ~!!!</p><blockquote>人對於圖形的記憶與聯想能力會遠高於文字記錄，用畫的會是個不錯的方式</blockquote><h4>Share your idea</h4><p>隨著時間的流逝，最初產生的想法也會有所變質~! 所以當有新的想法出現時，除了把它描繪出來外，馬上分享給同事或朋友並一起討論，也會大大的增進做事的有效性</p><blockquote>分享你的想法，並透過他人的回饋來加以修正~!</blockquote><h4>Review on your idea</h4><p>在不同執行階段，整理執行結果並比對最初的想法；這樣除了可以驗證想法是否可行外，還有助於調整當下的工作...</p><blockquote>驗證執行結果與持續的修正，這樣才能夠讓方向保持一致 !</blockquote><p><a href="https://wrkfree.com/"><strong>Wrkfree</strong></a><strong> </strong>的用途之一就是幫助使用者去思考與討論，不提供複雜的功能；讓使用者用最簡單的方式去思考與描繪其idea</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d6fb5c5201e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[good photos]]></title>
            <link>https://medium.com/@george_chen/good-photos-963f8cea382c?source=rss-613cf05ecb65------2</link>
            <guid isPermaLink="false">https://medium.com/p/963f8cea382c</guid>
            <dc:creator><![CDATA[George Chen]]></dc:creator>
            <pubDate>Mon, 29 Jun 2015 16:11:31 GMT</pubDate>
            <atom:updated>2015-06-29T16:18:20.838Z</atom:updated>
            <content:encoded><![CDATA[<ul><li><a href="https://pixabay.com/en/book-pen-open-notebook-empty-731199/">Book, Pen, Open, Notebook, Empty - Free Image on Pixabay</a></li><li><a href="https://www.flickr.com/photos/125167502@N02/14164315519/in/set-72157644611527928/">notebook</a></li><li><a href="https://www.flickr.com/photos/125167502@N02/14185447737/in/set-72157644655809428/">hipster desktop office</a></li><li><a href="https://secure.flickr.com/photos/125167502@N02/14317125527/in/set-72157644943289600/">neourban hipster office 3.0</a></li><li><a href="https://www.flickr.com/photos/125167502@N02/14354989289/in/set-72157645028734620/">office desk</a></li></ul><iframe src="https://cdn.embedly.com/widgets/media.html?src=http%3A%2F%2Fwww.flickr.com%2Fapps%2Fslideshow%2Fshow.swf%3Fv%3D107931&amp;fv=v%3D107931%26offsite%3Dtrue%26lang%3Den-us%26page_show_url%3D%252Fphotos%252F125167502%40N02%252Fsets%252F72157646779778414%252Fshow%252F%26page_show_back_url%3D%252Fphotos%252F125167502%40N02%252Fsets%252F72157646779778414%252F%26set_id%3D72157646779778414%26jump_to%3D&amp;url=https%3A%2F%2Fwww.flickr.com%2Fphotos%2F125167502%40N02%2Fsets%2F72157646779778414%2F&amp;image=https%3A%2F%2Fc1.staticflickr.com%2F1%2F398%2F18530095174_a2e2b601be_b.jpg&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=application%2Fx-shockwave-flash&amp;schema=flickr" width="800" height="450" frameborder="0" scrolling="no"><a href="https://medium.com/media/d1637db864da41e9552391daac3724da/href">https://medium.com/media/d1637db864da41e9552391daac3724da/href</a></iframe><ul><li><a href="https://www.flickr.com/photos/125167502@N02/14354950510/in/set-72157645028734620/">office desk</a></li><li><a href="https://www.flickr.com/photos/125167502@N02/14354986729/in/set-72157645028734620/">office desk</a></li><li><a href="https://www.flickr.com/photos/125167502@N02/14370075880/in/set-72157645464072972/">hipster office</a></li><li><a href="https://www.flickr.com/photos/125167502@N02/14441896787/in/set-72157645218200039">www sourcode analog</a></li><li><a href="https://www.flickr.com/photos/markusspiske/14625076141/in/album-72157645218200039/">book</a></li><li><a href="https://www.flickr.com/photos/markusspiske/19170536161/in/album-72157644655809428/">neourban hipster office desktop</a></li><li><a href="https://pixabay.com/en/study-workroom-office-glimpse-789631/">Study, Workroom, Office, Glimpse - Free Image on Pixabay</a></li><li><a href="https://pixabay.com/en/notes-paper-paper-ball-memo-office-514998/">Notes, Paper, Paper Ball, Memo - Free Image on Pixabay</a></li><li><a href="https://pixabay.com/en/creativity-idea-inspiration-819371/">Creativity, Idea, Inspiration - Free Image on Pixabay</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=963f8cea382c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nginx static file on nodeJs]]></title>
            <link>https://medium.com/@george_chen/nginx-static-file-on-nodejs-e92869524ceb?source=rss-613cf05ecb65------2</link>
            <guid isPermaLink="false">https://medium.com/p/e92869524ceb</guid>
            <dc:creator><![CDATA[George Chen]]></dc:creator>
            <pubDate>Sun, 14 Dec 2014 09:41:29 GMT</pubDate>
            <atom:updated>2014-12-14T09:41:29.717Z</atom:updated>
            <content:encoded><![CDATA[<p>nginx 的強大在於它的運作模式是類似於nodejs的event-driven；基於這樣的特色，加上用c語言實作的高效；讓nginx在處理request 時能夠更快速且不阻塞目前的process</p><h3>why nginx for nodejs ?</h3><p>事實上，nodejs的http module已經實作的非常完整，即使不靠nginx也能夠運作正常；然而nodejs的強項在於處理i/o 類的request，而身為web server又必須壓縮網頁內容，建立https的secure連線，以及讀取並轉送大量的靜態檔…</p><p>這些都必須大量使用到cpu 資源的，所以對nodejs而言反而是個負擔；然而這些tasks 對nginx而言卻是小菜一碟，用c語言實作的nginx剛剛好…</p><h3>basic Configuration example:</h3><h4>basic nginx configuration:</h4><pre># the nginx server instance<br>server {<br>  listen 80;  #設定nginx listen port80<br>  # 設定目前要提供的靜態檔案路徑<br>  root /xxx/xxx/web-server/public;<br>  <br>  # 截取所有的file request中，檔案的附檔名為以下字串時則由nginx 所服務<br>  location ~* ^.+.(css|js|png|svg|map|woff|ttf)$ {<br>    access_log off;<br>    expires 15d;<br>  }</pre><pre>  # 將所有的 url request 則轉送到nodejs web server<br>  location / {<br>    proxy_set_header X-Real-IP $remote_addr;<br>    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>    proxy_set_header Host $http_host;<br>    proxy_set_header X-NginX-Proxy true;<br>    <br>    proxy_pass <a href="http://webserver/">http://loc</a>alhost:300; # nodejs listen port<br>    proxy_redirect off;<br>  }<br>}</pre><h4>basic NodeJs configuration</h4><pre>var http = require(&#39;http&#39;);<br>http.createServer(function (req, res) { <br>  console.log(req.url); <br>  res.end(&#39;works&#39;); <br>}).listen(3000);<br></pre><blockquote>這樣簡單的設定，可以讓所有的client request都送到nginx那，當nginx發現不是static file request時，就將request改送到nodejs listen port=3000</blockquote><h3>Advanced Configuration example:</h3><p>對於一般的web site而言，basic configuration基本上就能滿足需求了；但如果目前的網站上是有隱私或驗證方面的需求時(ex: user a 必須先登入過後才能取得靜態檔案)... <strong>則此時的流程就會變得稍微複雜些…</strong></p><blockquote>URL -&gt; nginx (redirect to node) -&gt; nodejs (authenticate) -&gt; nginx (serve file)</blockquote><p>所以接下來的設定大致如下：</p><h4>1&gt; <strong><em>URL -&gt; nginx (redirect to node)</em></strong></h4><pre># 將所有的 url request 則轉送到nodejs web server<br> location / {<br>   proxy_set_header X-Real-IP $remote_addr;<br>   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br>   proxy_set_header Host $http_host;<br>   proxy_set_header X-NginX-Proxy true;<br> <br>   proxy_pass <a href="http://webserver/">http://loc</a>alhost:300; # nodejs listen port<br>   proxy_redirect off;<br> }</pre><h4><strong>2&gt;<em>nodejs (authenticate) -&gt; nginx (serve file)</em></strong></h4><pre><strong># app 為express.js 所new 出來的instance<em>，</em>這邊就會收到nginx所轉送過來的http request<em><br></em></strong>app.get(‘/’, isSessionAuth, function (req, res) {<br><strong> // 這邊透過middleware去確認目前的session是登入過的</strong><br><strong> // 在response時透過header [</strong>X-Accel-Redirect<strong>] 來轉送處理過後的url到/public下</strong><br>   res.setHeader(‘X-Accel-Redirect’, ‘/public/’);<br>   res.end(‘’);<br> });</pre><blockquote>url request 如果是<strong>http://example.com/,</strong> 則透過上面的設定，目前的url request 會變成<strong>http://example.com/public/ </strong>並轉送回nginx</blockquote><h4>3&gt;nginx (serve file) -&gt; client</h4><pre># 在nginx 這邊我們就必須要新增一個處理/public request的設定<br>location /public {<br><strong>  # 這邊指定其為&quot;internal&quot;，代表外部browser無法透過link要求nginx提供檔案</strong><br>  internal;<br><strong>  # 這邊設定一個這個request的index</strong><br>  index index.html;<br><br>  # 前面設定了root 為<strong>/xxx/xxx/web-server/public，</strong>所以當用這個request <br>  # 去找檔案時是會找不到的，<br>  # 因為它會去搜尋<strong>/xxx/xxx/web-server/public/public/下的index.html</strong><br>  # 所以這邊設定了alias 去重新命名這個轉送的request<br>  alias <strong>/xxx/xxx/web-server/public</strong><br> }</pre><p>透過這樣的設定，可以讓nginx繼續提供靜態檔案的服務，又能確保未經授權的request是拿不到檔案的…</p><h4>Conclusion</h4><p>nodejs在處理static file時，在request不多時，基本上都不會有啥問題；但如果遇到大量的concurrent requests時，除了performance 會往下掉以外，還會大量的使用到memory!</p><p>所以需不需要這麼麻煩的設定這些，還是要看使用者的量；量少的話，nodejs+express 就是個ok的soultion了 !</p><p><a href="http://stackoverflow.com/questions/9967887/node-js-itself-or-nginx-frontend-for-serving-static-files">node.js itself or nginx frontend for serving static files?</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e92869524ceb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Basic NodeJs and  Buffer]]></title>
            <link>https://medium.com/@george_chen/basic-nodejs-and-buffer-2cd69edc18da?source=rss-613cf05ecb65------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cd69edc18da</guid>
            <dc:creator><![CDATA[George Chen]]></dc:creator>
            <pubDate>Fri, 03 Oct 2014 09:33:55 GMT</pubDate>
            <atom:updated>2014-10-03T09:47:59.859Z</atom:updated>
            <content:encoded><![CDATA[<h3>Basic NodeJs and Buffer</h3><h4>Why we need buffer</h4><p>在nodejs中，若需要用到檔案傳輸，搬移，網路串流檔案，或其它較大的物件需要用到大量記憶體時，則就不適合使用javascript的記憶體操作方式…</p><blockquote>如果使用內建的array/object來解決這一類型的需求時，效能上會是一個很大的問題，且會造成v8 花費大量時間處理記憶體的分配…</blockquote><h4>What is Buffer</h4><p><strong>buffer</strong> 簡單來講就是一段記憶體區段；但不同的是，它並不是由v8 所分配出來的，而是由node_buffer 這個c++ module所處理!</p><p>而Buffer的結構會類似於<strong>Array，</strong>其中的每個元素都是16進位的數字。<br>預設是採用<strong>”utf-8&quot;</strong>的編碼</p><h4>new buffer</h4><p>buffer 已經被nodejs編譯成內建的object type；所以使用時不需要引用任何module…</p><pre>var str = ‘abcefs’;<br><strong>// 將str字串存成buffer object；而encoding這邊沒有設定，所以預設採用utf-8</strong><br>var buf = new Buffer(str);<br><strong>// var buf = new Buffer(str, &quot;hex&quot;); 這邊也可以指定hex encoding</strong></pre><pre><strong>// 這邊我們可以看到buf的內容為：&lt;Buffer 61 62 63 65 66 73&gt;</strong><br>console.log(buf);</pre><pre>var buf2 = new Buffer(32);</pre><pre><strong>// buf2的長度=32，類似於array的操作方式</strong><br>console.log(&#39;buf2 length is : &#39;+buf2.length);</pre><pre><strong>// buf2的內容會是&lt;Buffer e0 41 00 01 01 00 00 00 04 00 00 00 01 00 00 00 03 00 00 00 01 00 00 00 c0 03 00 02 01 00 00 00&gt; <br>// 內容為隨機的16進位數字，因為我們在產生這個buffer時沒給它內容</strong><br>console.log(buf2);</pre><h4>basic buffer operation</h4><p>對於建立好的buffer，我們是能夠對它做一些操作的，像是將buffer的某個bytes變動，或是重新指定新的內容到buffer中…</p><pre>// 這邊的buffer 內容會是隨機數<br>var buf2 = new Buffer(5);</pre><pre>// 將字串&quot;abcde&quot;寫入到這個buffer中<br>buf2.write(‘abcde’);</pre><pre><strong>// &lt;Buffer 61 62 63 64 65&gt;  此為utf-8的&quot;abcde&quot;編碼</strong><br>console.log(buf2);</pre><pre>// 重新寫入buf2的內容為&quot;xzxd&quot;<br>buf2.write(‘xzxd’);</pre><pre><strong>// &lt;Buffer 78 7a 78 64 65&gt; 可以看到前面4個bytes會是&quot;xzxd&quot;的編碼<br>// 由於我們只寫4個bytes，所以第5個bytes會保留原先的值 <br>// 如果我們一次寫超過buffer大小時，則buffer module會從頭截取合適大小寫入</strong><br>console.log(buf2);</pre><pre><strong>// 我們也可以使用指定特定bytes的方式賦值，<br>// 這邊要注意的是我們給的值必須是10進位的, 所以要自行換算16進位與10進位的轉換</strong><br>buf2[4] = 120;</pre><pre><strong>// &lt;Buffer 78 7a 78 64 78&gt; 最後一個0x78就是120的轉換</strong><br>console.log(buf2); </pre><pre><strong>// 這邊將buf2環原回string, 預設採用&quot;utf-8&quot; decode <br></strong>console.log(buf2.toString());  <strong>// output =&gt; xzxzx</strong></pre><blockquote>buffer的編碼只有常見的utf-8, hex, ASCII, Base64... 等；如果要使用像Big5這類似的編碼時，需要使用3rdParty module =&gt;<strong> iconv-lite</strong></blockquote><h4>Buffer combination — read stream</h4><p>透過小buffer的連接，我們可以組成一個較大的buffer；而通常會使用這種情況大都是在讀取檔案的內容時…</p><p>但唯一需要注意的是，不應該直接使用array的串接方式，這會有編碼與解碼的問題!!</p><pre>var fs = require(‘fs’);</pre><pre><strong>// words2.txt的內容為：<br>// ABC<br>// 123<br>// GG<br>// DIXX<br>// ZXCKVJ</strong><br>var rs = fs.createReadStream(‘./words2.txt’);</pre><pre>var data = [];<br>var size = 0;</pre><pre><strong>// 每次readStream讀取新的data chunk時，會emit &quot;data&quot; 事件，而這邊<br>// 我們就每次將chunk組成data object，<br>// 必須計算size的原因在於每次push data chunk時，在data array中只會佔一個// element大小，而不是它實際的bytes</strong><br>rs.on(‘data’, function(chunk){<br> data.push(chunk);<br> size+=chunk.length;<br>});</pre><pre><strong>// 最後讀完檔案時，readStream會送出一個&quot;end&quot; event，所以這邊我們可將暫存的<br>// data 物件重新轉換為一個buffer，然後在用toString() 去decode<br>// 如果編碼方式不同時，則可以使用3rdParty module來decode</strong><br>rs.on(‘end’, function () {<br> var buf = Buffer.concat(data, size);<br> var str = buf.toString();<br> console.log(str);<br>});</pre><blockquote>Buffer搭配上stream的操作，充分使用了nodejs的i/o特性；應該要好好的學習這些觀念</blockquote><h3>小記</h3><p>許多內建的node module 都是使用buffer來儲存或串流一些常用的物件，所以在適告的時機點使用buffer是能夠大幅提升nodejs的效能!</p><pre><strong>// CASE1: no pre-buffering the data</strong><br>http.createServer(function(req, res){<br><strong>  // 每次response時，nodejs都必須要將這個字串轉存為buffer才送出!</strong><br>  res.end(&quot;hello world&quot;);<br>});</pre><pre><br><strong>// CASE2: no pre-buffering the data</strong><br>var data = new Buffer(&#39;hello world&#39;);<br>http.createServer(function(req, res){<br><strong> // 使用前面預先建立好的buffer 給client</strong><br> res.end(data);<br>});</pre><blockquote>CASE2的效能會被CASE1的效能好到近1倍</blockquote><p><strong>reference: </strong><a href="http://www.books.com.tw/products/0010644512">深入淺出nodejs</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cd69edc18da" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Basic NodeJs stream]]></title>
            <link>https://medium.com/@george_chen/basic-nodejs-stream-29f7ee88149c?source=rss-613cf05ecb65------2</link>
            <guid isPermaLink="false">https://medium.com/p/29f7ee88149c</guid>
            <dc:creator><![CDATA[George Chen]]></dc:creator>
            <pubDate>Thu, 02 Oct 2014 15:59:43 GMT</pubDate>
            <atom:updated>2014-10-02T15:59:43.337Z</atom:updated>
            <content:encoded><![CDATA[<h4>stream</h4><p>stream是nodejs中處理資料傳輸的特別方式，它將一個較大的物件抽像化成無數個較小的data chunk，接著再將這些較小的data chunk串流到我們會需要用到的地方…</p><h4>Why should I use stream ?</h4><p>由於nodejs的非同步特性，當我們在處理較大的檔案或物件時，所需花費的時間必定增加許多，間接的就會影響到i/o thread的執行時間!<br><strong>=&gt; 造成nodejs 程式效能低弱!</strong></p><blockquote>但如果我們將較大的物件切成較小的data chunk時，這樣我們就可以在每次i/o thread執行時間內，傳送一定數量的data chunk而不阻塞i/o</blockquote><h4>Output readStream content .pipe()</h4><p>針對每個建立出來的stream 物件，我們可以透過stream.pipe()這個function將stream的內容串流到目的端的物件</p><h3>Read Streams</h3><h4>basic usage</h4><p>這種類型的stream主要是用來當作串流到目的端時的來源stream</p><pre>var readStream = require(&#39;stream&#39;).Readable;</pre><pre><strong>// 這邊產生一個新的readstream object</strong><br>var rs = new readStream;<br>rs.push(&#39;test1 &#39;);<br>rs.push(&#39;test2\n&#39;);<br><strong>// 最後push null 表示停止塞東西到readStream中</strong><br>rs.push(null);  <br><br><strong>// pipe(dst)中的參數為目的端的物件(dst)<br>// 最後我們將stream的內容 output到terminal上；</strong><br>rs.pipe(process.stdout);</pre><blockquote>stream.pipe(dst)最後會回傳dst物件，所以pipe()還可以套用function chain的方式，ex: <strong>stream.pipe(a).pipe(b),pipe(c);</strong></blockquote><h4>readStream on demand push</h4><p>前面的範例主要是先將readstream的內容先一次填完以後，最後再去使用readStream這個物件；<br>但這邊還提供了一個更好的方法：<strong>on-demand stream</strong></p><p>透過自訂<strong>rs._read</strong>方法，當有client想要透過stream取得物件時，才去找到對應的data chunks</p><pre>// push chunks on-demand by defining a ._read function<strong><br>var</strong> readStream <strong>=</strong> require(&#39;stream&#39;).Readable;<br><strong>var</strong> rs <strong>=</strong> readStream();<br><br><strong>var</strong> c <strong>=</strong> 97;<br><strong>// 這邊自訂的_read() 只有在每次consumer準備使用它們時才產生data chunk<br>// 到目前的readStream中</strong><br>rs._read = function () {<br><strong>// 如果透過setTimeout，還可以將每次的request延遲輸出</strong><br> rs.push(String.fromCharCode(c<strong>++</strong>));<br> <strong>if</strong> (c <strong>&gt;</strong> &#39;z&#39;.charCodeAt(0)) rs.push(<strong>null</strong>);<br>};<br><br>rs.pipe(process.stdout);</pre><p>The <strong>_read</strong> function will also <strong>get a provisional size parameter as its first argument</strong> that specifies how many bytes the consumer wants to read!</p><p>If you want to create a readable stream that pushes arbitrary values instead of just strings and buffers, make sure to create your readable stream with Readable(<strong>{ objectMode: true }</strong>).</p><blockquote>除了使用pipe()來觸發readStream的讀取時，<a href="https://github.com/substack/stream-handbook"><strong>stream-hanbook</strong></a>這邊還有其它更進階的用法來使用readStream ...</blockquote><h3>write Streams</h3><p>write stream通常指的是可被寫入的stream object；但這個stream 物件是無法當成<strong>來源</strong>來讀取的。<br><strong>EX: 當我們要寫檔的話就需要建立一個writeStream</strong></p><pre>var<strong> </strong>readStream <strong>= </strong>require(‘stream’).Readable;<br>var writeStream = require(‘stream’).Writable;</pre><pre><br><strong>//這邊可以簡單看出，writeStream會是pipe()中的目的端物件</strong><br>readStream.pipe(writeStream);</pre><p>不同於readStream，如果我們要讓自訂的writeStream真的有作用時，就必須自訂<strong>_write()</strong>，每次寫入時這個func都會被呼叫</p><h4>Basic Usage</h4><pre>var Writable = require(‘stream’).Writable;<br>var ws = Writable();</pre><pre><strong>// 這邊我們必須定義一個_write()<br>// chunk, 每個被寫入的data chunk<br>// enc, 編碼的方式 utf-8, buffer ...<br>// next, 每次完成寫入的動作時，就在呼叫next，執行下一次寫入</strong><br>ws._write = function (chunk, enc, next) {<br> console.dir(chunk);<br> next();<br>};</pre><pre>var str = &#39;abcde&#39;;<br><strong>// 這邊可以將任意的字串or字元 寫入到剛定義出來的writeStream中<br>// 而預設的enc格式會是&quot;buffer&quot;</strong><br>ws.write(str);</pre><h4>Write and decoding String</h4><pre>var Writable = require(‘stream’).Writable;<br><strong>// writeStream 這邊的decode設定會讓每次被寫入的內容為utf-8</strong><br>var ws = Writable({ decodeStrings: false });</pre><pre><br>ws._write = function (chunk, enc, next) {<br> console.dir(chunk);<br> next();<br>};</pre><pre>var str = &#39;abcde&#39;;<br>ws.write(str);<br><strong>// 這邊會在_write() output時 show出 &quot;abcde&quot;  而非buffer [xxxxx]</strong></pre><h4>Write from object</h4><pre>var Writable = require(‘stream’).Writable;<br><strong>// 如果要寫入的來源是&quot;object&quot;的話，則必須設定objectMode=true</strong><br>var ws = Writable({ objectMode: true });</pre><pre>var json = {<br> name: ‘abc’,<br> test: true<br>};</pre><pre>ws._write = function (chunk, enc, next) {<br> console.dir(chunk);<br> next();<br>};</pre><pre>ws.write(json);<br><strong>// 這邊我們將這個json 物件透過stream.write() output出去；<br>// 這邊console.log show出來的內容就會是json物件的內容</strong></pre><blockquote>通常會使用到write stream的地方都是在要把目前資料輸出到某個目的地<br><strong>ex: 我們可以建立一個array來儲存stream輸出的內容，每次_write被呼叫時，就儲存當下的data chunk...</strong></blockquote><h3>Transform Stream</h3><p>transform stream則是同時可以提供readStream的特性(providing data), 以及writeStream的特性(output data)；它就像是個&quot;<strong>middleware</strong>&quot;，</p><p>當readStream 的內容pipe()到transformStream以後，它可以做些處理後，再真正的丟給writeStream輸出到目的端 !</p><blockquote>transform stream 必須要再執行<strong>_transform()</strong> 這個<strong>function</strong></blockquote><p><strong>sample code 可以直接參考下面File Stream的用法</strong></p><h3>File Stream</h3><p>最常見的stream的用法就是在file i/o 上，而nodejs 也內建了file streaming的方式…</p><h4>Copy file to another file by streaming</h4><pre>var fs = require(‘fs’);</pre><pre><strong>// 建立 readable file stream and writable stream<br>// 讀檔的檔名 &quot;wrods&quot;，目的端的檔名：&quot;words2&quot;</strong><br>var rStream = fs.createReadStream(‘./words’);<br>var wStream = fs.createWriteStream(‘./words2.txt’);</pre><pre>function upperCaseStream(){<br> var ws = require(‘stream’).Transform();</pre><pre><strong>   // transform stream必須自訂_transform()這個function</strong><br>   ws._transform = function(chunk, enc, next){<br>     var upper = chunk.toString().toUpperCase();<br>  <strong>   // 收到stream時又使用push()的方式  <br>     // 建立成新的readStream，所以又可以當作下一個writeStream的source</strong><br>     this.push(upper);<br>     next();<br>   }<br>   return ws;<br>}</pre><pre>var tStream = new upperCaseStream();</pre><pre><strong>// 每次讀&quot;words&quot;的data chunk時，</strong><br><strong>// 透過中間的tStream將所有的內容都轉換成大寫字母，<br></strong>// <strong>最後再將資料寫到&quot;words2.txt&quot;上<br></strong>rStream.pipe(tStream).pipe(wStream);</pre><h3><strong>reference:</strong></h3><ul><li><a href="http://tagtree.tv/intro-to-node-streams">node streams</a></li><li><a href="http://blog.caesarchi.com/2014/09/stream.html">資料讀取提升法則，精選必學 Stream 處理</a></li><li><a href="https://github.com/substack/stream-handbook">substack/stream-handbook</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29f7ee88149c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to keep pushing yourself]]></title>
            <link>https://medium.com/@george_chen/how-to-keep-pushing-yourself-b9301e0647d9?source=rss-613cf05ecb65------2</link>
            <guid isPermaLink="false">https://medium.com/p/b9301e0647d9</guid>
            <dc:creator><![CDATA[George Chen]]></dc:creator>
            <pubDate>Tue, 30 Sep 2014 15:43:16 GMT</pubDate>
            <atom:updated>2014-09-30T15:46:18.325Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8f1tcmEnqS2UyJJJxJlJhQ.jpeg" /></figure><h3>Passion</h3><p>這段時間以來，真的發現自由工作者的心態調整是很重要的…</p><p>每天只有自己與電腦中密密麻麻的程式碼，規劃好的工作進度，以及看似高度自由的時間…<br>但這樣日復一日下來，發現&quot;熱情&quot;其實是會被消磨掉的；很多時候當你認為你能夠控制某件事情時，其實你無法真的可以控制它…</p><p>正當我的熱情被消磨時，前陣子的COSCUP剛好又幫我注了劑強心針，<strong>改變世界，讓世界變得更好 </strong>這個初衷又被重新燃起…</p><blockquote><em>熱情可以讓你全心投入</em>一件事物當中，所以你必須時刻的注意你的熱情，適時的給予外在的刺激</blockquote><h3>Take a break</h3><p>除了熱情外，另一個要注意的則是<strong>休息...<br></strong>維持紀律是很重要的，總以為自己能善用所有的工作時間，但卻忘了休息… 這點所造成的負作用是很嚴重的!</p><p>忘不清已經連續工作多少天了… 週末的感覺都快忘光了，只記得每週日有個進度會議要討論，剩下時間就是完成上週所訂出來的進度與當個稱職的<strong>碼農</strong></p><p>直到前兩天產品進入了下一個階段，但記得當天是很倦怠的… 強烈的疲備感湧入，而且有一絲想放棄的念頭!!!當下知道該休息了，這段時間我確實完成了許多自己訂的進度，但也寫出了很多糟糕的程式碼!</p><p><strong><em>這樣的工作衝刺，其實進度上是有回饋的，想法真的在轉變成產品了…<br>但卻也悄稍產生了些負面想法!</em></strong></p><blockquote>當覺得該休息時，就不該繼續盲目的亂衝；<br>否則一定會產生更不好的後果…</blockquote><h3>Learning</h3><p>這星期的工作就變得相對簡單許多，<strong>放假與學習!</strong></p><p>放慢腳步，好好的把所有學到的資訊內化成知識才是現階段更重要的事!很多時候我們知道要把事情做好，是需要兼顧效率與效果；但深處其中時，其實我們很難發現自己的狀態…</p><p><em>每天重複的發生這些事時，倦怠感自然會快速累積…</em></p><p>目前正好已經處於充電狀態<strong>(放假+學習)</strong>的第二天，但感受到的回復是無法言喻的...疲備感慢慢消失，對於技術知識的理解又有慢慢加深的感覺!</p><blockquote>每天趕著進度，囫圇吞棗的把查詢到的資訊用在工作上，看似有效率的完成進度，卻失去了效果，寫出許多品質不一的程式碼…</blockquote><h3>Freelance x startup</h3><p>全心投入當個自由工作者也已經半年了…<br>當個自由工作者是件幸福的事嗎? 為自己工作真的很快樂嗎?</p><p>老實說，真的不後悔當初就離職回家鄉做自己想做的事；<br>但卻也未必是每天都很快樂的…</p><p>有可能會遇上煩人的事，會遇到挫折的事，當然也有開心的事，但是有時當下找不到人分享也是蠻悲衰的XD</p><p>不過，當個freelance的<strong>好處就是時間，工作內容</strong>吧…</p><p>這段時間，大量的資訊湧入，每天都處於跟時間賽跑 !<br>需要時間把新的知識內化，需要時間把startup上的工作進度做完，需要時間把規劃startup的方向與雜事，需要時間運動，… 等等</p><p>當長時間這樣行程滿滿的狀態時，其實本身是不會發覺自己正處於高壓的狀態，而自己應該要有一套應對方式! 當發生時能夠更有效的調整心態</p><blockquote>創業是件長遠的路，好的心態才能持續的堅持下去…</blockquote><h4><strong>Passion x Rest x Learn , then keep going !</strong></h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b9301e0647d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>