Yes, I know about Uploading images using Node.js, Express, and Mongoose and How can I store images in mongodb with node.js? but the solution to those isn't what I want to achieve. I want to be able to use a Schema for the image to be stored in binary the db (Mongodb with Mongoose).
So, do you know of any applications that uses the combo Node.js, Express and Mongodb? The best would be if there were any applications with this combo + Jade, but I guess that's to much to ask for.
Thanks in advance!
GridFS that comes along with mongodb might be an option. Code for nodeJS (irrespective of express or jade) to read and write a file is as below:
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'));
});
It acts like a bucket kind of a store that stores binary. You can use mongoose-gridfs on top of it if a collection kind of a feel is needed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With