Free Code Camp and Learning Node: Uppercaserer

We’re almost done with learnyounode! This was another one that looks harder than it is.  Here we go!

First, as always, the directions and hints:

HTTP UPPERCASERER (Exercise 12 of 13)
  Write an HTTP server that receives only POST requests and converts incoming POST body characters to upper-case and returns it to the client.
  Your server should listen on the port provided by the first argument to your program.
 ─────────────────────────────────────────────────────────────────────────────
 ## HINTS
  While you’re not restricted to using the streaming capabilities of the request and response objects, it will be much easier if you do.
  There are a number of different packages in npm that you can use to “transform” stream data as it’s passing through. For this exercise the through2-map package offers the simplest API.
  through2-map allows you to create a transform stream using only a single function that takes a chunk of data and returns a chunk of data. It’s designed to work much like Array#map() but for streams:
  var map = require(‘through2-map’)
     inStream.pipe(map(function (chunk) {
       return chunk.toString().split(”).reverse().join(”)
     })).pipe(outStream)
  In the above example, the incoming data from inStream is converted to a String (if it isn’t already), the characters are reversed and the result is passed through to outStream. So we’ve made a chunk character reverser! Remember though that the chunk size is determined up-stream and you have little control over it for incoming data.
  To install through2-map type:
     $ npm install through2-map
  If you don’t have an Internet connection, simply make a node_modules directory and copy the entire directory for the module you want to use from inside the learnyounode installation directory:
  file:///home/ubuntu/.nvm/versions/node/v4.4.3/lib/node_modules/learnyounode/node_modules/through2-map
  Documentation for through2-map has been installed along with learnyounode on your system and you can read them by pointing your browser here:

  file:///home/ubuntu/.nvm/versions/node/v4.4.3/lib/node_modules/learnyounode/docs/through2-map.html

Okay, first order of business is to install the package.  So, at the $ prompt we type, as instructed, npm install through2-map.  We get this back:

npm WARN package.json chat-example@0.0.0 No repository field.
npm WARN package.json chat-example@0.0.0 No license field.
through2-map@2.0.0 node_modules/through2-map
├── xtend@4.0.1

└── through2@2.0.1 (readable-stream@2.0.6)

Next, we know we’re going to need to require it in the file.  So we create a new file, and this goes at the top:

</div>
<div>var map = require('through2-map');</div>
<div>

We are also going to need an HTTP server, so we add the require statement for that and the skeleton they gave us from before:

</div>
<div>var map = require('through2-map');</div>
<div>var http = require('http');</div>
<div></div>
<div>  var server = http.createServer(function (req, res) {</div>
<div>  // request handling logic...</div>
<div>  })</div>
<div>  server.listen(8000);</div>
<div>

The port is supplied as a command line variable again, so we assign it to a variable:

</div>
<div>var map = require('through2-map');</div>
<div>var http = require('http');</div>
<div>var port = process.argv[2];</div>
<div></div>
<div>  var server = http.createServer(function (req, res) {</div>
<div>  // request handling logic...</div>
<div>  })</div>
<div>  server.listen(port);</div>
<div>

Now for the request handling logic.  They gave us the skeleton above:

</div>
<div>inStream.pipe(map(function (chunk) {</div>
<div>       return chunk.toString().split('').reverse().join('')</div>
<div>     })).pipe(outStream)</div>
<div>

All we need to do is figure out what the inStream and outStream are, and change it from reversing the string to converting it to uppercase.  The streams are actually simple; note that in the hints they say “While you’re not restricted to using the streaming capabilities of the request and response objects, it will be much easier if you do.”  I took this to mean that request and response are our stream objects.  As far as the string conversion, JavaScript has a nice string method called toUpperCase(), and so now our request handling logic looks like this:

</div>
<div>req.pipe(map(function (chunk) {</div>
<div>       return chunk.toString().toUpperCase();</div>
<div>     })).pipe(res);</div>
<div>

We run it, and lo and behold, we passed!  Here’s the final program:

</div>
<div>var map = require('through2-map');</div>
<div>var http = require('http');</div>
<div>var port = process.argv[2];</div>
<div></div>
<div>var server = http.createServer(function (req, res) {</div>
<div></div>
<div>    req.pipe(map(function (chunk) {</div>
<div>       return chunk.toString().toUpperCase();</div>
<div>     })).pipe(res);</div>
<div>       // request handling logic...</div>
<div>     })</div>
<div>server.listen(port);</div>
<div>

All right!  One last exercise and we are done with learnyounode.  See you next time!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s