Hastily is an Express.js middleware designed to replicate the functionality of the Fastly Image Optimization API directly within your Node.js application. It enables on-the-fly image transformations and optimizations by parsing Fastly-compatible URL parameters and applying them to images served by an existing Express static server or image-serving middleware. This allows developers to integrate powerful image manipulation capabilities without relying on an external CDN service for basic optimizations. The package is currently at version 0.5.0, indicating pre-1.0 development where minor version bumps might introduce breaking changes. Its key differentiators include its drop-in compatibility with Express, leverage of the high-performance `sharp` library for image processing, and its focus on mimicking the Fastly API for familiar usage patterns.
npm install hastilyVerified import paths — ran on the pinned version, not inferred.
This quickstart sets up an Express server with `hastily` to serve and optimize images from a local `www/images` directory, demonstrating basic usage of the `imageopto` middleware with Fastly-compatible URL parameters. It includes setup for a dummy image file for immediate testing.
Run `npm install sharp` in your project's root directory. For specific environments (e.g., M1 Macs, Alpine Linux), consult `sharp`'s documentation for installation caveats.
Ensure the image files have supported extensions (e.g., `.jpg`, `.png`, `.webp`, `.svg`). Verify that your static file server is setting correct `Content-Type` headers for image responses. You might need to provide a custom `filter` function to `imageopto()` if your setup is non-standard.
Consult the `hastily` GitHub repository and npm changelog for specific breaking changes in new minor versions and adjust your code accordingly. Consider pinning to exact minor versions until 1.0 release.
Implement robust HTTP caching headers (`Cache-Control`, `ETag`, `Last-Modified`) for optimized images. Consider using a reverse proxy or CDN to cache common image variations. Monitor server resource usage and scale accordingly.
Run `npm install sharp` in your project's root directory. Ensure that `sharp` installs successfully without compilation errors specific to your system.
Add `"type": "module"` to your `package.json` file to enable ESM for your project, or rename your script files to `.mjs` extension. Alternatively, refactor to use CommonJS `const { imageopto } = require('hastily');` syntax if your project must remain CommonJS.Ensure image files have supported extensions (e.g., `.jpg`, `.png`, `.webp`, `.svg`). Verify your static file server is setting correct `Content-Type` headers (e.g., `image/jpeg`). If your setup is custom, consider providing a custom `filter` function to `imageopto()`.