0

I have been working on the answer from this question located here How to make a socket a stream? To connect https response to S3 after imagemagick. As per loganfsmyth recommendation I commented the req.end(image) line however when I attempt to upload a file the server simply times out. I experience similar behaviour when I uncomment the req.end(image) line with the exception that the image successfully uploadsto S3. Can someone clarify for me which way is correct also if it is right to uncomment the req.end(image) line what is the best way to send a response to the browser to prevent it from timing out?

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
      })
  })
})
Community
  • 1
  • 1
user1441287
  • 401
  • 2
  • 13

2 Answers2

0

Basically the page was being requested twice which caused the image to be overwritten because of the favicon. node.js page refresh calling resources twice?

Community
  • 1
  • 1
user1441287
  • 401
  • 2
  • 13
0

In the question you link to, the user was using putStream, so calling req.end() is incorrect, however in your case you are using put directly, so you need to call req.end(). Otherwise with it commented out, you never actually use the image value, except for the length, so you never send the image data.

It is hard to tell without seeing the server handler that actually runs this code, but you need to (optionally) return some response, and then .end() the actual connection to the browser too, or it will set there waiting.

So if you have something like this

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.
});
loganfsmyth
  • 156,129
  • 30
  • 331
  • 251