UNPKG

workshopper-browser-guide

Version:

Create an html browser version of the exercise descriptions

111 lines (105 loc) 6.37 kB
<!doctype html> <html class="no-js" lang="zh-tw"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>learnyounode Guide</title> <meta name="description" content="learn git and github"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="assets/css/style.css"> <link rel="stylesheet" href="assets/css/code.css"> <link href='assets/fonts/fonts.css' rel='stylesheet' type='text/css'> </head> <body> <!--[if lt IE 8]> <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> <![endif]--> <header class="site-header"> <div class="nav u-posFixed"> <ul class="nav-lang"> <li><a href="time_server.html" >English</a></li> <li><a href="time_server.es.html" >Español</a></li> <li><a href="time_server.ja.html" >日本語</a></li> <li><a href="time_server.pt-br.html" >Português (Brasil)</a></li> <li><a href="time_server.ru.html" >Русский</a></li> <li><a href="time_server.zh-cn.html" >中文 (中国)</a></li> <li><a href="time_server.zh-tw.html" >中文 (臺灣)</a></li> </ul> <div class="wrap-width u-textCenter"> <a href="juggling_async.zh-tw.html" <span class="u-floatLeft hand"></span> </a> <a class="filledblock" href="index.zh-tw.html">learnyounode</a> <a href="http_file_server.zh-tw.html" <span class="u-floatRight hand"></span> </a> </div> </div> <div class="wrapper"> <div class="u-floatLeft"> <span class="all-caps">CHALLENGE</span> <h2 class="challenge-name">TIME SERVER</h2> </div> <div class="u-floatRight u-textRight"> <span class="all-caps">NUMBER</span> <h2 class="challenge-name">10 / 13</h2> </div> </div> </header> <div class="wrapper"> <p>撰寫一個 <strong>TCP 時間伺服器</strong></p> <p>您的伺服器應該持續堅聽在第一個參數提供的 port 上。每個連線,您都必須以底下的格式回應當前的日期及24小時制的時間:</p> <pre><code><span class="hljs-string">"YYYY-MM-DD hh:mm"</span> </code></pre><p>緊接著是一個 <strong>換行(newline)</strong> 字元。月、日、小時、時間都必須 <em>填入零</em> 到成為2位數。例如:</p> <pre><code><span class="hljs-string">"2013-07-06 17:42"</span> </code></pre><hr> <h2 id="-">提示</h2> <p>在這個習題中,您必須建立一個 TCP 伺服器。這裡不涉及任何 HTTP 協議,所以您需要使用擁有完整基礎網路功能,屬於 Node 核心的 <code>net</code> 模組。</p> <p>這個 <code>net</code> 模組有一個名為 <code>net.createServer()</code> 的方法,這個方法需要一個 callback 函式作為參數。不像其他的 Node callback 函式,作為參數傳入的 callback 函式會被 <code>createServer()</code> 呼叫不只一次。您的伺服器所收到的每個連線都會呼叫 callback 函式。這個 callback 函式有以下的語法特徵:</p> <pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callback</span> <span class="hljs-params">(socket)</span> </span>{ <span class="hljs-comment">/* ... */</span> } </code></pre> <p><code>net.createServer()</code> 還會回傳一個您的 <code>server</code> 的實例(instance)。您必須呼叫 <code>server.listen(portNumber)</code> 以開始監聽特定的 port 。</p> <p>一個標準的 Node TCP 伺服器大概像這個樣子:</p> <pre><code class="lang-js"><span class="hljs-keyword">var</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'net'</span>) <span class="hljs-keyword">var</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(socket)</span> </span>{ <span class="hljs-comment">// socket handling logic</span> }) server.listen(<span class="hljs-number">8000</span>) </code></pre> <p>記得要使用第一個參數傳給您的 port number 。</p> <p><code>socket</code> 物件包含一堆和連線有關的 meta-data ,不過這也是一個可讀、可寫的 Node 雙工串流(duplex Stream)。在這個習題中,我們只需要寫入資料,然後關閉 socket。</p> <p>使用 <code>socket.write(data)</code> 可以對 socket 寫入資料,以及使用 <code>socket.end()</code> 以關閉 socket 。另外, <code>.end()</code> 方法也可以加上一個 data 物件作為參數,所以您可以很簡單的這樣使用: <code>socket.end(data)</code></p> <p>要閱讀 <code>net</code> 模組的文件,可以在瀏覽器中打開這個頁面:</p> <p> <a href="../node_apidoc/net.html">/node_apidoc/net.html</a></p> <p>您要從 <code>new Date()</code> 建立一個自定格式的日期。這個方法的使用方式如下:</p> <pre><code class="lang-js">date.getFullYear() date.getMonth() <span class="hljs-comment">// starts at 0</span> date.getDate() <span class="hljs-comment">// returns the day of month</span> date.getHours() date.getMinutes() </code></pre> <p>您如果想要再大膽一點,可以使用 npm 的 <code>strftime</code> 套件。這個 <code>strftime(fmt, date)</code> 函式使用的日期格式參數和 unix 系統的 <code>date</code> 命令相同。您可以在這裡了解更多關於 strftime 套件的使用方法: <a href="https://github.com/samsonjs/strftime">https://github.com/samsonjs/strftime</a></p> <hr> <div class="prenext"> <div class="u-floatLeft"> <a href="juggling_async.zh-tw.html" class="u-inline-block all-caps">JUGGLING ASYNC <div></div> </a> </div> <div class="u-textRight u-floatRight"> <a href="http_file_server.zh-tw.html" class="u-inlineBlock all-caps">HTTP FILE SERVER <div></div> </a> </div> </div> <footer> <!-- <ul> <li class="all-caps"><a href="index.html"><strong>Challenges</strong></a></li> <li class="all-caps"> <a href="https://github.com/rvagg/learnyounode/issues/new" target="_blank">Open an Issue</a> </li> </ul> --> </footer> </div> </body> </html>