connect-static-file is a lightweight middleware for Connect and Express applications designed to serve a single static file. Unlike `express.static` which serves entire directories, this module focuses on efficiently delivering a specified file, making it ideal for specific assets like `index.html` or a `robots.txt`. The current stable version is 2.0.0, with its last major update occurring in 2017, suggesting a low-cadence, maintenance-focused project. A key differentiator is its `encoded` option, allowing developers to serve pre-compressed files (e.g., gzip) directly, optimizing client delivery by avoiding on-the-fly compression. It also provides fine-grained control over HTTP caching headers like `ETag`, `Last-Modified`, and `Cache-Control` (`maxAge`).
npm install connect-static-fileVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up an Express server to serve a single text file and a pre-gzipped JavaScript bundle using `connect-static-file`, including a fallback for browsers that don't support gzip. It shows basic caching and custom header options.
Upgrade Node.js to a supported version (>=0.12) or downgrade to `connect-static-file@1.x` if you must use Node.js v0.10.
Remove the `acceptRanges` option from your configuration if present, as it is no longer supported.
Always include a fallback middleware for the unencoded file immediately after the `encoded` version for the same route, e.g., `app.use('/bundle.js', staticFile('bundle.js.gz', {encoded: 'gzip'})); app.use('/bundle.js', staticFile('bundle.js'));`.For serving entire directories of static assets, consider using `express.static`, `connect-gzip-static`, or `ecstatic`.
Ensure `extensions` is explicitly set to an array (e.g., `['html', 'htm']`) if you want to use it for extension-less URLs. Remember it won't affect requests for URLs that already specify an extension.
Verify that the file path argument in `staticFile('/your/path', ...)` is correct and the file exists at that location with proper read permissions for the Node.js process.Ensure the URL path provided to `app.use('/your-url', staticFile(...))` exactly matches the path clients are requesting. If using the `encoded` option, verify a non-encoded fallback middleware is provided immediately after the encoded version for browsers that don't support the encoding.Confirm the client's `Accept-Encoding` header. If it includes 'gzip', ensure your middleware order is correct with the `encoded` version preceding any unencoded fallback for the same route. Double-check the spelling and value of the `encoded` option.
No dependency data recorded yet.