使用节点在服务器上进行图像优化

Image optimization on the server using node

我有一个正在向服务器上传图像的服务器。我需要以某种方式优化图像上传。这就是我上传文件的方式:

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));
    });
},

上传前如何优化图片?最好如果有一些东西 gulp-like 但对于服务器?

我最终使用了这个 https://github.com/imagemin/imagemin。这很简单。这是我的代码:

async uploadImage(file, uid, res) {
    const imagemin = require('imagemin');
    const imageminJpegtran = require('imagemin-jpegtran');
    const imageminPngquant = require('imagemin-pngquant');
    console.log(1);
    // const newFilePath = `${file.path}optimized`;
    const newFile = await imagemin([file.path], newPath, {
        plugins: [
            imageminJpegtran(),
            imageminPngquant({quality: '65-80'})
        ]
    });
    // newFile.path = newFilePath;
    console.log(2);
    console.log(file);
    console.log(newFile);
    var fs = require('fs');
    await 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: newFile.filename,
    });
    fs.createReadStream(newFile.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));
    });
},