Чем отличаются эти два потока Node?

Я пытаюсь решить эту проблему:

https://github.com/workshopper/stream-adventure/blob/master/problems/http_server/problem.txt

Мне кажется, что мое решение очень похоже на официальное, но ничего не дает. Почему бы и нет?

mine.js

var through = require('through2')
var http = require('http')
var stream = through(goingin, goingout)
var port = process.argv[2]

var server = http.createServer(function(req, res){
    req.pipe(stream).pipe(res)

})

function goingin(buf, _, next){
    this.write(buf.toString().toUpperCase())
    next()
}

function goingout(done){
    done()
}

server.listen(port)

official.js

// Вот эталонное решение:

  var http = require('http');
  var through = require('through2');

  var server = http.createServer(function (req, res) {
      if (req.method === 'POST') {
          req.pipe(through(function (buf, _, next) {
              this.push(buf.toString().toUpperCase());
              next();
          })).pipe(res);
      }
      else res.end('send me a POST\n');
  });
  server.listen(parseInt(process.argv[2]));

0 ответов

Другие вопросы по тегам