是的,我知道 Uploading images using Node.js, Express, and Mongoose和 How can I store images in mongodb with node.js?但解决这些问题并不是我想要实现的。我希望能够使用 Schema 将图像存储在二进制数据库中(Mongodb with Mongoose)。
那么,您知道使用组合 Node.js、Express 和 Mongodb 的任何应用程序吗?如果有这个组合 + Jade 的应用程序最好,但我想这要求太多了。
提前致谢!
最佳答案
与 mongodb 一起提供的 GridFS 可能是一种选择。 nodeJS(无论express还是jade)读写文件的代码如下:
const { MongoClient } = require('mongodb');
const url = 'mongodb://localhost:27017';
const client = new MongoClient(url);
const fs = require('fs');
const mongodb= require('mongodb');
const connection = client.connect();
const dbName = 'myProject';
const connect = connection;
connect.then(() => {
console.log('Connected successfully to server');
const db = client.db(dbName);
const bucket = new mongodb.GridFSBucket(db, { bucketName: 'myCustomBucket' });
fs.createReadStream('./test.txt').pipe(bucket.openUploadStream('test.txt'));
bucket.openDownloadStreamByName('test.txt').pipe(fs.createWriteStream('./test2.txt'));
});
它就像一个存储二进制文件的存储桶。您可以使用mongoose-gridfs如果需要一种收藏的感觉,那就在它之上
关于node.js - 上传和存储图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10192452/