如何通过Express.js驱动的API将GridFS存储文件(PDF)最好地提供给连接的客户端(iOS)



我正在开发一个REST HTTP API,它有iOS客户端连接到它。它目前的设置方式(并使用POSTman chrome ext进行测试)是我请求资源,我必须等待整个内容被读入并吐出,以便它作为响应显示出来。

这是iOS和Mac客户端消费的好方法吗?还是有更好的方法从GridFS服务?

我正在做以下事情:

  // Download a PDF
  app.get('/api/download-pdf/:pdf_id', function(req, res){
    var gfs = new mongodb.GridStore(mongoose.connection.db, ObjectID(req.params.pdf_id), "r");
    gfs.open(function(err,gs) {
      if (err){
        res.send(500);
      } 
      else{
        gs.read(function(err,data) {
          res.header('Content-type','application/pdf');
          res.send(data);
          gs.close(function(err) {});
          if (err) throw(err);
        });
      }
    });
  });

节点驱动程序现在支持流式传输到GridFShttp://christiankvalheim.com/post/29753345741/new-features-in-the-driver-for-mongodb-2-2?8e43c3e0

gs.pipe(anotherStream)

请参阅Streams

最新更新