nodejs - 通过包 - 使请求同步

时间:2014-05-17 23:31:24

标签: node.js

我正在进行nodeschool的流式冒险 - 6这是我的代码(question here):

var capitalize = through(function (chunk) {
    this.queue(chunk.toString().toUpperCase() + '\n')
})

var server = http.createServer(function (request, response) {
    request.pipe(capitalize).pipe(response)
})

这是测试运行的样子(https://dpaste.de/mzn3

虽然这不起作用,但这个确实:

var server = http.createServer(function (request, response) {
    request.pipe(through(function (chunk) {
        this.queue(chunk.toString().toUpperCase() + '\n')
    })).pipe(response)
})

这个测试运行:https://dpaste.de/rNT2

我认为它是因为第一个是非阻塞的,这就是为什么我在一些其他响应之后得到一些请求的响应,但是它是如何非阻塞而第二个阻塞?对服务器的请求可以并行工作,对吗?

1 个答案:

答案 0 :(得分:0)

这是因为您为多个同时请求使用相同的直通流实例。

如果您仍想将其分开,可以执行以下操作:

function makeCapitalize() {
  return through(function (chunk) {
    this.queue(chunk.toString().toUpperCase() + '\n')
  })
}

var server = http.createServer(function (request, response) {
  request.pipe(makeCapitalize()).pipe(response)
})
相关问题