Node.js Express url image handler using sharp module to resize JPEG, PNG, WebP, AVIF and TIFF images

image, handler, image handler, image processing, image manipulation, image resize, jpeg, jpg, png, webp, avif, tiff, gif, svg, dzi, resize, thumbnail, crop, embed, sharp
npm install img-handler@0.0.1


img handler

The use for this Express Node.js module is to handle images by URL.


This needs to have sharp module installed previously.

npm install img-handler
const imgHandler = require('img-handler');

app.use( '/path', imgHandler() );


Images Directory and Extension File

It's recommended always set 'dir' option

'ext' option will set the extension of the image file

app.use( '/path', imgHandler({
  dir: './images/', // default './images'
  ext: '.png' // default '.jpg'

// recommended use of absolute path
app.use( '/path', imgHandler({
  dir: path.resolve('./images/')

If the image extension is not in the url it will use the one set in the 'ext' option or default


This option will assign where the parameters to use will be getting from

app.use( '/path', imgHandler({
  params: 'route' // default: 'route', opts: ['route', 'query', 'none']

route :{params}/image.jpg


query :{params}


none :


Options are simple letters, for 'route' param are uppercase letters, and 'query' param is not case sensitive

  • S (size): resize image to width x height (values >= 10)
  • W (width): pixels wide the resultant image should be. Use null or undefined to auto-scale the width to match the height.
  • H (height): pixels high the resultant image should be. Use null or undefined to auto-scale the height to match the width.
  • F (format): force output to a given format (optional, default: 'jpeg')
  • Q (quality): quality, integer 1-100 (optional)

'route' param can omit key "S" and just put the value.

Setting 'size' and, 'width' or 'height', the setted value will be that of 'size'.

Setting the same option more times, the setted value will be the first.

Formats and Qualities

Formats are simple letters too. If not set the 'quality' option, it will take the default value for each one.

  • j: jpeg, default quality: 80
  • p: png, default quality: 100
  • w: webp, default quality: 80
  • t: tiff, default quality: 80
  • a: avif, default quality: 50
  • h: heif, default quality: 50
  • r: raw
  • g: gif


Setting default values for each parameter, these will be assigned if no value has been setted for said option in 'route' or 'query', even for 'none'.

app.use( '/path', imgHandler({
  defaults: {
    size: {
      width: 100,
      height: 100
    format: {
      type: 'jpeg',
      options: {
        quality: 80


Copyright 2021 apvald.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.