【问题标题】:Strange stream behaviour node.js/knox奇怪的流行为 node.js/knox
【发布时间】:2013-03-31 22:17:58
【问题描述】:

我一直在研究这个问题的答案,位于How to make a socket a stream? To connect https response to S3 after imagemagick。根据 loganfsmyth 的建议,我评论了 req.end(image) 行,但是当我尝试上传文件时,服务器只是超时。当我取消注释 req.end(image) 行时,我遇到了类似的行为,但图像成功上传到 S3 除外。如果取消注释 req.end(image) 行是正确的,有人可以为我澄清哪种方式是正确的,向浏览器发送响应以防止它超时的最佳方式是什么?

https.get(JSON.parse(queryResponse).data.url,function(res){

  graphicsmagick(res)
    .resize('50','50')
    .stream(function (err, stdout, stderr) {

      ws. = fs.createWriteStream(output)

      i = []

      stdout.on('data',function(data){
        i.push(data)
      })

      stdout.on('close',function(){
        var image = Buffer.concat(i)

        var req = S3Client.put("new-file-name",{
           'Content-Length' : image.length
          ,'Content-Type' : res.headers['content-type']
        })

        req.on('response',function(res){  //prepare 'response' callback from S3
          if (200 == res.statusCode)
            console.log('it worked')
        })
        //req.end(image)  //send the content of the file and an end
      })
  })
})

【问题讨论】:

    标签: node.js amazon-web-services amazon-s3 imagemagick knox-amazon-s3-client


    【解决方案1】:

    基本上,该页面被请求两次,这导致图像因网站图标而被覆盖。 node.js page refresh calling resources twice?

    【讨论】:

      【解决方案2】:

      在您链接到的问题中,用户使用的是putStream,因此调用req.end() 是不正确的,但是在您的情况下,您直接使用put,因此您需要调用req.end()。否则,如果将其注释掉,您将永远不会真正使用 image 值,除了长度,因此您永远不会发送图像数据。

      如果没有看到实际运行此代码的服务器处理程序,很难判断,但您需要(可选)返回一些响应,然后 .end() 也将实际连接到浏览器,否则它将在那里等待。

      所以如果你有这样的东西

      http.createServer(function(req, browserResponse){
      
        // Other code.
      
        req.on('response',function(s3res){  //prepare 'response' callback from S3
            if (200 == s3res.statusCode) console.log('it worked')
      
      
            // Close the response. You also pass it data to send to the browser.
            browserResponse.end();
        })
      
        // Other code.
      });
      

      【讨论】:

        猜你喜欢
        • 2012-09-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-16
        • 1970-01-01
        • 2014-07-09
        • 2017-06-13
        • 2015-09-18
        • 2017-12-12
        相关资源
        最近更新 更多