@naumovs/image-middleware-sharp

Middleware to process images with sharp converter


Keywords
image, middleware, sharp, transform, webp
License
MIT
Install
npm install @naumovs/image-middleware-sharp@1.0.0

Documentation

@naumovs/image-middleware-sharp

Express/Connect middleware to transform images on-demand.

Usage

app.use(resizer({
  // Root path to the folder with original images
  root: 'public',
  // Root path to the folder with cached images
  cache: 'public',

  // list of transforms that are available
  transforms: {
    test(stream, sharp) {
      return stream
        .resize(200)
        .normalise()
        .crop();
    }
  }

  // prefix for your transforms in the url
  prefix: 't_',
}));

Given configuration above and the file available in public/uploads/file.png you will be able to access the transformed file with one of following requests

GET /uploads/t_test/file.png
GET /uploads/t_test/file.png.webp
HEAD /uploads/t_test/file.png
HEAD /uploads/t_test/file.png.webp

After first access a new file will be generated in cache folder at requested path. Next time it will serve cached file. If you want to omit cache add ?nocache to the url