UNPKG

workshopper-browser-guide

Version:

Create an html browser version of the exercise descriptions

94 lines (88 loc) 4.79 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_client.html" >English</a></li> <li><a href="http_client.es.html" >Español</a></li> <li><a href="http_client.ja.html" >日本語</a></li> <li><a href="http_client.pt-br.html" >Português (Brasil)</a></li> <li><a href="http_client.ru.html" >Русский</a></li> <li><a href="http_client.zh-cn.html" >中文 (中国)</a></li> <li><a href="http_client.zh-tw.html" >中文 (臺灣)</a></li> </ul> <div class="wrap-width u-textCenter"> <a href="make_it_modular.html" <span class="u-floatLeft hand"></span> </a> <a class="filledblock" href="index.html">learnyounode</a> <a href="http_collect.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 CLIENT</h2> </div> <div class="u-floatRight u-textRight"> <span class="all-caps">NUMBER</span> <h2 class="challenge-name">7 / 13</h2> </div> </div> </header> <div class="wrapper"> <p>Write a program that performs an HTTP GET request to a URL provided to you as the first command-line argument. Write the String contents of <strong>each</strong> &quot;data&quot; event from the response to a new line on the console (stdout).</p> <hr> <h2 id="hints">HINTS</h2> <p>For this exercise you will need to use the <code>http</code> core module.</p> <p>Documentation on the <code>http</code> module can be found by pointing your browser here: <a href="../node_apidoc/http.html">/node_apidoc/http.html</a></p> <p>The <code>http.get()</code> method is a shortcut for simple GET requests, use it to simplify your solution. The first argument to <code>http.get()</code> can be the URL you want to GET; provide a callback as the second argument.</p> <p>Unlike other callback functions, this one has the signature:</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">(response)</span> </span>{ <span class="hljs-comment">/* ... */</span> } </code></pre> <p>Where the <code>response</code> object is a Node <strong>Stream</strong> object. You can treat Node Streams as objects that emit events. The three events that are of most interest are: &quot;data&quot;, &quot;error&quot; and &quot;end&quot;. You listen to an event like so:</p> <pre><code class="lang-js">response.on(<span class="hljs-string">"data"</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-params">(data)</span> </span>{ <span class="hljs-comment">/* ... */</span> }) </code></pre> <p>The &quot;data&quot; event is emitted when a chunk of data is available and can be processed. The size of the chunk depends upon the underlying data source.</p> <p>The <code>response</code> object / Stream that you get from <code>http.get()</code> also has a <code>setEncoding()</code> method. If you call this method with &quot;utf8&quot;, the &quot;data&quot; events will emit Strings rather than the standard Node <code>Buffer</code> objects which you have to explicitly convert to Strings.</p> <hr> <div class="prenext"> <div class="u-floatLeft"> <a href="make_it_modular.html" class="u-inline-block all-caps">MAKE IT MODULAR <div></div> </a> </div> <div class="u-textRight u-floatRight"> <a href="http_collect.html" class="u-inlineBlock all-caps">HTTP COLLECT <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>