The `send` library provides a low-level utility for streaming static files directly from the file system in Node.js, specifically designed for HTTP responses. It expertly handles features like partial content responses via Range headers, conditional-GET negotiation using If-Match, If-None-Match, If-Modified-Since, and ETag generation. It offers granular control over aspects such as caching (Cache-Control, maxAge, immutable), dotfile handling, and automatic file extension resolution. While a `1.x.x` series exists (up to 1.2.1), the most recent stable release is currently `0.19.2`, indicating a somewhat unconventional release cadence or a focus shift. It is a foundational component often leveraged by higher-level static file serving middleware like `serve-static` within web frameworks.
npm install sendVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to set up a basic Node.js HTTP server using `send` to serve static files from a 'public' directory, handling errors and directory requests.
Upgrade your Node.js environment to version 18 or higher. Alternatively, pin `send` to a `0.x.x` version.
Ensure that your application explicitly handles requests for directory paths (e.g., `/images/`) and redirects them to an appropriate resource (e.g., `/images/index.html`) or configures `index` option if `send` is meant to resolve them.
For explicit control over dotfile handling, set the `dotfiles` option to `'deny'`, `'allow'`, or `'ignore'` as appropriate for your application's security requirements. For example: `send(req, path, { dotfiles: 'deny' })`.Ensure the `path` argument is a URL-encoded string derived from `req.url` or a similar source. If constructing the path manually, use `encodeURIComponent()` if necessary, but generally `req.url` is already suitable.
When installing, consider `npm install send@latest` to get the version tagged as `latest` by the maintainers. Refer to the official GitHub repository or npm page for the intended release line and support status.
Use `const send = require('send')` for CommonJS or `import send from 'send'` for ESM. The `send` package provides a default export, not named exports.Double-check the `root` option to ensure it points to the correct base directory. Verify that the `path` argument accurately reflects the URL path (which `send` resolves relative to `root`). Ensure the file actually exists on the filesystem and has correct permissions. Remember `path` is URL-encoded, not a raw filesystem path.
If your project is CommonJS, ensure `send` is installed as a CJS-compatible version or configure your build system to handle ESM. If your project is ESM (e.g., `"type": "module"` in `package.json`), use `import send from 'send'`. Node.js 18+ generally handles interoperability well, but conflicts can arise if `send` is configured as ESM-only without CJS fallback.