{"library":"send","title":"Node.js HTTP Static File Streaming","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install send"],"cli":null},"imports":["import send from 'send'","const send = require('send')","import type { SendStream } from 'send'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import http from 'http';\nimport path from 'path';\nimport send from 'send';\nimport { fileURLToPath } from 'url';\n\n// In an ESM module, __dirname is not directly available. Recreate it.\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\n\n// Create a simple HTTP server\nconst server = http.createServer((req, res) => {\n  const filePath = path.join(__dirname, 'public', req.url || 'index.html');\n  \n  // Basic example of creating a public directory and a file\n  // For a real application, ensure 'public' directory exists with files\n  // or adjust `root` option accordingly.\n  // Example: echo '<h1>Hello from send!</h1>' > public/index.html\n\n  send(req, req.url || '/', { root: path.join(__dirname, 'public') })\n    .on('error', (err) => {\n      if (err.statusCode === 404) {\n        res.statusCode = 404;\n        res.end('File not found');\n      } else {\n        res.statusCode = 500;\n        res.end('Internal Server Error: ' + err.message);\n      }\n    })\n    .on('directory', () => {\n      // Redirect or serve index.html for directory requests if desired\n      res.statusCode = 301;\n      res.setHeader('Location', req.url + '/index.html');\n      res.end('Redirecting to index.html');\n    })\n    .on('end', () => {\n      console.log(`Served: ${req.url}`);\n    })\n    .pipe(res);\n});\n\nconst PORT = 3000;\nserver.listen(PORT, () => {\n  console.log(`Server listening on http://localhost:${PORT}`);\n  console.log(`Try http://localhost:${PORT}/index.html (create 'public/index.html' first)`);\n});\n","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}