Inspired by a post from Cloudinary, this middleware generates and delivers JPG-, PNG- and TIFF-images on the fly as WebP-images if the requesting client supports that format.
WebP is a new image format from Google that provides lossless and lossy compression for images on the web. For more information please read the official documentation or the article on html5rocks.com about image compression.
npm install webp-middleware
var http = require('http');
var express = require('express');
var webp = require('webp-middleware');
var app = express();
app.use(webp(__dirname + '/public', { ... }));
app.use(express.static(__dirname + '/public'));
It is important to insert the webp middleware before the express.static middleware.
First parameter is the path of the static folder.
Second parameter is an optional options object.
You can also have a look into the example folder for examples.
cacheDir
is the directory where the webp files are saved. Default is a folder webp-cache in the executing directory (path.join(process.cwd(), 'webp-cache')
)preset
delegates to the-preset
option at cwebpquality
delegates to the-q
option at cwebpalphaQuality
delegates to the-alpha_q
option at cwebpmethod
delegates to the-m
option at cwebpsegments
delegates to the-segments
option at cwebpbytes
delegates to the-size
option at cwebppsnr
delegates to the-psnr
option at cwebpsize
delegates to the-s
option at cwebp. Should be an object withwidth
andheight
keysspatialNoiseShaping
delegates to the-sns
option at cwebpfilter
delegates to the-f
option at cwebpstrong
delegates to the-strong
option at cwebp. Should be a boolean valuenoStrong
delegates to the-nostrong
option at cwebp. Should be a boolean valuepartitionLimit
delegates to the-partition_limit
option at cwebppass
delegates to the-pass
option at cwebpcrop
delegates to the-crop
option at cwebp. Should be an object withwidth
,height
,x
andy
keysresize
delegates to the-resize
option at cwebp. Should be an object withwidth
andheight
keysmultiThreading
delegates to the-mt
option at cwebp. Should be a boolean valuelowMemory
delegates to the-low_memory
option at cwebp. Should be a boolean valuealphaMethod
delegates to the-alpha_method
option at cwebpalphaCleanup
delegates to the-alpha_cleanup
option at cwebp. Should be a boolean valuenoAlpha
delegates to the-noalpha
option at cwebp. Should be a boolean valuelossless
delegates to the-lossless
option at cwebp. Should be a boolean valuehint
delegates to the-hint
option at cwebpmetadata
delegates to the-metadata
option at cwebp
Licensed under the MIT License.