此题来自nodeschool第二个教学模块Stream Adventure,这个题是第七题HTTP SERVER 题目的要求是把通过浏览器POST进来的内容转成大写直接输出给浏览器(最下面贴有完整题目)
我的实现如下
var tr = through(function(buf){
this.queue(buf.toString().toUpperCase());
});
var server = http.createServer(function(req,res){
if(req.method == 'POST'){
req.pipe(tr).pipe(res);
}else{
res.end('please send a post request');
}
});
server.listen(Number(process.argv[2]));
但很可惜通不过测试,测试过程如下
ACTUAL EXPECTED
"YET" "YET"
"HOW" "HOW"
"TO" "TO"
"BURGEON." "BURGEON."
"IT'S" "IT'S"
"MEANT" !== "MILLIEMS"
"YET" !== "MEANT"
"HOW" !== "OF"
"TO" !== "MILLIEMS"
"BURGEON." !== "CENTIMENTS"
"IT'S" !== "OF"
"MILLIEMS" !== "DEADLOST"
"MEANT" !== "CENTIMENTS"
"OF" !== "OR"
"MILLIEMS" !== "DEADLOST"
"CENTIMENTS" !== "MISLAID"
"OF" !== "OR"
"DEADLOST" !== "ON"
"CENTIMENTS" !== "MISLAID"
"OR" !== "THEM"
"DEADLOST" !== "ON"
"MISLAID" !== "BUT,"
我发现开始数个测试都通过了,但是到了后面,发现输出的内容比输入慢,每次对应的输出都是之前输入的内容. 这是我比较疑惑的地方,为什么会出现这种情况.
而正确答案的代码是这样的
var http = require('http');
var through = require('through');
var server = http.createServer(function (req, res) {
if (req.method === 'POST') {
req.pipe(through(function (buf) {
this.queue(buf.toString().toUpperCase());
})).pipe(res);
}
else res.end('send me a POST\n');
});
server.listen(parseInt(process.argv[2]));
不知道这两种处理有什么区别,我只是把内建函数换成了变量.
题目的全部内容如下
HTTP SERVER
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[@-](/user/-) 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.
To verify your program, run: stream-adventure verify program.js.