title: Compressing Images with Node.js and Google Cloud Storage
body: <p>I would like to resize and compress images after users upload them to my site. It seems like there are quite a few options for resizing images with node (e.g. <a href="https://github.com/lovell/sharp" rel="nofollow noreferrer">https://github.com/lovell/sharp</a>), but I would like to compress the images as well in order to save disk space and allow for faster serving times. Is there a library or something else that makes this possible?</p>
  
  <p>Here is a simplified version of my current (functioning) route as it stands today:</p>
  
  <pre><code>var router = require('express').Router();
  var bucket = require('../modules/google-storage-bucket');
  var Multer = require('multer');
  var multer = Multer({
    storage: Multer.memoryStorage(),
    limits: {
      fileSize: 5 * 1024 * 1024 // no larger than 5mb
    }
  });
  
  // Process the file upload and upload to Google Cloud Storage.
  router.post('/', multer.single('file'), (req, res) =&gt; {
  
    // rejecting if no file is uploaded
    if (!req.file) {
      res.status(400).send('No file uploaded.');
      return;
    }
  
    // Create a new blob in the bucket and upload the file data.
    var blob = bucket.file('fileName');
    var blobStream = blob.createWriteStream();
  
    blobStream.on('error', (err) =&gt; {
      console.log('error', err);
      res.send(500);
    });
  
    blobStream.on('finish', () =&gt; {
      console.log('everything worked!')
    });
  
    blobStream.end(req.file.buffer);
  });
  
  module.exports = router;
  </code></pre>