{"library":"lws-static","title":"lws-static: Static File Server Middleware","description":"lws-static is a dedicated middleware plugin for the lws (local-web-server) ecosystem, designed to serve static files efficiently. It internally wraps koa-static, leveraging its robust capabilities for features like caching and directory indexing. The package is currently at version 3.1.1, actively maintained with periodic updates to its underlying dependencies, notably koa-static, and to ensure compatibility with the lws core framework. Its primary differentiator is seamless integration with lws's configuration system, allowing static file serving to be configured via concise CLI arguments or a programmatic lws setup. This approach simplifies the creation of development servers by offering a unified interface for defining the static root directory, cache control (max-age), request deferral, custom index files, and additional filename extensions, abstracting away direct koa-static configuration for lws users.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install lws-static"],"cli":{"name":"lws","version":null}},"imports":["import createStaticMiddleware from 'lws-static';","import Lws from 'local-web-server';","import type { Options } from 'lws-static';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import Lws from 'local-web-server';\nimport createStaticMiddleware from 'lws-static';\nimport path from 'path';\nimport fs from 'fs';\n\n// Create a temporary directory and file for demonstration\nconst tempDir = path.join(process.cwd(), 'lws-public-temp');\nconst tempFile = path.join(tempDir, 'index.html');\n\n// Ensure the directory exists and contains an index file\nfs.mkdirSync(tempDir, { recursive: true });\nfs.writeFileSync(tempFile, '<h1>Hello from lws-static!</h1>', 'utf8');\n\nconst lws = new Lws({\n  port: 8080,\n  stack: [\n    createStaticMiddleware({\n      directory: tempDir, // Serve from the temporary 'lws-public-temp' directory\n      maxAge: 3600, // Cache for 1 hour (in seconds)\n      index: 'index.html', // Default file if a directory is requested\n      defer: true // Allow other downstream middleware to respond first\n    })\n  ]\n});\n\nlws.start();\nconsole.log(`lws-static server running on http://localhost:8080`);\nconsole.log(`Serving files from: ${tempDir}`);\nconsole.log('Access http://localhost:8080 to see the static content.');\nconsole.log('Press Ctrl+C to stop the server and clean up.');\n\n// Clean up on exit\nprocess.on('SIGINT', () => {\n  console.log('\\nStopping server and cleaning up...');\n  lws.server.close(() => {\n    if (fs.existsSync(tempDir)) {\n      fs.rmSync(tempDir, { recursive: true, force: true });\n      console.log('Temporary directory cleaned up.');\n    }\n    process.exit(0);\n  });\n});","lang":"typescript","description":"Demonstrates programmatic setup of `lws` with `lws-static` to serve static files from a temporary directory. It configures caching, an index file, and proper cleanup.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}