npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-responsive-images

v1.8.2

Published

Server-side scaling and caching of images on-the-fly for Express on Node.js. Adjusting images to client's screen size or scaling by query parameters (e.g. ?w=200). Mobile friendly, reduces bandwidth and saves loading time.

Downloads

47

Readme

express-responsive-images

Server-side scaling and caching of images on-the-fly for Express on Node.js (npm).

Demo application

basic features

  • scaling by next breakpoint (default)
  • scaling by query parameter (usefull for srcset)
  • filetype conversion (e.g. to webp)
  • cache is updated when image is modified

express-responsive-images

usage

npm i express-responsive-images

frontend

<head>
    <script>document.cookie = 'screen=' + ('devicePixelRatio' in window ? devicePixelRatio : 1) + ',' + window.innerWidth + '; path=/; SameSite=strict; Secure';</script>
</head>

(not necessary for directScaling)

backend

import responsiveImages from 'express-responsive-images';
// const responsiveImages = require('express-responsive-images');

// use it before declaring static routes
app.use(responsiveImages({
    staticDir: '/public',
    watchedDirectories: ['/images', '/media'], // nested in staticDir
    // options ...
}));

// static routes, something like this:
app.use('/', express.static(path.join(__dirname, 'public')));

That's all. The default behavior should already work. If staticDir and watchedDirectories are set correctly, the images should be delivered, not much larger than the screen size of the client. The folders of watchedDirectories should then also contain chached files.

options (default values)

app.use(responsiveImages({
    staticDir:          '/public',
    watchedDirectories: ['/images'],
    fileTypes:          ['webp', 'jpg', 'jpeg', 'png', 'gif'],
    fileTypeConversion: '',
    cacheSuffix:        '-cache',
    cookieName:         'screen',
    scaleBy:            'breakpoint',
    breakpoints:        [320, 480, 640, 800, 1024, 1280, 1366, 1440, 1600, 1920, 2048, 2560, 3440, 4096],
    directScaling:      false,
    directScalingParam: 'w',
    directScaleSizes:   [],
    debug:              false,
}));

staticDir (string)

The application's public directory with static files. For example: '/public' or '/pub' or '/dist' ...

staticDir: '/public'

It should match the directory used by Express. E.g.:

app.use('/', express.static(path.join(__dirname, 'public')));

watchedDirectories (array)

Array of directories nested in staticDir to watch for images. The module is listening to requests pointing to this folders.

At least one directory must be specified!

The use of wildcards * is possible.

(following examples with staticDir: '/public')

// will match only /public/images directory, no subdirectories
watchedDirectories: ['/images']

// will match e.g. /public/img-user but not /public/img and not subdirectories e.g. /public/img/user
watchedDirectories: ['/img*'] 

// will match e.g. /public/images/user and /public/images/user/profile but not /public/images
watchedDirectories: ['/images/*']

// will match e.g. /public/images and /public/images/user and /public/images/user/profile
watchedDirectories: ['/images', '/images/*']

fileTypes (array)

Array of permitted filetypes.

fileTypes: ['webp', 'jpg', 'jpeg', 'png', 'gif']

fileTypeConversion (string)

Converts images to another filetype.

fileTypeConversion: 'webp'

cacheSuffix (string)

Suffix of the cache folder name where the images should be cached.

For example, the cache folder of /public/images is /public/images-cache.

cacheSuffix: '-cache'

cookieName (string)

The name of the cookie is changeable. The name must be the same as the one mentioned in the <head> tag (section "usage/frontend" above).

cookieName: 'my-cookie-name'

scaleBy (string)

Possible values: breakpoint or viewport.

breakpoint scales images to the next equal or higher breakpoint (see breakpoints option below).

scaleBy: 'breakpoint'

viewport scales images exactly to the width of the client browser (not recommended for public websites, can bloat your webspace).

scaleBy: 'viewport'

(scaleBy is ignored if directScaling: true and the parameter w is sent)

breakpoints (array)

Array of allowed sizes to which images are scaled.

Example: A notebook with a width of 1280px creates and receives images scaled to a width of 1280px (exact breakpoint).

Another example: A mobile device with a width of 780px creates and receives images scaled to a width of 800px (next higher breakpoint).

breakpoints: [320, 480, 640, 800, 1024, 1280, 1366, 1440, 1600, 1920, 2048, 2560, 3440, 4096]

directScaling (boolean)

directScaling and directScaleSizes are used to scale images directly if the query parameter w is set.

For example images/img.jpg?w=180 scales img.jpg to 180px width and stores it in images-cache/180/img.jpg.

directScaling: false

It is recommended to combine this option with directScaleSizes to prevent your web space from getting bloated.

Example for img srcset (MDN Responsive images).

<img srcset="img.jpg?w=480 480w,
             img.jpg?w=800 800w"
     sizes="(max-width: 600px) 480px,
            800px"
     src="img.jpg?w=800"
     alt="">

directScalingParam (string)

The query parameter name for directScaling.

directScalingParam: 'myparam'

The url should look then like this: img.jpg?myparam=180.

directScaleSizes (array)

Array of allowed sizes (see directScaling option above).

If directScaling is enabled, it is recommended to specify the allowed image sizes as well.

To allow certain sizes, e.g. 180px and 260px:

directScaleSizes: [180, 260]

This allows only the following parameters:

  • path-to/img.jpg?w=180
  • path-to/img.jpg?w=260

Leave it empty to allow any image size (not recommended).

directScaleSizes: []

debug (boolean)

Enable this option to log errors and events on the console.

debug: true