很难理解 gridfs 和连接
Hard time understanding gridfs and connections
所以我看了这个视频:https://youtu.be/EVIGIcm7o2w
它解释了很多东西。但问题是我已经用 Express
创建了一个项目,所以我的连接是在 app.js 中完成的,如下所示:
// mongodb connection
var mongoose = require('mongoose');
var config = require('./globals/config');
mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
var db = mongoose.connection;
db.once('open', () => {
console.log('Connected to mongodb. Go to http://localhost:3000 to open app');
});
现在我想使用 gridFS,在不同的文件 中将文件上传到我的数据库,我试过这个:
var grid = require('gridfs-stream');
var mongoose = require('mongoose');
var fs = require('fs');
uploadFile(file) {
var config = require('./config');
mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
var conn = mongoose.connection;
grid.mongo = mongoose.mongo;
const gfs = grid(conn.db);
const writeStream = gfs.createWriteStream({
filename: file.filename,
});
fs.createReadStream(file.path).pipe(writeStream);
writeStream.on('close', function (file) {
console.log(`${file.filename} written to the db`);
});
}
但这没有意义,因为我要重新打开 2 个连接。我查看了 mongoose-gridfs,但它也显示了以某种 "app.js" 没有跨文件的方式使用它。如何在不打开 2 个连接和使用 2 个不同的 js 文件的情况下使用 gridfs?
创建一个单独的模块,假设 mygridfs.js
并将您的函数放在 module.exports
中。将 mongoose
作为此函数的参数:
var grid = require('gridfs-stream');
var fs = require('fs');
exports.uploadFile = function(file, mongoose) {
grid.mongo = mongoose.mongo;
const gfs = grid(mongoose.connection.db);
const writeStream = gfs.createWriteStream({
filename: file.filename,
});
fs.createReadStream(file.path).pipe(writeStream);
writeStream.on('close', function (file) {
console.log(`${file.filename} written to the db`);
});
}
在您的 app.js
主文件中,只需要模块:
var mygridfs= require("mygridfs");
mygridfs.uploadFile(file, mongoose);
所以我看了这个视频:https://youtu.be/EVIGIcm7o2w
它解释了很多东西。但问题是我已经用 Express
创建了一个项目,所以我的连接是在 app.js 中完成的,如下所示:
// mongodb connection
var mongoose = require('mongoose');
var config = require('./globals/config');
mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
var db = mongoose.connection;
db.once('open', () => {
console.log('Connected to mongodb. Go to http://localhost:3000 to open app');
});
现在我想使用 gridFS,在不同的文件 中将文件上传到我的数据库,我试过这个:
var grid = require('gridfs-stream');
var mongoose = require('mongoose');
var fs = require('fs');
uploadFile(file) {
var config = require('./config');
mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
var conn = mongoose.connection;
grid.mongo = mongoose.mongo;
const gfs = grid(conn.db);
const writeStream = gfs.createWriteStream({
filename: file.filename,
});
fs.createReadStream(file.path).pipe(writeStream);
writeStream.on('close', function (file) {
console.log(`${file.filename} written to the db`);
});
}
但这没有意义,因为我要重新打开 2 个连接。我查看了 mongoose-gridfs,但它也显示了以某种 "app.js" 没有跨文件的方式使用它。如何在不打开 2 个连接和使用 2 个不同的 js 文件的情况下使用 gridfs?
创建一个单独的模块,假设 mygridfs.js
并将您的函数放在 module.exports
中。将 mongoose
作为此函数的参数:
var grid = require('gridfs-stream');
var fs = require('fs');
exports.uploadFile = function(file, mongoose) {
grid.mongo = mongoose.mongo;
const gfs = grid(mongoose.connection.db);
const writeStream = gfs.createWriteStream({
filename: file.filename,
});
fs.createReadStream(file.path).pipe(writeStream);
writeStream.on('close', function (file) {
console.log(`${file.filename} written to the db`);
});
}
在您的 app.js
主文件中,只需要模块:
var mygridfs= require("mygridfs");
mygridfs.uploadFile(file, mongoose);