node.js - NodeJS - 如何在不缓冲的情况下流式传输请求正文

标签 node.js http stream pipe epipe

在下面的代码中,我无法弄清楚为什么 req.pipe(res) 不起作用,但也不会引发错误。预感告诉我这是由于 nodejs 的异步行为,但这是一个非常简单的情况,没有回调。

我错过了什么?

http.createServer(function (req, res) {

  res.writeHead(200, { 'Content-Type': 'text/plain' });

  res.write('Echo service: \nUrl:  ' + req.url);
  res.write('\nHeaders:\n' + JSON.stringify(req.headers, true, 2));

  res.write('\nBody:\n'); 

  req.pipe(res); // does not work

  res.end();

}).listen(8000);

这是 curl :

➜  ldap-auth-gateway git:(master) ✗ curl -v -X POST --data "test.payload" --header "Cookie:  token=12345678" --header "Content-Type:text/plain" localhost:9002 

这是调试输出(请参阅上传的正文):

  About to connect() to localhost port 9002 (#0)
  Trying 127.0.0.1...
    connected
    Connected to localhost (127.0.0.1) port 9002 (#0)
  POST / HTTP/1.1
  User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5
  Host: localhost:9002
  Accept: */*
  Cookie:  token=12345678
  Content-Type:text/plain
  Content-Length: 243360
  Expect: 100-continue

  HTTP/1.1 100 Continue
  HTTP/1.1 200 OK
  Content-Type: text/plain
  Date: Sun, 04 Aug 2013 17:12:39 GMT
  Connection: keep-alive
  Transfer-Encoding: chunked

并且服务响应而不回显请求正文:

Echo service: 
Url:  /
Headers:
{
  "user-agent": "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5",
  "host": "localhost:9002",
  "accept": "*/*",
  "cookie": "token=12345678",
  "content-type": "text/plain",
  "content-length": "243360",
  "expect": "100-continue"
}

... 最后的 curl 调试是

Body:
 Connection #0 to host localhost left intact
 Closing connection #0

此外,当我对大型请求正文进行压力测试时,我收到 EPIPE 错误。我怎样才能避免这种情况?

-- 编辑:通过反复试验,我确实让它工作了,它仍然指向一个时间问题。虽然仍然很奇怪,因为超时会导致返回有效负载,但不介意超时持续时间。换句话说,无论我将超时设置为 5 秒还是 500 秒,有效负载都会正确地通过管道返回到请求并终止连接。

这里是编辑:

http.createServer(function (req, res) {

    try {
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.write('Echo service: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
      res.write('\nBody:"\n');
      req.pipe(res);
    } catch(ex) {
      console.log(ex);
      // how to change response code to error here?  since headers have already been written?
    } finally {
      setTimeout((function() {
        res.end();
      }), 500000);
    }

}).listen(TARGET_SERVER.port);

?

最佳答案

管道请求到资源。 Req 是可读流,响应是可写流。它应该可以工作

   http.createServer(function (req, res) {

       res.writeHead(200, { 'Content-Type': 'text/plain' });    
       res.write('Echo service: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));

       // pipe request body directly into the response body
       req.pipe(res);       

   }).listen(9002);

关于node.js - NodeJS - 如何在不缓冲的情况下流式传输请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17960370/

相关文章:

ios - NSURLRequest 什么时候超时?

来自 HTTP 调用的 Angular 动态模板

json - 如何使用 JSONStream 对大对象进行字符串化

python - 使用 cherrypy 流式传输文件

node.js - NodeJS 中的 Promise 困惑

node.js - 扩展 NodeJS 聊天室 - 在 Redis 中存储连接状态

http - 在 AWS-IoT 中使用 http 列出事物 api?

python - 如何在Python中创建 'word stream'和 'document stream'?

node.js - 不同语言的生产者和消费者的 Redis 消息队列

javascript - Node js函数返回[object Object]而不是字符串值