anybody has a quick example to insert an image in mongodb using mongoose and nodejs express
i read some examples but i cant understand how to??
i want to upload an image in a form
app.post('/videos/new', function(req, res) {
req.form.complete(function(err, fields, files) {
console.log('here i go');
if(err) {
next(err);
} else {
ins = fs.createReadStream(files.file.path);
console.log('insssssssssssss'+ins);
ous = fs.createWriteStream(__dirname + '/static/uploads/videos/' + files.file.filename);
util.pump(ins, ous, function(err) {
if(err) {
next(err);
} else { RegProvider.save({
file: req.param(files.file.filename),
filename: req.param('filename')
}, function(error, docs) {
res.redirect('/videos');
});
}
});
//console.log('\nUploaded %s to %s', files.file.filename, files.file.path);
//res.send('Uploaded ' + files.file.filename + ' to ' + files.file.path);
}
});
});
like this example but i dont understand how it works
No, MongoDB is not a good place for storing files. If you want to store files, you should use storages like Amazon S3 or Google Could Storage. The good practice is to store the files in a storage and then to just save the URL of the uploaded image in the MongoDB.
You can connect to MongoDB with the mongoose.connect() method. mongoose.connect('mongodb://localhost:27017/myapp'); This is the minimum needed to connect the myapp database running locally on the default port (27017). If connecting fails on your machine, try using 127.0.0.1 instead of localhost .
util.pump
is deprecated.
You might try out https://github.com/aheckmann/gridform or https://github.com/aheckmann/connect-multipart-gridform and see if they meet your needs.
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