-
Notifications
You must be signed in to change notification settings - Fork 0
/
uppercaser.js
54 lines (41 loc) · 1.59 KB
/
uppercaser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
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 port 8000.
----------------------------------------------------------------------
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.
*/
var http = require('http');
var fs = require('fs');
var server = http.createServer(function(req, res) {
if(req.method='POST'){
var body='';
req.on('data', function(chunk){
body+=chunk;
});
req.on('end', function(){
res.write(body.toUpperCase());
res.end();
});
}
});
server.listen(8000);