在GridFS,express,mongoDB,node.js中存储来自POST请求的数据流 [英] Storing data stream from POST request in GridFS, express, mongoDB, node.js

查看:128
本文介绍了在GridFS,express,mongoDB,node.js中存储来自POST请求的数据流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何将图像直接发布到GridFS,而不将其作为临时文件存储在服务器上的任何位置。

I am trying to figure out how I can post an image directly to GridFS without storing it anywhere on the server as a temporary file first.

我使用Postman(chrome ext。)发布文件,我设法将此帖子存储为文件:

I am using Postman (chrome ext.) to post a file, and I manage to store this post as a file using:

req.pipe(fs.createWriteStream('./test.png'));

当从一个文件创建readStream时,我也可以从readStream直接存储到GridFS服务器。 (见代码)

I am also able to store directly to GridFS from a readStream when the readStream is created from a file on the server. (see code)

我有以下文件, saveFromReq.js ,它监听POST,基本上只是通过这个到 savePic.js

I have the following files, saveFromReq.js which listens for the POST and basically just passes this on to the savePic.js.

saveFromReq.js:

saveFromReq.js:

var express = require('express');
var app = express();
var savePic = require('./savePic');
var fs = require('fs');
var GridStore = require('mongodb').GridStore;
var pic = './square.png';
var picID;



//When the following

 //var pic = fs.createReadStream('./square.png', {autoClose: true});

//is not commented out, and 'req' is replaced with 'pic' in the savePic function,
//the file square.png is stored correctly to GridFS

app.post('/picture', function(req, res){

    savePic(req, function(id){});
    res.writeHead(200, {'Content-Type': 'text' });
    res.end("Sucsess!\n");

});

app.listen(process.env.PORT || 3413);

savePic.js:

savePic.js:

var savePic = function(req, callback){


var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');
    fs = require('fs');

    //When the following 

      //req.pipe(fs.createWriteStream('./test.png'));

    //is not commented out, the correct image is stored to test.png, and 
    //the sequence after req.on("data"... starts
    //(That sequence does not start at all when this is commented out..)

var fileId = new ObjectID();
var db = new Db('testDB', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {


  var gridStore = new GridStore(db, 'test', 'w');

  //open 
  gridStore.open(function(err, gridStore) {
    console.log("opened");


    req.on("data", function (data) {
        console.log("data recieved");
            gridStore.write(data, function (err, gridStore) {
                if (err) {
                    console.log("error writing file");
                }
            });
        });
     req.on("end", function () {
            gridStore.close(function (err, gridStore) {
                if (!err) {
                    console.log("The file has been stored to database.");
                    db.close();
                }
            });
        });
           req.pipe(gridStore);


      });




});
callback(fileId);
};
module.exports = savePic;

任何帮助都将不胜感激!

Any help would be greatly appreciated!

推荐答案

gridfs-stream 非常简单:

gridfs-stream makes that pretty easy:

// `gfs` is a gridfs-stream instance
app.post('/picture', function(req, res) {
  req.pipe(gfs.createWriteStream({
    filename: 'test'
  }));
  res.send("Success!");
});

这篇关于在GridFS,express,mongoDB,node.js中存储来自POST请求的数据流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆