Created
March 10, 2015 14:36
-
-
Save nelson777/7e80ff12ee96fa7967c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In this challenge, write an http server that uses a through stream to write back | |
the request stream as upper-cased response data for POST requests. | |
Streams aren't just for text files and stdin/stdout. Did you know that http | |
request and response objects from node core's `http.createServer()` handler are | |
also streams? | |
For example, we can stream a file to the response object: | |
var http = require('http'); | |
var fs = require('fs'); | |
var server = http.createServer(function (req, res) { | |
fs.createReadStream('file.txt').pipe(res); | |
}); | |
server.listen(process.argv[2]); | |
This is great because our server can response immediately without buffering | |
everything in memory first. | |
We can also stream a request to populate a file with data: | |
var http = require('http'); | |
var fs = require('fs'); | |
var server = http.createServer(function (req, res) { | |
if (req.method === 'POST') { | |
req.pipe(fs.createWriteStream('post.txt')); | |
} | |
res.end('beep boop\n'); | |
}); | |
server.listen(process.argv[2]); | |
You can test this post server with curl: | |
$ node server.js 8000 & | |
$ echo hack the planet | curl -d@- http://localhost:8000 | |
beep boop | |
$ cat post.txt | |
hack the planet | |
Your http server should listen on the port given at process.argv[2] and convert | |
the POST request written to it to upper-case using the same approach as the | |
TRANSFORM example. | |
As a refresher, here's an example with the default through callbacks explicitly | |
defined: | |
var through = require('through') | |
process.stdin.pipe(through(write, end)).pipe(process.stdout); | |
function write (buf) { this.queue(buf) } | |
function end () { this.queue(null) | |
Do that, but send upper-case data in your http server in response to POST data. | |
Make sure to `npm install through` in the directory where your solution file | |
lives. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment