I have a server that's uploading images to the server. I need to optimize the image upload somehow. This is how I'm uploading my files:
uploadImage(file, uid, res) {
var fs = require('fs');
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', file => {
const {_id} = file;
return Account.findByIdAndUpdate(uid, {'employer.logo': _id}).then(() => res.redirect('/employer')).catch(e => console.log(e));
});
},
How can I optimize the image before the upload? Preferably if there is something gulp-like but for the server?
I ended up using this https://github.com/imagemin/imagemin. It's pretty straight forward. Here's my code: