Search code examples
node.jsexpresscouchdbcradle

Serving file with CouchDB attachment?


Im using Express. i can't figure out how to send an image file to client in a way that it will be displayed to HTML tag <img src='/preview/?doc=xxxxxx&image=img1.jpg'>. I'm using Cradle getAttachment function to communicate with Couchdb https://github.com/flatiron/cradle

db.getAttachment(id, filename, function (err, reply) {
    set('Content-Type', 'image/png');
    res.end(reply);
});

i don't know what reply is exactly and how to transfer that image to client without buffer


Solution

  • To transfer an attachment from cradle to a client without buffering, you can pipe its readableStream to the reponse's writableStream.

    The long version

    A variant of cradle's db.getAttachment returns a readableStream (see streaming from cradle's docs). express' res object on the other hand serves as a writableStream. This means you should be able* to pipe an attachment to it like this:

    // respond to a request like '/preview/?doc=xxxxxx&image=img1.jpg'
    app.get('/preview/', function(req, res){
    
      // fetch query parameters from url (?doc=...&image=...)
      var id = req.query.doc
      var filename = req.query.image
    
      // create a readableStream from the doc's attachment
      var readStream = db.getAttachment(id, filename, function (err) { 
        // note: no second argument
        // this inner function will be executed 
        // once the stream is finished
        // or has failed
        if (err)
          return console.dir(err)
        else
          console.dir('the stream has been successfully piped')
      })
      // set the appropriate headers here
      res.setHeader("Content-Type", "image/jpeg")
    
      // pipe the attachment to the client's response
      readStream.pipe(res)
    })
    

    Or, slightly shorter:

    app.get('/preview/', function(req, res){
      res.setHeader("Content-Type", "image/png")
      db.getAttachment(req.query.doc, req.query.image, someErrorHandlerFunction).pipe(res)
    })
    

    *I'm not at work, so sadly I cannot verify this code will run. Drop me a line if you have problems.