UNPKG

workshopper-browser-guide

Version:

Create an html browser version of the exercise descriptions

108 lines (102 loc) 5.38 kB
<!doctype html> <html class="no-js" lang="en"> <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="http_json_api_server.html" >English</a></li> <li><a href="http_json_api_server.es.html" >Español</a></li> <li><a href="http_json_api_server.ja.html" >日本語</a></li> <li><a href="http_json_api_server.pt-br.html" >Português (Brasil)</a></li> <li><a href="http_json_api_server.ru.html" >Русский</a></li> <li><a href="http_json_api_server.zh-cn.html" >中文 (中国)</a></li> <li><a href="http_json_api_server.zh-tw.html" >中文 (臺灣)</a></li> </ul> <div class="wrap-width u-textCenter"> <a href="http_uppercaserer.html" <span class="u-floatLeft hand"></span> </a> <a class="filledblock" href="index.html">learnyounode</a> <a href="index.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">HTTP JSON API SERVER</h2> </div> <div class="u-floatRight u-textRight"> <span class="all-caps">NUMBER</span> <h2 class="challenge-name">13 / 13</h2> </div> </div> </header> <div class="wrapper"> <p>Write an HTTP <strong>server</strong> that serves JSON data when it receives a GET request to the path &#39;/api/parsetime&#39;. Expect the request to contain a query string with a key &#39;iso&#39; and an ISO-format time as the value.</p> <p>For example:</p> <p> /api/parsetime?iso=2013-08-10T12:10:15.474Z</p> <p>The JSON response should contain only &#39;hour&#39;, &#39;minute&#39; and &#39;second&#39; properties. For example:</p> <pre><code class="lang-json">{ <span class="hljs-string">"hour"</span>: <span class="hljs-number">14</span>, <span class="hljs-string">"minute"</span>: <span class="hljs-number">23</span>, <span class="hljs-string">"second"</span>: <span class="hljs-number">15</span> } </code></pre> <p>Add second endpoint for the path &#39;/api/unixtime&#39; which accepts the same query string but returns UNIX epoch time in milliseconds (the number of milliseconds since 1 Jan 1970 00:00:00 UTC) under the property &#39;unixtime&#39;. For example:</p> <pre><code class="lang-json">{ <span class="hljs-string">"unixtime"</span>: <span class="hljs-number">1376136615474</span> } </code></pre> <p>Your server should listen on the port provided by the first argument to your program.</p> <hr> <h2 id="hints">HINTS</h2> <p>The <code>request</code> object from an HTTP server has a <code>url</code> property that you will need to use to <em>&quot;route&quot;</em> your requests for the two endpoints.</p> <p>You can parse the URL and query string using the Node core &#39;url&#39; module. <code>url.parse(request.url, true)</code> will parse content of request.url and provide you with an object with helpful properties.</p> <p>For example, on the command prompt, type:</p> <pre><code class="lang-sh">$ node -pe <span class="hljs-string">"require('url').parse('/test?q=1', true)"</span> </code></pre> <p>Documentation on the <code>url</code> module can be found by pointing your browser here: <a href="../node_apidoc/url.html">/node_apidoc/url.html</a></p> <p>Your response should be in a JSON string format. Look at <code>JSON.stringify()</code> for more information.</p> <p>You should also be a good web citizen and set the Content-Type properly:</p> <pre><code class="lang-js">res.writeHead(<span class="hljs-number">200</span>, { <span class="hljs-string">'Content-Type'</span>: <span class="hljs-string">'application/json'</span> }) </code></pre> <p>The JavaScript <code>Date</code> object can print dates in ISO format, e.g. <code>new Date().toISOString()</code>. It can also parse this format if you pass the string into the <code>Date</code> constructor. <code>Date#getTime()</code> will also come in handy.</p> <hr> <div class="prenext"> <div class="u-floatLeft"> <a href="http_uppercaserer.html" class="u-inline-block all-caps">HTTP UPPERCASERER <div></div> </a> </div> <div class="u-textRight u-floatRight"> <a href="index.html" class="u-inlineBlock all-caps"> <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>